'How to use `mongoose-delete` plugin with NestJs and typescript?
I'm using Mongoose in NestJs library and want to use mongoose-delete plugin for all of my schemas.
But I don't know how to use it with nestJS And Typescript.
First i installed both mongoose-delete and @Types/mongoose-delete libraries but there is no typescript documentary for This plugin.
This is the recommended method for adding plugin by nest:
MongooseModule.forRoot(MONGO_URI, {
connectionFactory: connection => {
connection.plugin(require('mongoose-delete'));
return connection;
},
}),
And absolutely this generates esLint error:
Require statement not part of import statement.eslint
And I cannot use delete function. It's not defined in the mongoose.Dcoument
export type ChannelDocument = Channel & Document;
constructor(
@InjectModel(Channel.name) private _channelModel: Model<ChannelDocument>,
) {}
async delete(id: string) {
this._channelModel.delete({ id });
// This is undefined -^
}
Solution 1:[1]
Try to restart you IDE (vscode if you use) after install this package: @types/mongoose-delete
Solution 2:[2]
use soft delete plugin => https://www.npmjs.com/package/soft-delete-mongoose-plugin
A simple and friendly soft delete plugin for mongoose?implementation using TS. Methods were added and overridden on mongoose model to realize soft deletion logic.
yuo can used as a global plugin:
import { plugin } from 'mongoose';
import { SoftDelete } from 'soft-delete-mongoose-plugin';
// defind soft delete field name
const IS_DELETED_FIELD = 'isDeleted';
const DELETED_AT_FIELD = 'deletedAt';
// use soft delete plugin
plugin(
new SoftDelete({
isDeletedField: IS_DELETED_FIELD,
deletedAtField: DELETED_AT_FIELD,
}).getPlugin(),
);
// other code
// ...
Solution 3:[3]
Please take a look at mongoose-softdelete-typescript.
import { Schema, model } from 'mongoose';
import { softDeletePlugin, ISoftDeletedModel, ISoftDeletedDocument } from 'mongoose-softdelete-typescript';
const TestSchema = new Schema({
name: { type: String, default: '' },
description: { type: String, default: 'description' },
});
TestSchema.plugin(softDeletePlugin);
const Test = model<ISoftDeletedDocument, ISoftDeletedModel<ISoftDeletedDocument>>('Test', TestSchema);
const test1 = new Test();
// delete single document
const newTest = await test1.softDelete();
// restore single document
const restoredTest = await test1.restore();
// find many deleted documents
const deletedTests = await Test.findDeleted(true);
// soft delete many documents with conditions
await Test.softDelete({ name: 'test' });
// support mongo transaction
const session = await Test.db.startSession();
session.startTransaction();
try {
const newTest = await test1.softDelete(session);
await session.commitTransaction();
} catch (e) {
console.log('e', e);
await session.abortTransaction();
} finally {
await session.endSession();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sergio Suárez |
| Solution 2 | GO-DIE |
| Solution 3 | Dharman |
