Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nedb

Package Overview
Dependencies
Maintainers
1
Versions
95
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nedb - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

58

lib/datastore.js

@@ -5,2 +5,3 @@ /**

* Queue operations
* Enable upserts
* Update and removes should only modify the corresponding part of the database

@@ -13,2 +14,3 @@ */

, model = require('./model')
, async = require('async')
;

@@ -112,7 +114,2 @@

//for (i = 0; i < queryKeys.length; i += 1) {
//k = queryKeys[i]
//if (obj[k] !== query[k]) { match = false; }
//}
Object.keys(query).forEach(function (k) {

@@ -212,3 +209,4 @@ if (obj[k] !== query[k]) { match = false; }

* options.multi If true, can update multiple documents (defaults to false)
* @param {Function} cb Optional callback, signature: err, numReplaced
* options.upsert If true, document is inserted if the query doesn't match anything
* @param {Function} cb Optional callback, signature: err, numReplaced, upsert (set to true if the update was in fact an upsert)
*/

@@ -219,3 +217,3 @@ Datastore.prototype.update = function (query, newDoc, options, cb) {

, numReplaced = 0
, multi
, multi, upsert
, newData = [];

@@ -226,17 +224,37 @@

multi = options.multi !== undefined ? options.multi : false;
upsert = options.upsert !== undefined ? options.upsert : false;
self.data.forEach(function (d) {
if (Datastore.match(d, query) && (multi || numReplaced === 0)) {
numReplaced += 1;
newData.push(Datastore.modify(d, newDoc));
} else {
newData.push(d);
}
});
async.waterfall([
function (cb) { // If upsert option is set, check whether we need to insert the doc
if (!upsert) { return cb(); }
self.persistWholeDatabase(newData, function (err) {
if (err) { return callback(err); }
self.data = newData;
return callback(null, numReplaced);
});
self.findOne(query, function (err, doc) {
if (err) { return callback(err); }
if (doc) {
return cb();
} else {
return self.insert(newDoc, function (err) {
if (err) { return callback(err); }
return callback(null, 1, true);
});
}
});
}
, function () { // Perform the update
self.data.forEach(function (d) {
if (Datastore.match(d, query) && (multi || numReplaced === 0)) {
numReplaced += 1;
newData.push(Datastore.modify(d, newDoc));
} else {
newData.push(d);
}
});
self.persistWholeDatabase(newData, function (err) {
if (err) { return callback(err); }
self.data = newData;
return callback(null, numReplaced);
});
}
]);
};

@@ -243,0 +261,0 @@

{
"name": "nedb",
"version": "0.0.3",
"version": "0.0.4",
"author": {

@@ -5,0 +5,0 @@ "name": "tldr.io",

@@ -358,2 +358,27 @@ var Datastore = require('../lib/datastore')

it('Can perform upserts if needed', function (done) {
d.update({ impossible: 'db is empty anyway' }, { newDoc: true }, {}, function (err, nr, upsert) {
assert.isNull(err);
nr.should.equal(0);
assert.isUndefined(upsert);
d.find({}, function (err, docs) {
docs.length.should.equal(0); // Default option for upsert is false
d.update({ impossible: 'db is empty anyway' }, { newDoc: true }, { upsert: true }, function (err, nr, upsert) {
assert.isNull(err);
nr.should.equal(1);
upsert.should.equal(true);
d.find({}, function (err, docs) {
docs.length.should.equal(1); // Default option for upsert is false
docs[0].newDoc.should.equal(true);
done();
});
});
});
});
});
}); // ==== End of 'Update' ==== //

@@ -360,0 +385,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc