Giter Club home page Giter Club logo

mongoose-softdelete's Introduction

Build Status

Mongoose Soft Delete Plugin

Mongoose plugin that enables soft deletion of Models/Documents.

This plugin is based on the work of Yi.

What's Different

In the original plugin, models were deleted with a date reference only. This version takes that and uses a Boolean flag to to mark models deleted/restored. Adds deletedAt field to record when a document was deleted. Additionally, it removes a lot of overhead from the original code and doesn't use Coffeescript.

Also checkout Mongoose Delete by Sanel Deljkic.

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 --save

Tests

IMPORTANT: You need to have MongoDB running to run tests

npm test

Usage

models/test.js

const mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    soft_delete = require('mongoose-softdelete');

const TestSchema = new Schema({
  somefield: { type: String, default: 'Hello World!' },
});

TestSchema.plugin(soft_delete);

mongoose.model('Test', TestSchema);

controllers/test.js

const Test = mongoose.model('Test');
const test = new Test();

test.softdelete(function (err, newTest) {
  if (err) {
    callback(err);
  }
  callback(null, newTest);
});

test.restore(function (err, newTest) {
  if (err) {
    callback(err);
  }
  callback(null, newTest);
});

// chainable query method
// defaults to true unless specified
Test.find().isDeleted(false).exec();

Typescript

import { Schema, model } from 'mongoose';
import softdelete, { ISoftDeletedDocument } from 'mongoose-softdelete';

interface ITestDocument extends ISoftDeletedDocument {
    somefield: string;
}

const TestSchema = new Schema({
    somefield: { type: String, default: 'Hello World!' }
});

TestSchema.plugin(softdelete);

const Test = model<ITestDocument>('Test', TestSchema);
const test1 = new Test();

test1.softdelete(function (err, newTest: ITestDocument) {
    // ...
});

// chainable query method
// defaults to true unless specified
(Test.find({}) as unknown as ISoftDeletedDocumentQuery).isDeleted(false)

Built with love in Dhaka, Bangladesh by Riyadh Al Nur

mongoose-softdelete's People

Contributors

riyadhalnur avatar madeindjs avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.