-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: node_js | ||
node_js: | ||
- "0.12" | ||
- "4" | ||
- "6" | ||
- "node" | ||
|
||
after_success: | ||
npm run coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,25 @@ | ||
# mongoose-plugin-disable-remove | ||
A plugin that will disable remove functions for mongoose models | ||
A plugin that will disable remove functions for mongoose models. | ||
|
||
If a schema enabled this schema, any call to `Model.remove` & `Document.remove` will return a reject promise or callback(err) if a callback provided. | ||
|
||
![travis](https://img.shields.io/travis/isayme/mongoose-plugin-disable-remove.svg) | ||
[![Coverage Status](https://coveralls.io/repos/github/isayme/mongoose-plugin-disable-remove/badge.svg?branch=master)](https://coveralls.io/github/isayme/mongoose-plugin-disable-remove?branch=master) | ||
|
||
## Install | ||
> npm install mongoose-plugin-disable-remove | ||
## Example | ||
|
||
```` | ||
var mongoose = require('mongoose') | ||
var disableRemovePlugin = require('mongoose-plugin-disable-remove') | ||
var YourSchema = new Schema({ | ||
content: String | ||
}) | ||
YourSchema.plugin(disableRemovePlugin) | ||
```` | ||
|
||
## Test | ||
> npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var mongoose = require('mongoose') | ||
var errmessage = 'remove function disabled for this schema' | ||
|
||
module.exports = function (schema) { | ||
// disable Model.remove and document.remove | ||
schema.methods.remove = schema.statics.remove = function () { | ||
var error = new Error(errmessage) | ||
|
||
var args = Array.prototype.slice.call(arguments) | ||
var callback = args[args.length - 1] | ||
var isCallbackModel = typeof callback === 'function' | ||
|
||
if (isCallbackModel) { | ||
callback(error) | ||
} else if (mongoose.Promise) { | ||
var MPromise = mongoose.Promise.ES6 || mongoose.Promise | ||
return new MPromise(function (resolve, reject) { | ||
reject(error) | ||
}) | ||
} else { | ||
throw error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
var assert = require('power-assert') | ||
var mongoose = require('mongoose') | ||
var Schema = mongoose.Schema | ||
var disableRemovePlugin = require('../index') | ||
|
||
var TestSchema = new Schema({ | ||
content: String | ||
}) | ||
|
||
TestSchema.plugin(disableRemovePlugin) | ||
var Test = mongoose.model('Test', TestSchema) | ||
|
||
describe(process.env.npm_package_name, function () { | ||
var errmessage = 'remove function disabled for this schema' | ||
|
||
describe('Document.remove', function () { | ||
it('shuold throw with callback', function (done) { | ||
var test = new Test() | ||
test.remove(function (err) { | ||
assert.ok(err instanceof Error) | ||
assert.ok(err.message === errmessage) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should return a reject promise if without callback', function (done) { | ||
var test = new Test() | ||
test.remove() | ||
.catch(function (err) { | ||
assert.ok(err instanceof Error) | ||
assert.ok(err.message === errmessage) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should ok for custom promise', function (done) { | ||
var Promise = mongoose.Promise | ||
mongoose.Promise = require('bluebird') | ||
var test = new Test() | ||
test.remove() | ||
.catch(function (err) { | ||
assert.ok(err instanceof Error) | ||
assert.ok(err.message === errmessage) | ||
mongoose.Promise = Promise | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Model.remove', function () { | ||
it('shuold throw with callback', function (done) { | ||
Test.remove({}, function (err) { | ||
assert.ok(err instanceof Error) | ||
assert.ok(err.message === errmessage) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should return a reject promise if without callback', function (done) { | ||
Test.remove({}) | ||
.catch(function (err) { | ||
assert.ok(err instanceof Error) | ||
assert.ok(err.message === errmessage) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should ok for custom promise', function (done) { | ||
var Promise = mongoose.Promise | ||
mongoose.Promise = require('bluebird') | ||
Test.remove() | ||
.catch(function (err) { | ||
assert.ok(err instanceof Error) | ||
assert.ok(err.message === errmessage) | ||
mongoose.Promise = Promise | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |