0.0.3 • Published 6 years ago

mongoose-plugin-soft-deleted v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

mongoose-plugin-soft-deleted

NPM
a plugin for mongoose adding soft delete features

supportmongoose@5.0.x

Feautres

  • add a field deleted to the schema
  • add instance and static methods delete and restore to the schema
  • options to add deletedAt which is Date
  • provide methods to append deleted: false automatically like:
    • async countNoDelete(cond)
    • async findOneNoDeleted(cond)
    • async findNoDeleted(cond)

TODO

  • override or overwrite basic find findOne update updateMany count etc...
    • lease notice that mongoose offical doc doesn't recommend to override methods already exist
    • consider using middleware
  • add index.d.ts

Usage

  • npm i --save mongoose-plugin-soft-deleted
  • before creating schemas, register the plugin globally:

    const mongoose = require('mongoose')
    const softDeleted = requrie('mongoose-plugin-soft-deleted')
    
    const mongoCon = mongoose.createConnection('mongodb://localhost:27017/test')
    mongoCon.plugin(softDeleted, options)
  • or use it for a single schema, before creating a model:

    const mongoose = require('mongoose')
    const softDeleted = requrie('mongoose-plugin-soft-deleted')
    
    const foodSchema = mongoose.Schema({ name: String, bestBefore: Date })
    foodSchema.plugin(softDeleted, options)
  • options above is like:

    {
      deletedAt: true  // true to set a deletedAt timestamp while deleting a doc
    }

Methods

WIP

Develop & Test

  • prepare a mongodb instance with no authentication(simply localhost with a new mongo) and create a new test db(usually test)
  • edit ./test/lib/config.json
  • run the npm script test
  • write things as you like under js-standard-style

About

Nyan Nyan Nyan JavaScript Style Guide