mongoose-softdelete-typescript
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

Mongoose Soft Delete Plugin

Mongoose plugin that enables soft deletion of Models/Documents. This plugin also supports Mongo Transaction (from MongoDB 4.4).

This plugin is based on Mongoose Soft Delete by Riyadh Al Nur

License

This plugin is licensed under the MIT license and can ve viewed in the LICENSE file.

Installation

Install using npm

npm install mongoose-softdelete-typescript --save

Tests

IMPORTANT: You need to have MongoDB running to run tests

npm test

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();
}

Package Sidebar

Install

npm i mongoose-softdelete-typescript

Weekly Downloads

44

Version

0.0.3

License

MIT

Unpacked Size

10.1 kB

Total Files

5

Last publish

Collaborators

  • zolstar