🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

mongoose-softdelete-typescript

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-softdelete-typescript

Mongoose soft deletion plugin for typescript, support transaction

0.0.3
latest
Source
npm
Version published
Weekly downloads
253
36.76%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

mongoose

FAQs

Package last updated on 20 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts