Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/euqen/mongoose-paranoid-plugin
:rocket: Simple, lightweight and easy to use mongoose soft deletion plugin :rocket:
https://github.com/euqen/mongoose-paranoid-plugin
mongodb mongoose mongoose-plugin mongoosejs sequelize
Last synced: about 1 month ago
JSON representation
:rocket: Simple, lightweight and easy to use mongoose soft deletion plugin :rocket:
- Host: GitHub
- URL: https://github.com/euqen/mongoose-paranoid-plugin
- Owner: euqen
- Created: 2017-12-03T23:05:10.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-01-23T20:17:50.000Z (almost 3 years ago)
- Last Synced: 2024-09-27T20:41:02.278Z (about 2 months ago)
- Topics: mongodb, mongoose, mongoose-plugin, mongoosejs, sequelize
- Language: JavaScript
- Homepage:
- Size: 16.6 KB
- Stars: 19
- Watchers: 3
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
:rocket: Mongoose Paranoid Plugin :rocket:
This plugin allows to apply soft deletion of mongo db documents. It's simple, lightweight and easy to use. Inspired by Sequelize.
## Philosophy
All existing soft deletion plugins don't allow to disable quering by non-deleted documents. They all require to use their own-implemented methods. Sometimes you need to get all documents including deleted, sometimes you need to retrieve only non-deleted. All existing plugins provide their own separate methods to do this. It's not flexible, you need to call different methods depending on situations you faced with. Introducing soft deletion plugin, it's hide all deleted documents by default, but you are able to find all documents including deleted in several ways. See usage section
## Installation
Install using npm
```
npm install mongoose-paranoid-plugin
```## Usage
### Enable plugin
```
const mongoose = require('mongoose');
const mongooseParanoidPlugin = require('mongoose-paranoid-plugin');mongoose.plugin(mongooseParanoidPlugin, { field: 'deleted_at' })
```
Options:
- field: String. Column name which is used to store timestamp of deletion. Default value is 'deletedAt'
### Using in schemas
By default this plugin won't override any methods. To enable soft deletion of your document's you need to pass `paranoid` option into your model options.
```
const mongoose = require('mongoose');const schema = new mongoose.Schema({
firstName: { type: String },
lastName: { type: String }
}, {
paranoid: true,
});const User = meta.model('users', schema);
```This will enable soft deletion of your documents. All deleted models will be marked with `deletedAt` field by default and will not be retrieved by built-in mongoose methods. If you need to include deleted documents you need pass an attribute `{ paranoid: false }` to query options.
```
return User.find(query, {}, { paranoid: false });
```### Soft deletion behavior
The behavior of remove method is also very explicit. If you enabled soft deletion in your schema, `remove` method will mark the document with deletedAt field. Otherwise the document will be completely removed. You can also use `paranoid` method before removing to enable/disable soft deletion.
## Copyright
Copyright (c) 2017 Eugene Shilin See LICENSE for further details.