0.0.3 • Published 8 years ago
mongoose-plugin-soft-deleted v0.0.3
mongoose-plugin-soft-deleted

a plugin for mongoose adding soft delete features
supportmongoose@5.0.x
Feautres
- add a field
deletedto the schema - add instance and static methods
deleteandrestoreto the schema - options to add
deletedAtwhich isDate - provide methods to append
deleted: falseautomatically like:async countNoDelete(cond)async findOneNoDeleted(cond)async findNoDeleted(cond)
TODO
- override or overwrite basic
findfindOneupdateupdateManycountetc...- 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-deletedbefore 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)optionsabove is like:{ deletedAt: true // true to set a deletedAt timestamp while deleting a doc }
Methods
WIPDevelop & 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