mongoose-update-if-current
Advanced tools
Comparing version 1.1.2 to 1.2.0
{ | ||
"name": "mongoose-update-if-current", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Optimistic concurrency control (OCC) plugin for mongoose", | ||
"main": "dist/index.js", | ||
"main": "lib/index.js", | ||
"typings": "typings/index.d.ts", | ||
@@ -12,7 +12,7 @@ "engines": { | ||
"test": "jest", | ||
"build": "npm run clean && npm run tsc", | ||
"build": "npm run clean && npm run babel", | ||
"ci": "npm run build && npm test -- --coverage --no-cache", | ||
"clean": "del-cli 'dist/**' '!dist' 'typings/**' '!typings'", | ||
"tsc": "tsc --project tsconfig.json", | ||
"tslint": "tslint --project tsconfig.json", | ||
"clean": "del-cli 'lib/**' '!lib'", | ||
"babel": "babel src --out-dir lib --source-maps", | ||
"lint": "eslint src __tests__", | ||
"prepack": "npm run build && npm test" | ||
@@ -45,15 +45,20 @@ }, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.15", | ||
"@types/mongoose": "^5.5.8", | ||
"@types/node": "^12.6.8", | ||
"@babel/cli": "^7.5.5", | ||
"@babel/core": "^7.5.5", | ||
"@babel/node": "^7.5.5", | ||
"@babel/preset-env": "^7.5.5", | ||
"babel-core": "^6.26.3", | ||
"babel-jest": "^24.8.0", | ||
"core-js": "^3.2.0", | ||
"del-cli": "^2.0.0", | ||
"eslint": "^6.1.0", | ||
"eslint-config-google": "^0.13.0", | ||
"jest": "^24.8.0", | ||
"mongoose": "^5.6.4", | ||
"ts-jest": "^24.0.2", | ||
"ts-node": "^8.3.0", | ||
"tslint": "^5.18.0", | ||
"tslint-config-airbnb": "^5.11.1", | ||
"typescript": "^3.5.3" | ||
"mongoose": "^5.6.9", | ||
"regenerator-runtime": "^0.13.3", | ||
"uuid": "^3.3.2" | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"kareem": "^2.3.0" | ||
}, | ||
"peerDependencies": { | ||
@@ -64,9 +69,9 @@ "mongoose": ">=5.0.0" | ||
"setupFilesAfterEnv": [ | ||
"<rootDir>/__tests__/__setup__.ts" | ||
"<rootDir>/__tests__/__setup__.js" | ||
], | ||
"transform": { | ||
".(ts|tsx)": "ts-jest" | ||
"^.+\\.jsx?$": "babel-jest" | ||
}, | ||
"collectCoverageFrom": [ | ||
"src/**/*.ts" | ||
"src/**/*.js" | ||
], | ||
@@ -78,3 +83,3 @@ "coveragePathIgnorePatterns": [ | ||
"testEnvironment": "node", | ||
"testRegex": "__tests__/.*\\.spec\\.ts$", | ||
"testRegex": "__tests__/.*\\.spec\\.js$", | ||
"moduleFileExtensions": [ | ||
@@ -81,0 +86,0 @@ "ts", |
@@ -10,3 +10,3 @@ # mongoose-update-if-current | ||
> Optimistic concurrency control plugin for [Mongoose](http://mongoosejs.com) v4.8 and higher. | ||
> Optimistic concurrency control plugin for [Mongoose](http://mongoosejs.com) v5.0 and higher. | ||
@@ -33,3 +33,3 @@ This plugin brings optimistic concurrency control to Mongoose documents by incrementing document version numbers on each save, and preventing previous versions of a document from being saved over the current version. | ||
/* Using require() */ | ||
var updateIfCurrentPlugin = require('mongoose-update-if-current').updateIfCurrentPlugin; | ||
const { updateIfCurrentPlugin } = require('mongoose-update-if-current'); | ||
``` | ||
@@ -48,5 +48,17 @@ | ||
Default behaviour is to use the schema's version key (`__v` by default) to implement concurrency control. | ||
The plugin can be configured to use timestamps (`updatedAt` by default) instead, if they are enabled on the schema: | ||
```javascript | ||
/* Global plugin - remember to add { timestamps: true } to each schema */ | ||
mongoose.plugin(updateIfCurrentPlugin, { strategy: 'timestamp' }); | ||
/* Single schema */ | ||
const mySchema = new mongoose.Schema({ ... }, { timestamps: true }); | ||
mySchema.plugin(updateIfCurrentPlugin, { strategy: 'timestamp' }); | ||
``` | ||
The plugin will hook into the `save()` function on schema documents to increment the version and check that it matches the version in the database before persisting it. | ||
**NB:** If the schema does not have a version key, then the plugin will enable the default version key of `__v`. If the schema has a custom version key set, then the plugin will automatically regognise and use it. | ||
**NB:** If the schema has a custom version key or timestamp field set, then the plugin will automatically regognise and use it. An error will be throws if you attempt to add the plugin to a schema without the fields to support it. | ||
@@ -79,3 +91,3 @@ ## Usage | ||
```javascript | ||
let book = await Book.findOne({ title: 'The Prince'}); | ||
let book = await Book.findOne({ title: 'The Prince' }); | ||
book.title = 'Il Principe'; | ||
@@ -100,7 +112,7 @@ book = await book.save(); | ||
// Before the call to save() above, so book.__v is 0 | ||
let book = await Book.findOne({ title: 'The Prince'}); | ||
let book = await Book.findOne({ title: 'The Prince' }); | ||
// Now the other user updates the book, so our version is out of date | ||
// Try to update the book based on the stale version | ||
book.author = 'Niccolò di Bernardo dei Machiavelli'; | ||
book = await book.save(); // throws | ||
book = await book.save(); // throws a VersionError | ||
``` | ||
@@ -110,13 +122,15 @@ | ||
**NB:** The plugin throws a `VersionError` when in `{ strategy: 'version' }` mode, but throws a `DocumentNotFoundError` when in `{ strategy: 'timestamp' }` mode. | ||
See the `__tests__` directory for more usage examples. | ||
## Caveats | ||
## Notes | ||
- The plugin manages concurrency when a document is updated using `Document.save()`, but you can still force updates using `Model.update()`, `Model.findByIdAndUpdate()` or `Model.findOneAndUpdate()` if you so desire. | ||
- Due to its reliance on the document version key, this plugin might not be compatible with others that affect this key. | ||
- The plugin causes the document's version to be incremented whenever `save()` is called, contrary to Mongoose's default behaviour. | ||
- The plugin relies on either `__v` or `updatedAt` to implement concurrency control; as such, this plugin might not be compatible with other plugins that alter these fields. | ||
- The plugin causes the document's version to be incremented whenever `save()` is called when using it for concurrency control. | ||
## Development | ||
The project uses the [AirBnB JavaScript code style](https://github.com/airbnb/javascript) adapted for [TypeScript](https://github.com/progre/tslint-config-airbnb). The test suites are built on Facebook's [Jest](https://facebook.github.io/jest/). Make sure that any changes you make are fully tested and linted before submitting a pull request! | ||
The project uses the [Google JavaScript code style](https://google.github.io/styleguide/jsguide.html). The test suites are built on Facebook's [Jest](https://facebook.github.io/jest/). Make sure that any changes you make are fully tested and linted before submitting a pull request! | ||
@@ -129,4 +143,4 @@ | Command | Description | | ||
| `npm run clean` | Cleans build output directories | | ||
| `npm run tsc` | Transpiles TypeScript to ES5 | | ||
| `npm run tslint` | Lints TypeScript code | | ||
| `npm run babel` | Transpiles JavaScript code | | ||
| `npm run lint` | Lints JavaScript code | | ||
@@ -133,0 +147,0 @@ ## License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19891
10
92
143
2
14
+ Addedkareem@^2.3.0