mongoose-plugin-soft-deleted
v0.0.3
Published
a plugin for mongoose 5.0.x adding soft delete features
Downloads
5
Readme
mongoose-plugin-soft-deleted
a plugin for mongoose adding soft delete features
support[email protected]
Feautres
- add a field
deleted
to the schema - add instance and static methods
delete
andrestore
to the schema - options to add
deletedAt
which isDate
- 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