Skip to content

Commit

Permalink
Merge 5add50a into ae5692e
Browse files Browse the repository at this point in the history
  • Loading branch information
joa-queen committed Nov 25, 2018
2 parents ae5692e + 5add50a commit de12b34
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -252,6 +252,20 @@ PetSchema.plugin(mongoose_delete, { indexFields: ['deleted', 'deletedBy'] });
PetSchema.plugin(mongoose_delete, { indexFields: ['deletedAt'] });


```

### Avoid default selection of `deleted`, `deletedAt` and `deletedBy`

```javascript
var mongoose_delete = require('mongoose-delete');

var PetSchema = new Schema({
name: String
});

// Index all field related to plugin (deleted, deletedAt, deletedBy)
PetSchema.plugin(mongoose_delete, { select: false });

```

## License
Expand Down
14 changes: 8 additions & 6 deletions index.js
Expand Up @@ -79,17 +79,18 @@ function createSchemaObject (typeKey, typeValue, options) {
module.exports = function (schema, options) {
options = options || {};
var indexFields = parseIndexFields(options)
var select = typeof options.select === 'undefined' ? true : options.select;

var typeKey = schema.options.typeKey;

schema.add({ deleted: createSchemaObject(typeKey, Boolean, { default: false, index: indexFields.deleted }) });
schema.add({ deleted: createSchemaObject(typeKey, Boolean, { default: false, index: indexFields.deleted, select: select }) });

if (options.deletedAt === true) {
schema.add({ deletedAt: createSchemaObject(typeKey, Date, { index: indexFields.deletedAt }) });
schema.add({ deletedAt: createSchemaObject(typeKey, Date, { index: indexFields.deletedAt, select: select }) });
}

if (options.deletedBy === true) {
schema.add({ deletedBy: createSchemaObject(typeKey, options.deletedByType || Schema.Types.ObjectId, { index: indexFields.deletedBy }) });
schema.add({ deletedBy: createSchemaObject(typeKey, options.deletedByType || Schema.Types.ObjectId, { index: indexFields.deletedBy, select: select }) });
}

schema.pre('save', function (next) {
Expand Down Expand Up @@ -122,14 +123,15 @@ module.exports = function (schema, options) {

finalList.forEach(function(method) {
if (method === 'count' || method === 'find' || method === 'findOne') {
var select = '+deleted +deletedAt +deletedBy';
schema.statics[method] = function () {
return Model[method].apply(this, arguments).where('deleted').ne(true);
return Model[method].apply(this, arguments).where('deleted').select(select).ne(true);
};
schema.statics[method + 'Deleted'] = function () {
return Model[method].apply(this, arguments).where('deleted').ne(false);
return Model[method].apply(this, arguments).where('deleted').select(select).ne(false);
};
schema.statics[method + 'WithDeleted'] = function () {
return Model[method].apply(this, arguments);
return Model[method].apply(this, arguments).select(select);
};
} else {
schema.statics[method] = function () {
Expand Down
8 changes: 4 additions & 4 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "mongoose-delete",
"version": "0.4.0",
"description": "Mongoose soft delete plugin",
"name": "@joakink/mongoose-delete",
"version": "0.6.0",
"description": "Fork for Mongoose soft delete plugin",
"author": "Sanel Deljkic <dsanel@gmail.com> (http://dsanel.github.io/)",
"main": "index.js",
"scripts": {
Expand All @@ -10,7 +10,7 @@
},
"repository": {
"type": "git",
"url": "git@github.com:dsanel/mongoose-delete.git"
"url": "git@github.com:joaqtor/mongoose-delete.git"
},
"keywords": [
"mongoose",
Expand Down

0 comments on commit de12b34

Please sign in to comment.