Comparing version 0.5.16 to 0.5.17
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -42,2 +46,3 @@ | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var randomId = arguments[2]; | ||
@@ -84,2 +89,4 @@ return this.db.indexManager.indexDocument(doc).then(function () { | ||
var multi = _ref2$multi === undefined ? false : _ref2$multi; | ||
var _ref2$upsert = _ref2.upsert; | ||
var upsert = _ref2$upsert === undefined ? false : _ref2$upsert; | ||
@@ -90,3 +97,3 @@ return this.find(query, { noClone: true }).sort(sort).then(function (docs) { | ||
} | ||
return new _DocumentModifier2.default(query).modify(docs, modifier); | ||
return new _DocumentModifier2.default(query).modify(docs, modifier, { upsert: upsert }); | ||
}).then(function (_ref3) { | ||
@@ -96,15 +103,31 @@ var original = _ref3.original; | ||
var updateStorgePromises = (0, _map3.default)(updated, function (d) { | ||
return _this3.db.storageManager.persist(d._id, d); | ||
}); | ||
var updateIndexPromises = (0, _map3.default)(updated, function (d, i) { | ||
return _this3.db.indexManager.reindexDocument(original[i], d); | ||
}); | ||
return Promise.all([].concat(_toConsumableArray(updateStorgePromises), _toConsumableArray(updateIndexPromises))).then(function () { | ||
return { | ||
modified: updated.length, | ||
original: original, | ||
updated: updated | ||
}; | ||
}); | ||
if (upsert && original.length && original[0] === null) { | ||
var _ret = function () { | ||
var newDoc = updated[0]; | ||
return { | ||
v: _this3.db.insert(newDoc, { quiet: true }).then(function (docId) { | ||
return { | ||
modified: 0, original: [], updated: [], | ||
inserted: _extends({ _id: docId }, newDoc) | ||
}; | ||
}) | ||
}; | ||
}(); | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
} else { | ||
var updateStorgePromises = (0, _map3.default)(updated, function (d) { | ||
return _this3.db.storageManager.persist(d._id, d); | ||
}); | ||
var updateIndexPromises = (0, _map3.default)(updated, function (d, i) { | ||
return _this3.db.indexManager.reindexDocument(original[i], d); | ||
}); | ||
return Promise.all([].concat(_toConsumableArray(updateStorgePromises), _toConsumableArray(updateIndexPromises))).then(function () { | ||
return { | ||
modified: updated.length, | ||
original: original, | ||
updated: updated | ||
}; | ||
}); | ||
} | ||
}); | ||
@@ -111,0 +134,0 @@ } |
@@ -12,2 +12,3 @@ 'use strict'; | ||
exports.isIndexable = isIndexable; | ||
exports.removeDollarOperators = removeDollarOperators; | ||
exports.isOperatorObject = isOperatorObject; | ||
@@ -66,2 +67,13 @@ exports.isNumericKey = isNumericKey; | ||
// Oddball function used by upsert. | ||
function removeDollarOperators(selector) { | ||
var selectorDoc = {}; | ||
for (var k in selector) { | ||
if (k.substr(0, 1) !== '$') { | ||
selectorDoc[k] = selector[k]; | ||
} | ||
} | ||
return selectorDoc; | ||
} | ||
// Returns true if this is an object with at least one key and all keys begin | ||
@@ -68,0 +80,0 @@ // with $. Unless inconsistentOK is set, throws if some keys begin with $ and |
@@ -52,2 +52,3 @@ 'use strict'; | ||
this._query = query; | ||
this._matcher = new _DocumentMatcher2.default(query); | ||
@@ -76,2 +77,9 @@ } | ||
if (!docs.length && options.upsert) { | ||
var newDoc = (0, _Document.removeDollarOperators)(this._query); | ||
newDoc = this._modifyDocument(newDoc, mod, { isInsert: true }); | ||
newResults.push(newDoc); | ||
oldResults.push(null); | ||
} | ||
return { | ||
@@ -78,0 +86,0 @@ updated: newResults, |
@@ -14,3 +14,3 @@ import _map from 'fast.js/map'; | ||
insert(doc, options = {}) { | ||
insert(doc, options = {}, randomId) { | ||
return this.db.indexManager.indexDocument(doc).then(() => | ||
@@ -42,3 +42,3 @@ this.db.storageManager.persist(doc._id, doc).then(() => | ||
update(query, modifier, {sort = {_id: 1}, multi = false}) { | ||
update(query, modifier, {sort = {_id: 1}, multi = false, upsert = false}) { | ||
return this.find(query, {noClone: true}) | ||
@@ -50,18 +50,26 @@ .sort(sort).then((docs) => { | ||
return new DocumentModifier(query) | ||
.modify(docs, modifier); | ||
.modify(docs, modifier, { upsert }); | ||
}).then(({original, updated}) => { | ||
const updateStorgePromises = _map(updated, d => | ||
this.db.storageManager.persist(d._id, d) | ||
); | ||
const updateIndexPromises = _map(updated, (d, i) => | ||
this.db.indexManager.reindexDocument(original[i], d) | ||
); | ||
return Promise.all([ | ||
...updateStorgePromises, | ||
...updateIndexPromises, | ||
]).then(() => ({ | ||
modified: updated.length, | ||
original: original, | ||
updated: updated, | ||
})); | ||
if (upsert && original.length && original[0] === null) { | ||
const newDoc = updated[0]; | ||
return this.db.insert(newDoc, {quiet: true}).then((docId) => ({ | ||
modified: 0, original: [], updated: [], | ||
inserted: {_id: docId, ...newDoc}, | ||
})); | ||
} else { | ||
const updateStorgePromises = _map(updated, d => | ||
this.db.storageManager.persist(d._id, d) | ||
); | ||
const updateIndexPromises = _map(updated, (d, i) => | ||
this.db.indexManager.reindexDocument(original[i], d) | ||
); | ||
return Promise.all([ | ||
...updateStorgePromises, | ||
...updateIndexPromises, | ||
]).then(() => ({ | ||
modified: updated.length, | ||
original: original, | ||
updated: updated, | ||
})); | ||
} | ||
}); | ||
@@ -68,0 +76,0 @@ } |
@@ -47,2 +47,13 @@ import _check from 'check-types'; | ||
// Oddball function used by upsert. | ||
export function removeDollarOperators(selector) { | ||
var selectorDoc = {}; | ||
for (var k in selector) { | ||
if (k.substr(0, 1) !== '$') { | ||
selectorDoc[k] = selector[k]; | ||
} | ||
} | ||
return selectorDoc; | ||
} | ||
// Returns true if this is an object with at least one key and all keys begin | ||
@@ -49,0 +60,0 @@ // with $. Unless inconsistentOK is set, throws if some keys begin with $ and |
@@ -9,3 +9,3 @@ import _check from 'check-types'; | ||
import {isPlainObject, isIndexable, isOperatorObject, | ||
isNumericKey, MongoTypeComp} from './Document'; | ||
isNumericKey, MongoTypeComp, removeDollarOperators} from './Document'; | ||
@@ -15,2 +15,3 @@ | ||
constructor(query = {}) { | ||
this._query = query; | ||
this._matcher = new DocumentMatcher(query); | ||
@@ -36,2 +37,9 @@ } | ||
if (!docs.length && options.upsert) { | ||
let newDoc = removeDollarOperators(this._query); | ||
newDoc = this._modifyDocument(newDoc, mod, {isInsert: true}); | ||
newResults.push(newDoc); | ||
oldResults.push(null); | ||
} | ||
return { | ||
@@ -38,0 +46,0 @@ updated: newResults, |
{ | ||
"name": "marsdb", | ||
"version": "0.5.16", | ||
"version": "0.5.17", | ||
"author": { | ||
@@ -40,6 +40,7 @@ "name": "Artem Artemev", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-preset-stage-0": "^6.3.13", | ||
"babel-register": "^6.3.13", | ||
"babelify": "^7.2.0", | ||
"brfs": "^1.4.1", | ||
"browserify": "^12.0.1", | ||
"browserify": "^13.0.0", | ||
"bulk-require": "^0.2.1", | ||
@@ -46,0 +47,0 @@ "bulkify": "^1.1.1", |
@@ -186,2 +186,11 @@ <div style="text-align:center"><img src="https://static.studytime.me/marsdb.png" /></div> | ||
}); | ||
// Upsert (insert when nothing found) | ||
posts.update( | ||
{authorId: "123"}, | ||
{$set: {text: 'noop'}}, | ||
{upsert: true} | ||
).then(result => { | ||
// { authorId: "123", text: 'noop', _id: '...' } | ||
}); | ||
``` | ||
@@ -198,4 +207,2 @@ ### Removing | ||
## Roadmap | ||
* Keep track of multiple remove/update documents in selector (allow only if opations.multi passed) | ||
* Upsert updating | ||
* Indexes support for some kind of simple requests {a: '^b'}, {a: {$lt: 9}} | ||
@@ -202,0 +209,0 @@ * Documentation |
@@ -368,2 +368,25 @@ import {Document} from '../../lib/Document'; | ||
it('should insert document if it is not exists with updasert', function () { | ||
const db = new Collection('test'); | ||
const testUpsert = (query, mod, expected) => { | ||
return db.remove({}, {multi: true}).then(() => { | ||
return db.update(query, mod, {upsert: true}).then((res) => { | ||
res.modified.should.be.equals(0); | ||
res.original.should.be.deep.equals([]); | ||
res.updated.should.be.deep.equals([]); | ||
expect(res.inserted._id).to.have.length(17); | ||
delete res.inserted._id; | ||
res.inserted.should.be.deep.equals(expected); | ||
}); | ||
}); | ||
} | ||
return Promise.all([ | ||
testUpsert({a: 2}, {$set: {b: 3}}, {a: 2, b: 3}), | ||
testUpsert({a: 2}, {b: 3}, {b: 3}), | ||
testUpsert({a: 2}, {$unset: {a: 1}}, {}), | ||
testUpsert({a: 2}, {$setOnInsert: {a: 1}}, {a: 1}), | ||
]); | ||
}); | ||
it('should update by primitive id type', function () { | ||
@@ -370,0 +393,0 @@ const db = new Collection('test'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
970176
22318
215
34