From 9a3e0f71bffcb87ca9564acd6c5695ba3d00b873 Mon Sep 17 00:00:00 2001 From: Mahdad Ghasemian Date: Tue, 26 Dec 2023 16:32:08 +0330 Subject: [PATCH 1/7] added findOne --- package-lock.json | 4 ++-- package.json | 2 +- src/soft-delete-plugin.ts | 11 +++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b158941..45c877d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "soft-delete-plugin-mongoose", - "version": "1.0.14", + "version": "1.0.15", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "soft-delete-plugin-mongoose", - "version": "1.0.14", + "version": "1.0.15", "license": "MIT", "devDependencies": { "@types/jest": "^29.4.0", diff --git a/package.json b/package.json index 51f6883..df41ce5 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "soft-delete-plugin-mongoose", + "name": "@mahdad/soft-delete-plugin-mongoose", "version": "1.0.15", "description": "a mongoose plugin that allows you to soft delete documents and restore them (for JS & TS)", "main": "dist/index.js", diff --git a/src/soft-delete-plugin.ts b/src/soft-delete-plugin.ts index e9cb03f..a8ca168 100644 --- a/src/soft-delete-plugin.ts +++ b/src/soft-delete-plugin.ts @@ -13,6 +13,17 @@ export const softDeletePlugin = (schema: mongoose.Schema) => { }, }); + // @ts-ignore + schema.pre('findOne', + async function (this, next: (err?: CallbackError) => void) { + if (this.getFilter().isDeleted === true) { + return next(); + } + this.setQuery({ ...this.getFilter(), isDeleted: { $ne: true } }); + next(); + }, + ); + // @ts-ignore schema.pre('find', async function (this, next: (err?: CallbackError) => void) { From c4a2f9cb01cb1c841f767cfa27bdd10ebf89d9bc Mon Sep 17 00:00:00 2001 From: Mahdad Ghasemian Date: Tue, 26 Dec 2023 16:41:24 +0330 Subject: [PATCH 2/7] 1.0.16 --- README.md | 12 ++++++------ package-lock.json | 8 ++++---- package.json | 4 ++-- tests/index.test.ts | 36 +++++++++++++++++++----------------- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 7a97140..f88af95 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -

Welcome to soft-delete-plugin-mongoose 👋

+

Welcome to @mahdad/soft-delete-plugin-mongoose 👋

- - Version + + Version Documentation @@ -27,7 +27,7 @@ ## Install ```sh -npm install soft-delete-plugin-mongoose +npm install @mahdad/soft-delete-plugin-mongoose ``` ## How It Works @@ -35,7 +35,7 @@ npm install soft-delete-plugin-mongoose **Javascript Version** ```js const mongoose = require('mongoose'); -const { softDeletePlugin } = require('soft-delete-plugin-mongoose'); +const { softDeletePlugin } = require('@mahdad/soft-delete-plugin-mongoose'); const Schema = mongoose.Schema; const TestSchema = new Schema({ @@ -82,7 +82,7 @@ const countAvailable = await TestModel.count(); **Typescript Version** ```ts import * as mongoose from 'mongoose'; -import { softDeletePlugin, SoftDeleteModel } from 'soft-delete-plugin-mongoose'; +import { softDeletePlugin, SoftDeleteModel } from '@mahdad/soft-delete-plugin-mongoose'; interface Test extends mongoose.Document { name: string; diff --git a/package-lock.json b/package-lock.json index 45c877d..066a06f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { - "name": "soft-delete-plugin-mongoose", - "version": "1.0.15", + "name": "@mahdad/soft-delete-plugin-mongoose", + "version": "1.0.16", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "soft-delete-plugin-mongoose", - "version": "1.0.15", + "name": "@mahdad/soft-delete-plugin-mongoose", + "version": "1.0.16", "license": "MIT", "devDependencies": { "@types/jest": "^29.4.0", diff --git a/package.json b/package.json index df41ce5..2c5c300 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mahdad/soft-delete-plugin-mongoose", - "version": "1.0.15", + "version": "1.0.16", "description": "a mongoose plugin that allows you to soft delete documents and restore them (for JS & TS)", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -8,7 +8,7 @@ "dist/**/*" ], "scripts": { - "clean": "del .\\dist\\*", + "clean": "rm -rf ./dist", "build": "npm run clean && tsc", "test": "jest" }, diff --git a/tests/index.test.ts b/tests/index.test.ts index 7b7725a..fe9530c 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,5 +1,5 @@ -import mongoose from 'mongoose'; -import { softDeletePlugin, SoftDeleteModel } from '../src/index'; +import mongoose from "mongoose"; +import { softDeletePlugin, SoftDeleteModel } from "../src/index"; interface User extends mongoose.Document { name: string; @@ -8,25 +8,28 @@ const UserSchema = new mongoose.Schema({ name: String, }); UserSchema.plugin(softDeletePlugin); -const userModel = mongoose.model>('User', UserSchema); +const userModel = mongoose.model>( + "User", + UserSchema +); -describe('soft delete plugin', () => { +describe("soft delete plugin", () => { beforeAll(async () => { - await mongoose.connect('mongodb://0.0.0.0:27017/test', { useNewUrlParser: true, useUnifiedTopology: true }); - }) + await mongoose.connect("mongodb://0.0.0.0:27017/test"); + }); afterAll(async () => { await mongoose.disconnect(); - }) + }); afterEach(async () => { await userModel.deleteMany(); }); - test('softDelete should be successed', async () => { + test("softDelete should be successed", async () => { // create one user - const user = await new userModel({ name: 'peter' }).save(); - expect(user.name).toBe('peter'); + const user = await new userModel({ name: "peter" }).save(); + expect(user.name).toBe("peter"); // get this user before we perform soft delete const userBeforeDelete = await userModel.find({ _id: user._id }); @@ -41,10 +44,10 @@ describe('soft delete plugin', () => { expect(userAfterDelete?.length).toBe(0); }); - test('restore should be successed', async () => { + test("restore should be successed", async () => { // create one user - const user = await new userModel({ name: 'peter' }).save(); - expect(user.name).toBe('peter'); + const user = await new userModel({ name: "peter" }).save(); + expect(user.name).toBe("peter"); // perform soft delete const softDeleteResp = await userModel.softDelete({ _id: user._id }); @@ -63,10 +66,10 @@ describe('soft delete plugin', () => { expect(userAfterRestore?.length).toBe(1); }); - test('findDeleted should be successed', async () => { + test("findDeleted should be successed", async () => { // create one user - const user = await new userModel({ name: 'peter' }).save(); - expect(user.name).toBe('peter'); + const user = await new userModel({ name: "peter" }).save(); + expect(user.name).toBe("peter"); // perform soft delete const softDeleteResp = await userModel.softDelete({ _id: user._id }); @@ -81,4 +84,3 @@ describe('soft delete plugin', () => { expect(deletedUsers.length).toBe(1); }); }); - From 966506ba3ff07d31e647db90f38b0b784b0c972d Mon Sep 17 00:00:00 2001 From: Mahdad Ghasemian Date: Tue, 26 Dec 2023 16:58:29 +0330 Subject: [PATCH 3/7] fix tsconfig --- README.md | 18 +++++++++--------- package.json | 10 +++++----- tsconfig.json | 22 ++++++++++++---------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index f88af95..31509a7 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,13 @@ Version - + Documentation - + Maintenance - + License: MIT

@@ -21,7 +21,7 @@ * **JS and TS** -### 🏠 [Homepage](https://github.com/nour-karoui/mongoose-soft-delete) +### 🏠 [Homepage](https://github.com/MahdadGhasemian/mongoose-soft-delete) ## Install @@ -138,21 +138,21 @@ const countAvailable = await this.test.count(); 👤 **Nour** -* Github: [@nour-karoui](https://github.com/nour-karoui) +* Github: [@MahdadGhasemian](https://github.com/MahdadGhasemian) * LinkedIn: [@nourkaroui](https://www.linkedin.com/in/nourkaroui/) ## 🤝 Contributing -Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/nour-karoui/mongoose-soft-delete/issues). You can also take a look at the [contributing guide](https://github.com/nour-karoui/mongoose-soft-delete/blob/master/CONTRIBUTING.md). +Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/MahdadGhasemian/mongoose-soft-delete/issues). You can also take a look at the [contributing guide](https://github.com/MahdadGhasemian/mongoose-soft-delete/blob/master/CONTRIBUTING.md). ## Show your support -Give a [STAR](https://github.com/nour-karoui/mongoose-soft-delete) if this project helped you! +Give a [STAR](https://github.com/MahdadGhasemian/mongoose-soft-delete) if this project helped you! ## 📝 License -* Copyright © 2021 [Nour](https://github.com/nour-karoui). -* This project is [MIT](https://github.com/nour-karoui/mongoose-soft-delete/blob/master/LICENSE) licensed. +* Copyright © 2021 [Nour](https://github.com/MahdadGhasemian). +* This project is [MIT](https://github.com/MahdadGhasemian/mongoose-soft-delete/blob/master/LICENSE) licensed. *** _This README was generated with by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_ diff --git a/package.json b/package.json index 2c5c300..712a26f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mahdad/soft-delete-plugin-mongoose", - "version": "1.0.16", + "version": "1.0.17", "description": "a mongoose plugin that allows you to soft delete documents and restore them (for JS & TS)", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -22,15 +22,15 @@ "mongoose soft delete typescript", "mongoose soft delete nestjs" ], - "author": "Nour KAROUI", + "author": "Mahdad Ghasemian", "repository": { "type": "git", - "url": "git+https://github.com/nour-karoui/mongoose-soft-delete" + "url": "git+https://github.com/MahdadGhasemian/mongoose-soft-delete" }, "bugs": { - "url": "https://github.com/nour-karoui/mongoose-soft-delete/issues" + "url": "https://github.com/MahdadGhasemian/mongoose-soft-delete/issues" }, - "homepage": "https://github.com/nour-karoui/mongoose-soft-delete", + "homepage": "https://github.com/MahdadGhasemian/mongoose-soft-delete", "license": "MIT", "peerDependencies": { "mongoose": "^7.6.1" diff --git a/tsconfig.json b/tsconfig.json index 28e3ebe..ee53f63 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,17 +4,17 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ - "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ + "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, + "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */ - "declaration": true, /* Generates corresponding '.d.ts' file. */ + "declaration": true /* Generates corresponding '.d.ts' file. */, // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./dist", /* Redirect output structure to the directory. */ + "outDir": "./dist" /* Redirect output structure to the directory. */, // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ @@ -25,7 +25,7 @@ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ /* Strict Type-Checking Options */ - "strict": true, /* Enable all strict type-checking options. */ + "strict": true /* Enable all strict type-checking options. */, // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* Enable strict null checks. */ // "strictFunctionTypes": true, /* Enable strict checking of function types. */ @@ -44,13 +44,13 @@ /* Module Resolution Options */ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + "baseUrl": "./src", /* Base directory to resolve non-absolute module names. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ - "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ @@ -65,7 +65,9 @@ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ /* Advanced Options */ - "skipLibCheck": true, /* Skip type checking of declaration files. */ - "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ - } + "skipLibCheck": true /* Skip type checking of declaration files. */, + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + }, + "include": ["src/**/*.ts"], + "exclude": ["tests"] } From e95927673d6e101fc85d091f21d7605994d7a896 Mon Sep 17 00:00:00 2001 From: Mahdad Ghasemian Date: Tue, 26 Dec 2023 17:12:32 +0330 Subject: [PATCH 4/7] support findOneAndUpdate --- package.json | 2 +- src/soft-delete-plugin.ts | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 712a26f..0f2d817 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mahdad/soft-delete-plugin-mongoose", - "version": "1.0.17", + "version": "1.0.18", "description": "a mongoose plugin that allows you to soft delete documents and restore them (for JS & TS)", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/soft-delete-plugin.ts b/src/soft-delete-plugin.ts index a8ca168..f2698ca 100644 --- a/src/soft-delete-plugin.ts +++ b/src/soft-delete-plugin.ts @@ -35,6 +35,17 @@ export const softDeletePlugin = (schema: mongoose.Schema) => { }, ); + // @ts-ignore + schema.pre('findOneAndUpdate', + async function (this, next: (err?: CallbackError) => void) { + if (this.getFilter().isDeleted === true) { + return next(); + } + this.setQuery({ ...this.getFilter(), isDeleted: { $ne: true } }); + next(); + }, + ); + // @ts-ignore schema.pre('count', async function (this, next: (err?: CallbackError) => void) { From 96d0c785566e05bdd19f8b5d7b872ef2a3f67b40 Mon Sep 17 00:00:00 2001 From: Mahdad Ghasemian Date: Tue, 26 Dec 2023 17:14:44 +0330 Subject: [PATCH 5/7] build --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0f2d817..22fd757 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mahdad/soft-delete-plugin-mongoose", - "version": "1.0.18", + "version": "1.0.19", "description": "a mongoose plugin that allows you to soft delete documents and restore them (for JS & TS)", "main": "dist/index.js", "types": "dist/index.d.ts", From 89c0075702c890af3430028405311858b99e1f25 Mon Sep 17 00:00:00 2001 From: Mahdad Ghasemian Date: Tue, 26 Dec 2023 17:37:09 +0330 Subject: [PATCH 6/7] write test --- LICENSE | 2 +- README.md | 58 +++++++++++++++++++++++++++++---------------- package.json | 2 +- tests/index.test.ts | 46 ++++++++++++++++++++++++++++++++++- 4 files changed, 84 insertions(+), 24 deletions(-) diff --git a/LICENSE b/LICENSE index 6db3ca3..1cedd73 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 NourKaroui +Copyright (c) 2021 MahdadGhasemian Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 31509a7..c111d1f 100644 --- a/README.md +++ b/README.md @@ -16,14 +16,12 @@ > a mongoose plugin that allows you to soft delete documents and restore them in MongoDB (for JS & TS) -* **Soft delete your MongoDB documents and restore them** - -* **JS and TS** +- **Soft delete your MongoDB documents and restore them** +- **JS and TS** ### 🏠 [Homepage](https://github.com/MahdadGhasemian/mongoose-soft-delete) - ## Install ```sh @@ -33,20 +31,21 @@ npm install @mahdad/soft-delete-plugin-mongoose ## How It Works **Javascript Version** + ```js -const mongoose = require('mongoose'); -const { softDeletePlugin } = require('@mahdad/soft-delete-plugin-mongoose'); +const mongoose = require("mongoose"); +const { softDeletePlugin } = require("@mahdad/soft-delete-plugin-mongoose"); const Schema = mongoose.Schema; const TestSchema = new Schema({ - name: String, - lastName: String + name: String, + lastName: String, }); TestSchema.plugin(softDeletePlugin); -const TestModel = mongoose.model("Test", TestSchema); +const TestModel = mongoose.model("Test", TestSchema); -const test = new TestModel({name: 'hello', lastName: "world"}); +const test = new TestModel({ name: "hello", lastName: "world" }); /*** returns an object containing the number of softDeleted elements ***/ /*** @@ -56,7 +55,10 @@ const test = new TestModel({name: 'hello', lastName: "world"}); the argument options is optional ***/ const options = { validateBeforeSave: false }; -const deleted = await TestModel.softDelete({ _id: test._id, name: test.name }, options); +const deleted = await TestModel.softDelete( + { _id: test._id, name: test.name }, + options +); /** const deleted = await Test.softDelete({ _id: test._id, name: test.name }); is also valid **/ @@ -73,6 +75,12 @@ const deletedElements = await TestModel.findDeleted(); /*** returns all available elements (not deleted) ***/ const availableElements = await TestModel.find(); +/*** returns one available element (not deleted) ***/ +const availableElements = await TestModel.findOne(); + +/*** update if is not be deleted (not deleted) ***/ +const availableElements = await TestModel.findOneAndUpdate(); + /*** counts all available elements (not deleted) ***/ const countAvailable = await TestModel.count(); @@ -80,6 +88,7 @@ const countAvailable = await TestModel.count(); ``` **Typescript Version** + ```ts import * as mongoose from 'mongoose'; import { softDeletePlugin, SoftDeleteModel } from '@mahdad/soft-delete-plugin-mongoose'; @@ -105,20 +114,20 @@ const test = await new this.testModel({name: 'hello', lastName: 'world'}); /*** returns an object containing the number of softDeleted elements ***/ /*** - {deleted: number} + {deleted: number} ***/ /*** the argument options is optional ***/ const options = { validateBeforeSave: false }; const deleted = await this.testModel.softDelete({ _id: test._id, name: test.name }, options); -/** +/** const deleted = await Test.softDelete({ _id: test._id, name: test.name }); is also valid **/ /*** returns an object containing the number of restored elements ***/ /*** - {restored: number} + {restored: number} ***/ const restored = await this.testModel.restore({ _id: test._id, name: test.name }); @@ -128,18 +137,24 @@ const deletedElements = await this.testModel.findDeleted(); /*** returns all available elements (not deleted) ***/ const availableElements = await this.testModel.find(); +/*** returns one available element (not deleted) ***/ +const availableElements = await this.testModel.findOne(); + +/*** update if is not be deleted (not deleted) ***/ +const availableElements = await this.testModel.findOneAndUpdate(); + /*** counts all available elements (not deleted) ***/ -const countAvailable = await this.test.count(); +const countAvailable = await this.testModel.count(); /*** findById returns the document whether deleted or not ***/ ``` ## Author -👤 **Nour** +👤 **MahdadGhasemian** -* Github: [@MahdadGhasemian](https://github.com/MahdadGhasemian) -* LinkedIn: [@nourkaroui](https://www.linkedin.com/in/nourkaroui/) +- Github: [@MahdadGhasemian](https://github.com/MahdadGhasemian) +- LinkedIn: [@nourkaroui](https://www.linkedin.com/in/nourkaroui/) ## 🤝 Contributing @@ -151,8 +166,9 @@ Give a [STAR](https://github.com/MahdadGhasemian/mongoose-soft-delete) if this p ## 📝 License -* Copyright © 2021 [Nour](https://github.com/MahdadGhasemian). -* This project is [MIT](https://github.com/MahdadGhasemian/mongoose-soft-delete/blob/master/LICENSE) licensed. +- Copyright © 2021 [MahdadGhasemian](https://github.com/MahdadGhasemian). +- This project is [MIT](https://github.com/MahdadGhasemian/mongoose-soft-delete/blob/master/LICENSE) licensed. + +--- -*** _This README was generated with by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_ diff --git a/package.json b/package.json index 22fd757..5295492 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mahdad/soft-delete-plugin-mongoose", - "version": "1.0.19", + "version": "1.0.20", "description": "a mongoose plugin that allows you to soft delete documents and restore them (for JS & TS)", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/tests/index.test.ts b/tests/index.test.ts index fe9530c..6557c92 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -15,7 +15,8 @@ const userModel = mongoose.model>( describe("soft delete plugin", () => { beforeAll(async () => { - await mongoose.connect("mongodb://0.0.0.0:27017/test"); + const url = "mongodb://0.0.0.0:27017/test"; + await mongoose.connect(url); }); afterAll(async () => { @@ -44,6 +45,49 @@ describe("soft delete plugin", () => { expect(userAfterDelete?.length).toBe(0); }); + test("softDelete should be successed on findOne", async () => { + // create one user + const user = await new userModel({ name: "peter" }).save(); + expect(user.name).toBe("peter"); + + // get this user before we perform soft delete + const userBeforeDelete = await userModel.findOne({ _id: user._id }); + expect(userBeforeDelete?.name).toBe("peter"); + + // perform soft delete + const softDeleteResp = await userModel.softDelete({ _id: user._id }); + expect(softDeleteResp.deleted).toBe(1); + + // get this user after we performed soft delete + const userAfterDelete = await userModel.findOne({ _id: user._id }); + expect(userAfterDelete).toBe(null); + }); + + test("softDelete should be successed on findOneAndUpdate", async () => { + // create one user + const user = await new userModel({ name: "peter" }).save(); + expect(user.name).toBe("peter"); + + // get this user before we perform soft delete + const userBeforeDelete = await userModel.findOneAndUpdate( + { _id: user._id }, + { name: "mahdad" }, + { new: true } + ); + expect(userBeforeDelete?.name).toBe("mahdad"); + + // perform soft delete + const softDeleteResp = await userModel.softDelete({ _id: user._id }); + expect(softDeleteResp.deleted).toBe(1); + + // get this user after we performed soft delete + const userAfterDelete = await userModel.findOneAndUpdate( + { _id: user._id }, + { name: "mahdad" } + ); + expect(userAfterDelete).toBe(null); + }); + test("restore should be successed", async () => { // create one user const user = await new userModel({ name: "peter" }).save(); From 628155c28f33973767b88231bd422dade05d05db Mon Sep 17 00:00:00 2001 From: Mahdad Ghasemian Date: Tue, 26 Dec 2023 17:57:50 +0330 Subject: [PATCH 7/7] support for findOne and findOneAndUpdate hooks --- LICENSE | 2 +- README.md | 80 ++++++++++++++++++--------------------------- package-lock.json | 8 ++--- package.json | 12 +++---- tests/index.test.ts | 2 +- tsconfig.json | 18 +++++----- 6 files changed, 53 insertions(+), 69 deletions(-) diff --git a/LICENSE b/LICENSE index 1cedd73..6db3ca3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 MahdadGhasemian +Copyright (c) 2021 NourKaroui Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index c111d1f..7a97140 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,52 @@ -

Welcome to @mahdad/soft-delete-plugin-mongoose 👋

+

Welcome to soft-delete-plugin-mongoose 👋

- - Version + + Version - + Documentation - + Maintenance - + License: MIT

> a mongoose plugin that allows you to soft delete documents and restore them in MongoDB (for JS & TS) -- **Soft delete your MongoDB documents and restore them** +* **Soft delete your MongoDB documents and restore them** -- **JS and TS** +* **JS and TS** + + +### 🏠 [Homepage](https://github.com/nour-karoui/mongoose-soft-delete) -### 🏠 [Homepage](https://github.com/MahdadGhasemian/mongoose-soft-delete) ## Install ```sh -npm install @mahdad/soft-delete-plugin-mongoose +npm install soft-delete-plugin-mongoose ``` ## How It Works **Javascript Version** - ```js -const mongoose = require("mongoose"); -const { softDeletePlugin } = require("@mahdad/soft-delete-plugin-mongoose"); +const mongoose = require('mongoose'); +const { softDeletePlugin } = require('soft-delete-plugin-mongoose'); const Schema = mongoose.Schema; const TestSchema = new Schema({ - name: String, - lastName: String, + name: String, + lastName: String }); TestSchema.plugin(softDeletePlugin); -const TestModel = mongoose.model("Test", TestSchema); +const TestModel = mongoose.model("Test", TestSchema); -const test = new TestModel({ name: "hello", lastName: "world" }); +const test = new TestModel({name: 'hello', lastName: "world"}); /*** returns an object containing the number of softDeleted elements ***/ /*** @@ -55,10 +56,7 @@ const test = new TestModel({ name: "hello", lastName: "world" }); the argument options is optional ***/ const options = { validateBeforeSave: false }; -const deleted = await TestModel.softDelete( - { _id: test._id, name: test.name }, - options -); +const deleted = await TestModel.softDelete({ _id: test._id, name: test.name }, options); /** const deleted = await Test.softDelete({ _id: test._id, name: test.name }); is also valid **/ @@ -75,12 +73,6 @@ const deletedElements = await TestModel.findDeleted(); /*** returns all available elements (not deleted) ***/ const availableElements = await TestModel.find(); -/*** returns one available element (not deleted) ***/ -const availableElements = await TestModel.findOne(); - -/*** update if is not be deleted (not deleted) ***/ -const availableElements = await TestModel.findOneAndUpdate(); - /*** counts all available elements (not deleted) ***/ const countAvailable = await TestModel.count(); @@ -88,10 +80,9 @@ const countAvailable = await TestModel.count(); ``` **Typescript Version** - ```ts import * as mongoose from 'mongoose'; -import { softDeletePlugin, SoftDeleteModel } from '@mahdad/soft-delete-plugin-mongoose'; +import { softDeletePlugin, SoftDeleteModel } from 'soft-delete-plugin-mongoose'; interface Test extends mongoose.Document { name: string; @@ -114,20 +105,20 @@ const test = await new this.testModel({name: 'hello', lastName: 'world'}); /*** returns an object containing the number of softDeleted elements ***/ /*** - {deleted: number} + {deleted: number} ***/ /*** the argument options is optional ***/ const options = { validateBeforeSave: false }; const deleted = await this.testModel.softDelete({ _id: test._id, name: test.name }, options); -/** +/** const deleted = await Test.softDelete({ _id: test._id, name: test.name }); is also valid **/ /*** returns an object containing the number of restored elements ***/ /*** - {restored: number} + {restored: number} ***/ const restored = await this.testModel.restore({ _id: test._id, name: test.name }); @@ -137,38 +128,31 @@ const deletedElements = await this.testModel.findDeleted(); /*** returns all available elements (not deleted) ***/ const availableElements = await this.testModel.find(); -/*** returns one available element (not deleted) ***/ -const availableElements = await this.testModel.findOne(); - -/*** update if is not be deleted (not deleted) ***/ -const availableElements = await this.testModel.findOneAndUpdate(); - /*** counts all available elements (not deleted) ***/ -const countAvailable = await this.testModel.count(); +const countAvailable = await this.test.count(); /*** findById returns the document whether deleted or not ***/ ``` ## Author -👤 **MahdadGhasemian** +👤 **Nour** -- Github: [@MahdadGhasemian](https://github.com/MahdadGhasemian) -- LinkedIn: [@nourkaroui](https://www.linkedin.com/in/nourkaroui/) +* Github: [@nour-karoui](https://github.com/nour-karoui) +* LinkedIn: [@nourkaroui](https://www.linkedin.com/in/nourkaroui/) ## 🤝 Contributing -Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/MahdadGhasemian/mongoose-soft-delete/issues). You can also take a look at the [contributing guide](https://github.com/MahdadGhasemian/mongoose-soft-delete/blob/master/CONTRIBUTING.md). +Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/nour-karoui/mongoose-soft-delete/issues). You can also take a look at the [contributing guide](https://github.com/nour-karoui/mongoose-soft-delete/blob/master/CONTRIBUTING.md). ## Show your support -Give a [STAR](https://github.com/MahdadGhasemian/mongoose-soft-delete) if this project helped you! +Give a [STAR](https://github.com/nour-karoui/mongoose-soft-delete) if this project helped you! ## 📝 License -- Copyright © 2021 [MahdadGhasemian](https://github.com/MahdadGhasemian). -- This project is [MIT](https://github.com/MahdadGhasemian/mongoose-soft-delete/blob/master/LICENSE) licensed. - ---- +* Copyright © 2021 [Nour](https://github.com/nour-karoui). +* This project is [MIT](https://github.com/nour-karoui/mongoose-soft-delete/blob/master/LICENSE) licensed. +*** _This README was generated with by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_ diff --git a/package-lock.json b/package-lock.json index 066a06f..45c877d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { - "name": "@mahdad/soft-delete-plugin-mongoose", - "version": "1.0.16", + "name": "soft-delete-plugin-mongoose", + "version": "1.0.15", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "@mahdad/soft-delete-plugin-mongoose", - "version": "1.0.16", + "name": "soft-delete-plugin-mongoose", + "version": "1.0.15", "license": "MIT", "devDependencies": { "@types/jest": "^29.4.0", diff --git a/package.json b/package.json index 5295492..cbaf067 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@mahdad/soft-delete-plugin-mongoose", - "version": "1.0.20", + "name": "soft-delete-plugin-mongoose", + "version": "1.0.15", "description": "a mongoose plugin that allows you to soft delete documents and restore them (for JS & TS)", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -22,15 +22,15 @@ "mongoose soft delete typescript", "mongoose soft delete nestjs" ], - "author": "Mahdad Ghasemian", + "author": "Nour KAROUI", "repository": { "type": "git", - "url": "git+https://github.com/MahdadGhasemian/mongoose-soft-delete" + "url": "git+https://github.com/nour-karoui/mongoose-soft-delete" }, "bugs": { - "url": "https://github.com/MahdadGhasemian/mongoose-soft-delete/issues" + "url": "https://github.com/nour-karoui/mongoose-soft-delete/issues" }, - "homepage": "https://github.com/MahdadGhasemian/mongoose-soft-delete", + "homepage": "https://github.com/nour-karoui/mongoose-soft-delete", "license": "MIT", "peerDependencies": { "mongoose": "^7.6.1" diff --git a/tests/index.test.ts b/tests/index.test.ts index 6557c92..a8ebd9c 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -15,7 +15,7 @@ const userModel = mongoose.model>( describe("soft delete plugin", () => { beforeAll(async () => { - const url = "mongodb://0.0.0.0:27017/test"; + const url = "'mongodb://0.0.0.0:27017/test'"; await mongoose.connect(url); }); diff --git a/tsconfig.json b/tsconfig.json index ee53f63..9cd85b1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,17 +4,17 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, - "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, + "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */ - "declaration": true /* Generates corresponding '.d.ts' file. */, + "declaration": true, /* Generates corresponding '.d.ts' file. */ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./dist" /* Redirect output structure to the directory. */, + "outDir": "./dist", /* Redirect output structure to the directory. */ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ @@ -25,7 +25,7 @@ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ /* Strict Type-Checking Options */ - "strict": true /* Enable all strict type-checking options. */, + "strict": true, /* Enable all strict type-checking options. */ // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* Enable strict null checks. */ // "strictFunctionTypes": true, /* Enable strict checking of function types. */ @@ -44,13 +44,13 @@ /* Module Resolution Options */ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - "baseUrl": "./src", /* Base directory to resolve non-absolute module names. */ + "baseUrl": "./src", /* Base directory to resolve non-absolute module names. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ - "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ @@ -65,8 +65,8 @@ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ /* Advanced Options */ - "skipLibCheck": true /* Skip type checking of declaration files. */, - "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + "skipLibCheck": true, /* Skip type checking of declaration files. */ + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ }, "include": ["src/**/*.ts"], "exclude": ["tests"]