Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $setOnInsert modifier #669

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: nedb

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

publish-gpr:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ db.count({}, function (err, count) {
* `update` specifies how the documents should be modified. It is either a new document or a set of modifiers (you cannot use both together, it doesn't make sense!)
* A new document will replace the matched docs
* The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs. Available field modifiers are `$set` to change a field's value, `$unset` to delete a field, `$inc` to increment a field's value and `$min`/`$max` to change field's value, only if provided value is less/greater than current value. To work on arrays, you have `$push`, `$pop`, `$addToSet`, `$pull`, and the special `$each` and `$slice`. See examples below for the syntax.
* `$setOnInsert` will set a field if a new object is being inserted.
* `options` is an object with two possible parameters
* `multi` (defaults to `false`) which allows the modification of several documents if set to true
* `upsert` (defaults to `false`) if you want to insert a new document corresponding to the `update` rules if your `query` doesn't match anything. If your `update` is a simple object with no modifiers, it is the inserted document. In the other case, the `query` is stripped from all operator recursively, and the `update` is applied to it.
Expand Down
2 changes: 1 addition & 1 deletion lib/indexes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var BinarySearchTree = require('binary-search-tree').AVLTree
var BinarySearchTree = require('@yetzt/binary-search-tree').AVLTree
, model = require('./model')
, _ = require('underscore')
, util = require('util')
Expand Down
10 changes: 10 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ lastStepModifierFunctions.$unset = function (obj, field, value) {
};


/**
* Set a field on a new object
*/
lastStepModifierFunctions.$setOnInsert = function (obj, field, value) {
if (!obj._id) {
obj[field] = value;
}
};


/**
* Push an element to the end of an array field
* Optional modifier $each instead of value to push several values
Expand Down
2 changes: 1 addition & 1 deletion lib/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Persistence.ensureDirectoryExists = function (dir, cb) {
var callback = cb || function () {}
;

storage.mkdirp(dir, function (err) { return callback(err); });
storage.mkdirp(dir).then(function() { return callback(); }).catch(callback);
};


Expand Down
Loading