Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
  • Loading branch information
isayme committed Apr 7, 2017
1 parent 94900b1 commit 1358e72
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 3 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
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
25 changes: 24 additions & 1 deletion README.md
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
24 changes: 24 additions & 0 deletions index.js
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
}
}
}
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"description": "A plugin that will disable remove functions for mongoose models",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "mocha",
"pretest": "npm run lint",
"lint": "standard --env mocha",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"coverage": "istanbul cover _mocha --report lcovonly -- -R spec test/**/*.js && npm run coveralls"
},
"repository": {
"type": "git",
Expand All @@ -21,5 +25,14 @@
"bugs": {
"url": "https://github.com/isayme/mongoose-plugin-disable-remove/issues"
},
"homepage": "https://github.com/isayme/mongoose-plugin-disable-remove#readme"
"homepage": "https://github.com/isayme/mongoose-plugin-disable-remove#readme",
"devDependencies": {
"bluebird": "^3.5.0",
"coveralls": "^2.13.0",
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"mongoose": "^4.9.3",
"power-assert": "^1.4.2",
"standard": "^10.0.1"
}
}
80 changes: 80 additions & 0 deletions test/index-test.js
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()
})
})
})
})

0 comments on commit 1358e72

Please sign in to comment.