Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/weisjohn/mongoose-deleted
a soft-delete implementation utilizing mongoose middleware
https://github.com/weisjohn/mongoose-deleted
Last synced: about 1 month ago
JSON representation
a soft-delete implementation utilizing mongoose middleware
- Host: GitHub
- URL: https://github.com/weisjohn/mongoose-deleted
- Owner: weisjohn
- License: mit
- Created: 2015-07-23T05:37:55.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-14T22:53:45.000Z (about 9 years ago)
- Last Synced: 2024-11-16T01:12:45.611Z (about 1 month ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/mongoose-deleted
- Size: 14.6 KB
- Stars: 8
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-deleted
a soft-delete implementation utilizing mongoose middleware
### usage
```javascript
var mongoose = require('mongoose');
var mongoose_deleted = require('mongoose-deleted');
var user = new mongoose.Schema({ name: String });
mongoose_deleted(user);
user = mongoose.model('user', user);var name = "John Q Public";
var user1 = new user({ name : name });user1.save(function() {
user.findOne({ name : name }, function(err, doc) {
if (err || !doc) console.log('failed to find document');
doc.delete(function(err) {
user.findOne({ name: name }, function() {
if (!doc) console.log('soft delete worked');
})
});
});
});
```### documents
`mongoose-deleted` utilizes mongoose middleware to transparently modify queries to select for documents that are not `{ deleted: true }`. Documents that are `.delete()`-ed will not be returned. To explicitly return documents that are deleted:
```javascript
schema.find({ deleted: true }, function(err, docs) {
// ...
});
```Additionally, the `deleted` boolean property is set by default to not be selected/returned on a document.
To have `deleted` normally returned:
```javascript
schema.plugin(mongoose_deleted, { select : true });
```To have the `deleted` property included, [in addition](http://mongoosejs.com/docs/api.html#query_Query-select) to the normal properties:
```javascript
schema.findOne(query).select('+deleted').exec(function(err, doc) {
console.log(doc.deleted);
});
```Or, to retrieve the `deleted` property only on a particular query, manually select for it:
```javascript
schema.findOne({}, { deleted : 1 }, function(err, doc) {
console.log(doc.deleted);
});
```### toJSON
By default, `mongoose-deleted` hides the `deleted` property on `doc.toJSON()`. This is configurable in the options:
```javascript
schema.plugin(mongoose_deleted, { toJSON : true });
```This can be overriden in a `toJSON()` call:
```javascript
var json = doc.toJSON({ deleted : true });
```### history
`mongoose-deleted` allows an optional integration with [`mongoose-history-log`](https://www.npmjs.com/package/mongoose-history-log) by passing in the options:
```javascript
mongoose_deleted(schema, { history: true });
```This will automatically insert a `{ status: 'deleted' }` object with the current time.