How to use `mongoose-delete` plugin with NestJs and typescript?

4.3k views Asked by At

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 -^
  }

3

There are 3 answers

1
Centaur On

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();
}
0
Sergio Suárez On

Try to restart you IDE (vscode if you use) after install this package: @types/mongoose-delete

0
GO-DIE On

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.

you can use it as a global plugin:

import { plugin } from 'mongoose';
import { SoftDelete } from 'soft-delete-mongoose-plugin';

// define 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
// ...