2

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

rostamiani
  • 2,859
  • 7
  • 38
  • 74

3 Answers3

1

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
// ... 
MikeH
  • 4,242
  • 1
  • 17
  • 32
GO-DIE
  • 11
  • 1
  • 3
0

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

Sergio Suárez
  • 136
  • 1
  • 4
-1

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();
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Centaur
  • 87
  • 1
  • This code is about adding the plugin to one single schema not to all schemas globally. I need to add it to all schemas. (Thanks for your package. In fact I'm using your package right now :-) ) – rostamiani Jun 01 '21 at 07:20