remove underscore

pull/2/head
Timothée Rebours 4 years ago
parent 761059505e
commit 5c6561307d
  1. 1
      karma.conf.template.js
  2. 4
      lib/cursor.js
  3. 9
      lib/datastore.js
  4. 9
      lib/indexes.js
  5. 18
      lib/model.js
  6. 6
      lib/utils.js
  7. 6
      package-lock.json
  8. 3
      package.json
  9. 124
      test/browser/nedb-browser.spec.js
  10. 139
      test/cursor.test.js
  11. 329
      test/db.test.js
  12. 134
      test/indexes.test.js
  13. 107
      test/model.test.js
  14. 77
      test/persistence.test.js

@ -13,7 +13,6 @@ module.exports = (config) => ({
// list of files / patterns to load in the browser
files: [
'node_modules/underscore/underscore-min.js',
'node_modules/localforage/dist/localforage.min.js',
'node_modules/async/lib/async.js',
'browser-version/out/nedb.min.js',

@ -1,7 +1,6 @@
/**
* Manage access to data, be it to find, update or remove it
*/
const _ = require('underscore')
const model = require('./model.js')
class Cursor {
@ -65,7 +64,8 @@ class Cursor {
}
const keepId = this._projection._id !== 0
this._projection = _.omit(this._projection, '_id')
const { _id, ...rest } = this._projection
this._projection = rest
// Check for consistency
const keys = Object.keys(this._projection)

@ -1,7 +1,6 @@
const { EventEmitter } = require('events')
const util = require('util')
const async = require('async')
const _ = require('underscore')
const Cursor = require('./cursor.js')
const customUtils = require('./customUtils.js')
const Executor = require('./executor.js')
@ -268,7 +267,7 @@ class Datastore extends EventEmitter {
usableQueryKeys.push(k)
}
})
usableQueryKeys = _.intersection(usableQueryKeys, indexNames)
usableQueryKeys = usableQueryKeys.filter(k => indexNames.includes(k))
if (usableQueryKeys.length > 0) {
return cb(null, self.indexes[usableQueryKeys[0]].getMatching(query[usableQueryKeys[0]]))
}
@ -280,7 +279,7 @@ class Datastore extends EventEmitter {
usableQueryKeys.push(k)
}
})
usableQueryKeys = _.intersection(usableQueryKeys, indexNames)
usableQueryKeys = usableQueryKeys.filter(k => indexNames.includes(k))
if (usableQueryKeys.length > 0) {
return cb(null, self.indexes[usableQueryKeys[0]].getMatching(query[usableQueryKeys[0]].$in))
}
@ -292,7 +291,7 @@ class Datastore extends EventEmitter {
usableQueryKeys.push(k)
}
})
usableQueryKeys = _.intersection(usableQueryKeys, indexNames)
usableQueryKeys = usableQueryKeys.filter(k => indexNames.includes(k))
if (usableQueryKeys.length > 0) {
return cb(null, self.indexes[usableQueryKeys[0]].getBetweenBounds(query[usableQueryKeys[0]]))
}
@ -635,7 +634,7 @@ class Datastore extends EventEmitter {
}
// Update the datafile
const updatedDocs = _.pluck(modifications, 'newDoc')
const updatedDocs = modifications.map(x => x.newDoc)
self.persistence.persistNewState(updatedDocs, function (err) {
if (err) { return callback(err) }
if (!options.returnUpdatedDocs) {

@ -1,6 +1,7 @@
const _ = require('underscore')
const util = require('util')
const BinarySearchTree = require('@seald-io/binary-search-tree').BinarySearchTree
const model = require('./model.js')
const { uniq } = require('./utils.js')
/**
* Two indexed pointers are equal iif they point to the same place
@ -17,7 +18,7 @@ function projectForUnique (elt) {
if (typeof elt === 'string') { return '$string' + elt }
if (typeof elt === 'boolean') { return '$boolean' + elt }
if (typeof elt === 'number') { return '$number' + elt }
if (Array.isArray(elt)) { return '$date' + elt.getTime() }
if (util.types.isDate(elt)) { return '$date' + elt.getTime() } // TODO: there is an obvious error here,
return elt // Arrays and objects, will check for pointer equality
}
@ -77,7 +78,7 @@ class Index {
this.tree.insert(key, doc)
} else {
// If an insert fails due to a unique constraint, roll back all inserts before it
keys = _.uniq(key, projectForUnique)
keys = uniq(key, projectForUnique)
for (i = 0; i < keys.length; i += 1) {
try {
@ -150,7 +151,7 @@ class Index {
if (!Array.isArray(key)) {
this.tree.delete(key, doc)
} else {
_.uniq(key, projectForUnique).forEach(function (_key) {
uniq(key, projectForUnique).forEach(function (_key) {
self.tree.delete(_key, doc)
})
}

@ -5,7 +5,7 @@
* Querying, update
*/
const util = require('util')
const _ = require('underscore')
const { uniq } = require('./utils.js')
const modifierFunctions = {}
const lastStepModifierFunctions = {}
const comparisonFunctions = {}
@ -358,7 +358,7 @@ lastStepModifierFunctions.$inc = function (obj, field, value) {
if (typeof value !== 'number') { throw new Error(value + ' must be a number') }
if (typeof obj[field] !== 'number') {
if (!_.has(obj, field)) {
if (!Object.prototype.hasOwnProperty.call(obj, field)) {
obj[field] = value
} else {
throw new Error('Don\'t use the $inc modifier on non-number fields')
@ -417,8 +417,8 @@ Object.keys(lastStepModifierFunctions).forEach(function (modifier) {
*/
function modify (obj, updateQuery) {
const keys = Object.keys(updateQuery)
const firstChars = _.map(keys, function (item) { return item[0] })
const dollarFirstChars = _.filter(firstChars, function (c) { return c === '$' })
const firstChars = keys.map(item => item[0])
const dollarFirstChars = firstChars.filter(c => c === '$')
let newDoc
let modifiers
@ -434,7 +434,7 @@ function modify (obj, updateQuery) {
newDoc._id = obj._id
} else {
// Apply modifiers
modifiers = _.uniq(keys)
modifiers = uniq(keys)
newDoc = deepCopy(obj)
modifiers.forEach(function (m) {
if (!modifierFunctions[m]) { throw new Error('Unknown modifier ' + m) }
@ -689,10 +689,10 @@ logicalOperators.$not = function (obj, query) {
* @param {Query} query
*/
logicalOperators.$where = function (obj, fn) {
if (!_.isFunction(fn)) { throw new Error('$where operator used without a function') }
if (typeof fn !== 'function') { throw new Error('$where operator used without a function') }
const result = fn.call(obj)
if (!_.isBoolean(result)) { throw new Error('$where function must return boolean') }
if (typeof result !== 'boolean') { throw new Error('$where function must return boolean') }
return result
}
@ -768,8 +768,8 @@ function matchQueryPart (obj, queryKey, queryValue, treatObjAsValue) {
// or only normal fields. Mixed objects are not allowed
if (queryValue !== null && typeof queryValue === 'object' && !util.types.isRegExp(queryValue) && !Array.isArray(queryValue)) {
keys = Object.keys(queryValue)
firstChars = _.map(keys, function (item) { return item[0] })
dollarFirstChars = _.filter(firstChars, function (c) { return c === '$' })
firstChars = keys.map(item => item[0])
dollarFirstChars = firstChars.filter(c => c === '$')
if (dollarFirstChars.length !== 0 && dollarFirstChars.length !== firstChars.length) {
throw new Error('You cannot mix operators and normal fields')

@ -0,0 +1,6 @@
const uniq = (array, iterator) => {
if (iterator) return [...(new Map(array.map(x => [iterator(x), x]))).values()]
else return [...new Set(array)]
}
module.exports.uniq = uniq

6
package-lock.json generated

@ -1,17 +1,17 @@
{
"name": "nedb",
"name": "@seald-io/nedb",
"version": "2.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@seald-io/nedb",
"version": "2.0.0",
"license": "MIT",
"dependencies": {
"@seald-io/binary-search-tree": "^1.0.0",
"async": "0.2.10",
"localforage": "^1.9.0",
"underscore": "^1.13.1"
"localforage": "^1.9.0"
},
"devDependencies": {
"chai": "^4.3.4",

@ -36,8 +36,7 @@
"dependencies": {
"@seald-io/binary-search-tree": "^1.0.0",
"async": "0.2.10",
"localforage": "^1.9.0",
"underscore": "^1.13.1"
"localforage": "^1.9.0"
},
"devDependencies": {
"chai": "^4.3.4",

@ -1,5 +1,5 @@
/* eslint-env mocha */
/* global chai, _, Nedb */
/* global chai, Nedb */
/**
* Testing the browser version of NeDB
@ -14,15 +14,15 @@ const assert = chai.assert
* Given a docs array and an id, return the document whose id matches, or null if none is found
*/
function findById (docs, id) {
return _.find(docs, function (doc) { return doc._id === id }) || null
return docs.find(function (doc) { return doc._id === id }) || null
}
describe('Basic CRUD functionality', function () {
it('Able to create a database object in the browser', function () {
const db = new Nedb()
assert.equal(db.inMemoryOnly, true)
assert.equal(db.persistence.inMemoryOnly, true)
assert.strictEqual(db.inMemoryOnly, true)
assert.strictEqual(db.persistence.inMemoryOnly, true)
})
it('Insertion and querying', function (done) {
@ -36,18 +36,18 @@ describe('Basic CRUD functionality', function () {
assert.isNull(err)
db.find({ a: { $gt: 36 } }, function (err, docs) {
const doc2 = _.find(docs, function (doc) { return doc._id === newDoc2._id })
const doc3 = _.find(docs, function (doc) { return doc._id === newDoc3._id })
const doc2 = docs.find(function (doc) { return doc._id === newDoc2._id })
const doc3 = docs.find(function (doc) { return doc._id === newDoc3._id })
assert.isNull(err)
assert.equal(docs.length, 2)
assert.equal(doc2.a, 40)
assert.equal(doc3.a, 400)
assert.strictEqual(docs.length, 2)
assert.strictEqual(doc2.a, 40)
assert.strictEqual(doc3.a, 400)
db.find({ a: { $lt: 36 } }, function (err, docs) {
assert.isNull(err)
assert.equal(docs.length, 1)
assert.equal(docs[0].a, 4)
assert.strictEqual(docs.length, 1)
assert.strictEqual(docs[0].a, 4)
done()
})
})
@ -72,17 +72,17 @@ describe('Basic CRUD functionality', function () {
db.find({ planet: /ar/ }, function (err, docs) {
assert.isNull(err)
assert.equal(docs.length, 4)
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc1._id }).planet, 'Earth')
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc2._id }).planet, 'Mars')
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc4._id }).planet, 'Eaaaaaarth')
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc5._id }).planet, 'Maaaars')
assert.strictEqual(docs.length, 4)
assert.strictEqual(docs.find(doc => doc._id === newDoc1._id).planet, 'Earth')
assert.strictEqual(docs.find(doc => doc._id === newDoc2._id).planet, 'Mars')
assert.strictEqual(docs.find(doc => doc._id === newDoc4._id).planet, 'Eaaaaaarth')
assert.strictEqual(docs.find(doc => doc._id === newDoc5._id).planet, 'Maaaars')
db.find({ planet: /aa+r/ }, function (err, docs) {
assert.isNull(err)
assert.equal(docs.length, 2)
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc4._id }).planet, 'Eaaaaaarth')
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc5._id }).planet, 'Maaaars')
assert.strictEqual(docs.length, 2)
assert.strictEqual(docs.find(doc => doc._id === newDoc4._id).planet, 'Eaaaaaarth')
assert.strictEqual(docs.find(doc => doc._id === newDoc5._id).planet, 'Maaaars')
done()
})
@ -104,48 +104,48 @@ describe('Basic CRUD functionality', function () {
// Simple update
db.update({ _id: newDoc2._id }, { $set: { planet: 'Saturn' } }, {}, function (err, nr) {
assert.isNull(err)
assert.equal(nr, 1)
assert.strictEqual(nr, 1)
// eslint-disable-next-line node/handle-callback-err
db.find({}, function (err, docs) {
assert.equal(docs.length, 2)
assert.equal(findById(docs, newDoc1._id).planet, 'Eaaaaarth')
assert.equal(findById(docs, newDoc2._id).planet, 'Saturn')
assert.strictEqual(docs.length, 2)
assert.strictEqual(findById(docs, newDoc1._id).planet, 'Eaaaaarth')
assert.strictEqual(findById(docs, newDoc2._id).planet, 'Saturn')
// Failing update
db.update({ _id: 'unknown' }, { $inc: { count: 1 } }, {}, function (err, nr) {
assert.isNull(err)
assert.equal(nr, 0)
assert.strictEqual(nr, 0)
// eslint-disable-next-line node/handle-callback-err
db.find({}, function (err, docs) {
assert.equal(docs.length, 2)
assert.equal(findById(docs, newDoc1._id).planet, 'Eaaaaarth')
assert.equal(findById(docs, newDoc2._id).planet, 'Saturn')
assert.strictEqual(docs.length, 2)
assert.strictEqual(findById(docs, newDoc1._id).planet, 'Eaaaaarth')
assert.strictEqual(findById(docs, newDoc2._id).planet, 'Saturn')
// Document replacement
db.update({ planet: 'Eaaaaarth' }, { planet: 'Uranus' }, { multi: false }, function (err, nr) {
assert.isNull(err)
assert.equal(nr, 1)
assert.strictEqual(nr, 1)
// eslint-disable-next-line node/handle-callback-err
db.find({}, function (err, docs) {
assert.equal(docs.length, 2)
assert.equal(findById(docs, newDoc1._id).planet, 'Uranus')
assert.equal(findById(docs, newDoc2._id).planet, 'Saturn')
assert.strictEqual(docs.length, 2)
assert.strictEqual(findById(docs, newDoc1._id).planet, 'Uranus')
assert.strictEqual(findById(docs, newDoc2._id).planet, 'Saturn')
// Multi update
db.update({}, { $inc: { count: 3 } }, { multi: true }, function (err, nr) {
assert.isNull(err)
assert.equal(nr, 2)
assert.strictEqual(nr, 2)
// eslint-disable-next-line node/handle-callback-err
db.find({}, function (err, docs) {
assert.equal(docs.length, 2)
assert.equal(findById(docs, newDoc1._id).planet, 'Uranus')
assert.equal(findById(docs, newDoc1._id).count, 3)
assert.equal(findById(docs, newDoc2._id).planet, 'Saturn')
assert.equal(findById(docs, newDoc2._id).count, 3)
assert.strictEqual(docs.length, 2)
assert.strictEqual(findById(docs, newDoc1._id).planet, 'Uranus')
assert.strictEqual(findById(docs, newDoc1._id).count, 3)
assert.strictEqual(findById(docs, newDoc2._id).planet, 'Saturn')
assert.strictEqual(findById(docs, newDoc2._id).count, 3)
done()
})
@ -168,19 +168,19 @@ describe('Basic CRUD functionality', function () {
// Pushing to an array
db.update({}, { $push: { satellites: 'Phobos' } }, {}, function (err, nr) {
assert.isNull(err)
assert.equal(nr, 1)
assert.strictEqual(nr, 1)
// eslint-disable-next-line node/handle-callback-err
db.findOne({}, function (err, doc) {
assert.deepEqual(doc, { planet: 'Earth', _id: newDoc1._id, satellites: ['Phobos'] })
assert.deepStrictEqual(doc, { planet: 'Earth', _id: newDoc1._id, satellites: ['Phobos'] })
db.update({}, { $push: { satellites: 'Deimos' } }, {}, function (err, nr) {
assert.isNull(err)
assert.equal(nr, 1)
assert.strictEqual(nr, 1)
// eslint-disable-next-line node/handle-callback-err
db.findOne({}, function (err, doc) {
assert.deepEqual(doc, { planet: 'Earth', _id: newDoc1._id, satellites: ['Phobos', 'Deimos'] })
assert.deepStrictEqual(doc, { planet: 'Earth', _id: newDoc1._id, satellites: ['Phobos', 'Deimos'] })
done()
})
@ -196,15 +196,15 @@ describe('Basic CRUD functionality', function () {
db.update({ a: 4 }, { $inc: { b: 1 } }, { upsert: true }, function (err, nr, upsert) {
assert.isNull(err)
// Return upserted document
assert.equal(upsert.a, 4)
assert.equal(upsert.b, 1)
assert.equal(nr, 1)
assert.strictEqual(upsert.a, 4)
assert.strictEqual(upsert.b, 1)
assert.strictEqual(nr, 1)
// eslint-disable-next-line node/handle-callback-err
db.find({}, function (err, docs) {
assert.equal(docs.length, 1)
assert.equal(docs[0].a, 4)
assert.equal(docs[0].b, 1)
assert.strictEqual(docs.length, 1)
assert.strictEqual(docs[0].a, 4)
assert.strictEqual(docs[0].b, 1)
done()
})
@ -221,31 +221,31 @@ describe('Basic CRUD functionality', function () {
// Multi remove
db.remove({ a: { $in: [5, 7] } }, { multi: true }, function (err, nr) {
assert.isNull(err)
assert.equal(nr, 2)
assert.strictEqual(nr, 2)
// eslint-disable-next-line node/handle-callback-err
db.find({}, function (err, docs) {
assert.equal(docs.length, 1)
assert.equal(docs[0].a, 2)
assert.strictEqual(docs.length, 1)
assert.strictEqual(docs[0].a, 2)
// Remove with no match
db.remove({ b: { $exists: true } }, { multi: true }, function (err, nr) {
assert.isNull(err)
assert.equal(nr, 0)
assert.strictEqual(nr, 0)
// eslint-disable-next-line node/handle-callback-err
db.find({}, function (err, docs) {
assert.equal(docs.length, 1)
assert.equal(docs[0].a, 2)
assert.strictEqual(docs.length, 1)
assert.strictEqual(docs[0].a, 2)
// Simple remove
db.remove({ a: { $exists: true } }, { multi: true }, function (err, nr) {
assert.isNull(err)
assert.equal(nr, 1)
assert.strictEqual(nr, 1)
// eslint-disable-next-line node/handle-callback-err
db.find({}, function (err, docs) {
assert.equal(docs.length, 0)
assert.strictEqual(docs.length, 0)
done()
})
@ -266,17 +266,17 @@ describe('Indexing', function () {
db.insert({ a: 7 }, function () {
// eslint-disable-next-line node/handle-callback-err
db.getCandidates({ a: 6 }, function (err, candidates) {
assert.equal(candidates.length, 3)
assert.isDefined(_.find(candidates, function (doc) { return doc.a === 4 }))
assert.isDefined(_.find(candidates, function (doc) { return doc.a === 6 }))
assert.isDefined(_.find(candidates, function (doc) { return doc.a === 7 }))
assert.strictEqual(candidates.length, 3)
assert.isDefined(candidates.find(function (doc) { return doc.a === 4 }))
assert.isDefined(candidates.find(function (doc) { return doc.a === 6 }))
assert.isDefined(candidates.find(function (doc) { return doc.a === 7 }))
db.ensureIndex({ fieldName: 'a' })
// eslint-disable-next-line node/handle-callback-err
db.getCandidates({ a: 6 }, function (err, candidates) {
assert.equal(candidates.length, 1)
assert.isDefined(_.find(candidates, function (doc) { return doc.a === 6 }))
assert.strictEqual(candidates.length, 1)
assert.isDefined(candidates.find(function (doc) { return doc.a === 6 }))
done()
})
@ -298,7 +298,7 @@ describe('Indexing', function () {
assert.isNull(err)
db.insert({ u: 5 }, function (err) {
assert.equal(err.errorType, 'uniqueViolated')
assert.strictEqual(err.errorType, 'uniqueViolated')
done()
})

@ -3,7 +3,6 @@ const chai = require('chai')
const testDb = 'workspace/test.db'
const fs = require('fs')
const path = require('path')
const _ = require('underscore')
const async = require('async')
const Datastore = require('../lib/datastore')
const Persistence = require('../lib/persistence')
@ -67,11 +66,11 @@ describe('Cursor', function () {
cursor.exec(function (err, docs) {
assert.isNull(err)
docs.length.should.equal(5)
_.filter(docs, function (doc) { return doc.age === 5 })[0].age.should.equal(5)
_.filter(docs, function (doc) { return doc.age === 57 })[0].age.should.equal(57)
_.filter(docs, function (doc) { return doc.age === 52 })[0].age.should.equal(52)
_.filter(docs, function (doc) { return doc.age === 23 })[0].age.should.equal(23)
_.filter(docs, function (doc) { return doc.age === 89 })[0].age.should.equal(89)
docs.filter(function (doc) { return doc.age === 5 })[0].age.should.equal(5)
docs.filter(function (doc) { return doc.age === 57 })[0].age.should.equal(57)
docs.filter(function (doc) { return doc.age === 52 })[0].age.should.equal(52)
docs.filter(function (doc) { return doc.age === 23 })[0].age.should.equal(23)
docs.filter(function (doc) { return doc.age === 89 })[0].age.should.equal(89)
cb()
})
},
@ -80,11 +79,11 @@ describe('Cursor', function () {
cursor.exec(function (err, docs) {
assert.isNull(err)
docs.length.should.equal(5)
_.filter(docs, function (doc) { return doc.age === 5 })[0].age.should.equal(5)
_.filter(docs, function (doc) { return doc.age === 57 })[0].age.should.equal(57)
_.filter(docs, function (doc) { return doc.age === 52 })[0].age.should.equal(52)
_.filter(docs, function (doc) { return doc.age === 23 })[0].age.should.equal(23)
_.filter(docs, function (doc) { return doc.age === 89 })[0].age.should.equal(89)
docs.filter(function (doc) { return doc.age === 5 })[0].age.should.equal(5)
docs.filter(function (doc) { return doc.age === 57 })[0].age.should.equal(57)
docs.filter(function (doc) { return doc.age === 52 })[0].age.should.equal(52)
docs.filter(function (doc) { return doc.age === 23 })[0].age.should.equal(23)
docs.filter(function (doc) { return doc.age === 89 })[0].age.should.equal(89)
cb()
})
},
@ -93,9 +92,9 @@ describe('Cursor', function () {
cursor.exec(function (err, docs) {
assert.isNull(err)
docs.length.should.equal(3)
_.filter(docs, function (doc) { return doc.age === 57 })[0].age.should.equal(57)
_.filter(docs, function (doc) { return doc.age === 52 })[0].age.should.equal(52)
_.filter(docs, function (doc) { return doc.age === 89 })[0].age.should.equal(89)
docs.filter(function (doc) { return doc.age === 57 })[0].age.should.equal(57)
docs.filter(function (doc) { return doc.age === 52 })[0].age.should.equal(52)
docs.filter(function (doc) { return doc.age === 89 })[0].age.should.equal(89)
cb()
})
}
@ -208,16 +207,16 @@ describe('Cursor', function () {
// eslint-disable-next-line node/handle-callback-err
db.find({}).sort({ name: 1 }).exec(function (err, docs) {
_.pluck(docs, 'name')[0].should.equal('zulu')
_.pluck(docs, 'name')[1].should.equal('alpha')
_.pluck(docs, 'name')[2].should.equal('charlie')
docs.map(x => x.name)[0].should.equal('zulu')
docs.map(x => x.name)[1].should.equal('alpha')
docs.map(x => x.name)[2].should.equal('charlie')
delete db.compareStrings
// eslint-disable-next-line node/handle-callback-err
db.find({}).sort({ name: 1 }).exec(function (err, docs) {
_.pluck(docs, 'name')[0].should.equal('alpha')
_.pluck(docs, 'name')[1].should.equal('charlie')
_.pluck(docs, 'name')[2].should.equal('zulu')
docs.map(x => x.name)[0].should.equal('alpha')
docs.map(x => x.name)[1].should.equal('charlie')
docs.map(x => x.name)[2].should.equal('zulu')
done()
})
@ -740,21 +739,21 @@ describe('Cursor', function () {
cursor.exec(function (err, docs) {
assert.isNull(err)
docs.length.should.equal(5)
assert.deepEqual(docs[0], doc0)
assert.deepEqual(docs[1], doc3)
assert.deepEqual(docs[2], doc2)
assert.deepEqual(docs[3], doc1)
assert.deepEqual(docs[4], doc4)
assert.deepStrictEqual(docs[0], doc0)
assert.deepStrictEqual(docs[1], doc3)
assert.deepStrictEqual(docs[2], doc2)
assert.deepStrictEqual(docs[3], doc1)
assert.deepStrictEqual(docs[4], doc4)
cursor.projection({})
cursor.exec(function (err, docs) {
assert.isNull(err)
docs.length.should.equal(5)
assert.deepEqual(docs[0], doc0)
assert.deepEqual(docs[1], doc3)
assert.deepEqual(docs[2], doc2)
assert.deepEqual(docs[3], doc1)
assert.deepEqual(docs[4], doc4)
assert.deepStrictEqual(docs[0], doc0)
assert.deepStrictEqual(docs[1], doc3)
assert.deepStrictEqual(docs[2], doc2)
assert.deepStrictEqual(docs[3], doc1)
assert.deepStrictEqual(docs[4], doc4)
done()
})
@ -769,21 +768,21 @@ describe('Cursor', function () {
assert.isNull(err)
docs.length.should.equal(5)
// Takes the _id by default
assert.deepEqual(docs[0], { age: 5, name: 'Jo', _id: doc0._id })
assert.deepEqual(docs[1], { age: 23, name: 'LM', _id: doc3._id })
assert.deepEqual(docs[2], { age: 52, name: 'Grafitti', _id: doc2._id })
assert.deepEqual(docs[3], { age: 57, name: 'Louis', _id: doc1._id })
assert.deepEqual(docs[4], { age: 89, _id: doc4._id }) // No problems if one field to take doesn't exist
assert.deepStrictEqual(docs[0], { age: 5, name: 'Jo', _id: doc0._id })
assert.deepStrictEqual(docs[1], { age: 23, name: 'LM', _id: doc3._id })
assert.deepStrictEqual(docs[2], { age: 52, name: 'Grafitti', _id: doc2._id })
assert.deepStrictEqual(docs[3], { age: 57, name: 'Louis', _id: doc1._id })
assert.deepStrictEqual(docs[4], { age: 89, _id: doc4._id }) // No problems if one field to take doesn't exist
cursor.projection({ age: 1, name: 1, _id: 0 })
cursor.exec(function (err, docs) {
assert.isNull(err)
docs.length.should.equal(5)
assert.deepEqual(docs[0], { age: 5, name: 'Jo' })
assert.deepEqual(docs[1], { age: 23, name: 'LM' })
assert.deepEqual(docs[2], { age: 52, name: 'Grafitti' })
assert.deepEqual(docs[3], { age: 57, name: 'Louis' })
assert.deepEqual(docs[4], { age: 89 }) // No problems if one field to take doesn't exist
assert.deepStrictEqual(docs[0], { age: 5, name: 'Jo' })
assert.deepStrictEqual(docs[1], { age: 23, name: 'LM' })
assert.deepStrictEqual(docs[2], { age: 52, name: 'Grafitti' })
assert.deepStrictEqual(docs[3], { age: 57, name: 'Louis' })
assert.deepStrictEqual(docs[4], { age: 89 }) // No problems if one field to take doesn't exist
done()
})
@ -798,21 +797,21 @@ describe('Cursor', function () {
assert.isNull(err)
docs.length.should.equal(5)
// Takes the _id by default
assert.deepEqual(docs[0], { planet: 'B', _id: doc0._id, toys: { bebe: true, ballon: 'much' } })
assert.deepEqual(docs[1], { planet: 'S', _id: doc3._id })
assert.deepEqual(docs[2], { planet: 'C', _id: doc2._id, toys: { bebe: 'kind of' } })
assert.deepEqual(docs[3], { planet: 'R', _id: doc1._id, toys: { bebe: false, ballon: 'yeah' } })
assert.deepEqual(docs[4], { planet: 'Earth', _id: doc4._id })
assert.deepStrictEqual(docs[0], { planet: 'B', _id: doc0._id, toys: { bebe: true, ballon: 'much' } })
assert.deepStrictEqual(docs[1], { planet: 'S', _id: doc3._id })
assert.deepStrictEqual(docs[2], { planet: 'C', _id: doc2._id, toys: { bebe: 'kind of' } })
assert.deepStrictEqual(docs[3], { planet: 'R', _id: doc1._id, toys: { bebe: false, ballon: 'yeah' } })
assert.deepStrictEqual(docs[4], { planet: 'Earth', _id: doc4._id })
cursor.projection({ age: 0, name: 0, _id: 0 })
cursor.exec(function (err, docs) {
assert.isNull(err)
docs.length.should.equal(5)
assert.deepEqual(docs[0], { planet: 'B', toys: { bebe: true, ballon: 'much' } })
assert.deepEqual(docs[1], { planet: 'S' })
assert.deepEqual(docs[2], { planet: 'C', toys: { bebe: 'kind of' } })
assert.deepEqual(docs[3], { planet: 'R', toys: { bebe: false, ballon: 'yeah' } })
assert.deepEqual(docs[4], { planet: 'Earth' })
assert.deepStrictEqual(docs[0], { planet: 'B', toys: { bebe: true, ballon: 'much' } })
assert.deepStrictEqual(docs[1], { planet: 'S' })
assert.deepStrictEqual(docs[2], { planet: 'C', toys: { bebe: 'kind of' } })
assert.deepStrictEqual(docs[3], { planet: 'R', toys: { bebe: false, ballon: 'yeah' } })
assert.deepStrictEqual(docs[4], { planet: 'Earth' })
done()
})
@ -830,20 +829,20 @@ describe('Cursor', function () {
cursor.projection({ age: 1, _id: 0 })
cursor.exec(function (err, docs) {
assert.isNull(err)
assert.deepEqual(docs[0], { age: 5 })
assert.deepEqual(docs[1], { age: 23 })
assert.deepEqual(docs[2], { age: 52 })
assert.deepEqual(docs[3], { age: 57 })
assert.deepEqual(docs[4], { age: 89 })
assert.deepStrictEqual(docs[0], { age: 5 })
assert.deepStrictEqual(docs[1], { age: 23 })
assert.deepStrictEqual(docs[2], { age: 52 })
assert.deepStrictEqual(docs[3], { age: 57 })
assert.deepStrictEqual(docs[4], { age: 89 })
cursor.projection({ age: 0, toys: 0, planet: 0, _id: 1 })
cursor.exec(function (err, docs) {
assert.isNull(err)
assert.deepEqual(docs[0], { name: 'Jo', _id: doc0._id })
assert.deepEqual(docs[1], { name: 'LM', _id: doc3._id })
assert.deepEqual(docs[2], { name: 'Grafitti', _id: doc2._id })
assert.deepEqual(docs[3], { name: 'Louis', _id: doc1._id })
assert.deepEqual(docs[4], { _id: doc4._id })
assert.deepStrictEqual(docs[0], { name: 'Jo', _id: doc0._id })
assert.deepStrictEqual(docs[1], { name: 'LM', _id: doc3._id })
assert.deepStrictEqual(docs[2], { name: 'Grafitti', _id: doc2._id })
assert.deepStrictEqual(docs[3], { name: 'Louis', _id: doc1._id })
assert.deepStrictEqual(docs[4], { _id: doc4._id })
done()
})
@ -857,11 +856,11 @@ describe('Cursor', function () {
cursor.projection({ name: 0, planet: 0, 'toys.bebe': 0, _id: 0 })
// eslint-disable-next-line node/handle-callback-err
cursor.exec(function (err, docs) {
assert.deepEqual(docs[0], { age: 5, toys: { ballon: 'much' } })
assert.deepEqual(docs[1], { age: 23 })
assert.deepEqual(docs[2], { age: 52, toys: {} })
assert.deepEqual(docs[3], { age: 57, toys: { ballon: 'yeah' } })
assert.deepEqual(docs[4], { age: 89 })
assert.deepStrictEqual(docs[0], { age: 5, toys: { ballon: 'much' } })
assert.deepStrictEqual(docs[1], { age: 23 })
assert.deepStrictEqual(docs[2], { age: 52, toys: {} })
assert.deepStrictEqual(docs[3], { age: 57, toys: { ballon: 'yeah' } })
assert.deepStrictEqual(docs[4], { age: 89 })
done()
})
@ -873,11 +872,11 @@ describe('Cursor', function () {
cursor.projection({ name: 1, 'toys.ballon': 1, _id: 0 })
// eslint-disable-next-line node/handle-callback-err
cursor.exec(function (err, docs) {
assert.deepEqual(docs[0], { name: 'Jo', toys: { ballon: 'much' } })
assert.deepEqual(docs[1], { name: 'LM' })
assert.deepEqual(docs[2], { name: 'Grafitti' })
assert.deepEqual(docs[3], { name: 'Louis', toys: { ballon: 'yeah' } })
assert.deepEqual(docs[4], {})
assert.deepStrictEqual(docs[0], { name: 'Jo', toys: { ballon: 'much' } })
assert.deepStrictEqual(docs[1], { name: 'LM' })
assert.deepStrictEqual(docs[2], { name: 'Grafitti' })
assert.deepStrictEqual(docs[3], { name: 'Louis', toys: { ballon: 'yeah' } })
assert.deepStrictEqual(docs[4], {})
done()
})

@ -3,7 +3,6 @@ const chai = require('chai')
const testDb = 'workspace/test.db'
const fs = require('fs')
const path = require('path')
const _ = require('underscore')
const async = require('async')
const model = require('../lib/model')
const Datastore = require('../lib/datastore')
@ -147,9 +146,9 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
docs.length.should.equal(3)
_.pluck(docs, 'somedata').should.contain('ok')
_.pluck(docs, 'somedata').should.contain('another')
_.pluck(docs, 'somedata').should.contain('again')
docs.map(x => x.somedata).should.contain('ok')
docs.map(x => x.somedata).should.contain('another')
docs.map(x => x.somedata).should.contain('again')
done()
})
})
@ -249,11 +248,11 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
docs.length.should.equal(2)
_.find(docs, function (doc) { return doc.a === 5 }).b.should.equal('hello')
_.find(docs, function (doc) { return doc.a === 42 }).b.should.equal('world')
docs.find(function (doc) { return doc.a === 5 }).b.should.equal('hello')
docs.find(function (doc) { return doc.a === 42 }).b.should.equal('world')
// The data has been persisted correctly
const data = _.filter(fs.readFileSync(testDb, 'utf8').split('\n'), function (line) { return line.length > 0 })
const data = fs.readFileSync(testDb, 'utf8').split('\n').filter(function (line) { return line.length > 0 })
data.length.should.equal(2)
model.deserialize(data[0]).a.should.equal(5)
model.deserialize(data[0]).b.should.equal('hello')
@ -276,7 +275,7 @@ describe('Database', function () {
d.find({}, function (err, docs) {
// Datafile only contains index definition
const datafileContents = model.deserialize(fs.readFileSync(testDb, 'utf8'))
assert.deepEqual(datafileContents, { $$indexCreated: { fieldName: 'a', unique: true } })
assert.deepStrictEqual(datafileContents, { $$indexCreated: { fieldName: 'a', unique: true } })
docs.length.should.equal(0)
@ -296,7 +295,7 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.insert(newDoc, function (err, insertedDoc) {
// No side effect on given input
assert.deepEqual(newDoc, { hello: 'world' })
assert.deepStrictEqual(newDoc, { hello: 'world' })
// Insert doc has two new fields, _id and createdAt
insertedDoc.hello.should.equal('world')
assert.isDefined(insertedDoc.createdAt)
@ -313,8 +312,8 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
docs.length.should.equal(1)
assert.deepEqual(newDoc, { hello: 'world' })
assert.deepEqual({
assert.deepStrictEqual(newDoc, { hello: 'world' })
assert.deepStrictEqual({
hello: 'world',
_id: insertedDoc._id,
createdAt: insertedDoc.createdAt,
@ -326,8 +325,8 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
docs.length.should.equal(1)
assert.deepEqual(newDoc, { hello: 'world' })
assert.deepEqual({
assert.deepStrictEqual(newDoc, { hello: 'world' })
assert.deepStrictEqual({
hello: 'world',
_id: insertedDoc._id,
createdAt: insertedDoc.createdAt,
@ -352,7 +351,7 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
docs.length.should.equal(1)
assert.deepEqual(docs[0], insertedDoc)
assert.deepStrictEqual(docs[0], insertedDoc)
done()
})
@ -370,12 +369,12 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
assert.deepEqual(insertedDoc, docs[0])
assert.deepStrictEqual(insertedDoc, docs[0])
d.loadDatabase(function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
assert.deepEqual(insertedDoc, docs[0])
assert.deepStrictEqual(insertedDoc, docs[0])
done()
})
@ -395,12 +394,12 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
assert.deepEqual(insertedDoc, docs[0])
assert.deepStrictEqual(insertedDoc, docs[0])
d.loadDatabase(function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
assert.deepEqual(insertedDoc, docs[0])
assert.deepStrictEqual(insertedDoc, docs[0])
done()
})
@ -473,12 +472,12 @@ describe('Database', function () {
d.insert({ tf: 9 }, function () {
// eslint-disable-next-line node/handle-callback-err
d.getCandidates({ r: 6, tf: 4 }, function (err, data) {
const doc1 = _.find(data, function (d) { return d._id === _doc1._id })
const doc2 = _.find(data, function (d) { return d._id === _doc2._id })
const doc1 = data.find(function (d) { return d._id === _doc1._id })
const doc2 = data.find(function (d) { return d._id === _doc2._id })
data.length.should.equal(2)
assert.deepEqual(doc1, { _id: doc1._id, tf: 4 })
assert.deepEqual(doc2, { _id: doc2._id, tf: 4, an: 'other' })
assert.deepStrictEqual(doc1, { _id: doc1._id, tf: 4 })
assert.deepStrictEqual(doc2, { _id: doc2._id, tf: 4, an: 'other' })
done()
})
@ -502,12 +501,12 @@ describe('Database', function () {
d.insert({ tf: 9 }, function (err, _doc2) {
// eslint-disable-next-line node/handle-callback-err
d.getCandidates({ r: 6, tf: { $in: [6, 9, 5] } }, function (err, data) {
const doc1 = _.find(data, function (d) { return d._id === _doc1._id })
const doc2 = _.find(data, function (d) { return d._id === _doc2._id })
const doc1 = data.find(function (d) { return d._id === _doc1._id })
const doc2 = data.find(function (d) { return d._id === _doc2._id })
data.length.should.equal(2)
assert.deepEqual(doc1, { _id: doc1._id, tf: 6 })
assert.deepEqual(doc2, { _id: doc2._id, tf: 9 })
assert.deepStrictEqual(doc1, { _id: doc1._id, tf: 6 })
assert.deepStrictEqual(doc2, { _id: doc2._id, tf: 9 })
done()
})
@ -531,16 +530,16 @@ describe('Database', function () {
d.insert({ tf: 9 }, function (err, _doc4) {
// eslint-disable-next-line node/handle-callback-err
d.getCandidates({ r: 6, notf: { $in: [6, 9, 5] } }, function (err, data) {
const doc1 = _.find(data, function (d) { return d._id === _doc1._id })
const doc2 = _.find(data, function (d) { return d._id === _doc2._id })
const doc3 = _.find(data, function (d) { return d._id === _doc3._id })
const doc4 = _.find(data, function (d) { return d._id === _doc4._id })
const doc1 = data.find(function (d) { return d._id === _doc1._id })
const doc2 = data.find(function (d) { return d._id === _doc2._id })
const doc3 = data.find(function (d) { return d._id === _doc3._id })
const doc4 = data.find(function (d) { return d._id === _doc4._id })
data.length.should.equal(4)
assert.deepEqual(doc1, { _id: doc1._id, tf: 4 })
assert.deepEqual(doc2, { _id: doc2._id, tf: 6 })
assert.deepEqual(doc3, { _id: doc3._id, tf: 4, an: 'other' })
assert.deepEqual(doc4, { _id: doc4._id, tf: 9 })
assert.deepStrictEqual(doc1, { _id: doc1._id, tf: 4 })
assert.deepStrictEqual(doc2, { _id: doc2._id, tf: 6 })
assert.deepStrictEqual(doc3, { _id: doc3._id, tf: 4, an: 'other' })
assert.deepStrictEqual(doc4, { _id: doc4._id, tf: 9 })
done()
})
@ -564,12 +563,12 @@ describe('Database', function () {
d.insert({ tf: 9 }, function (err, _doc4) {
// eslint-disable-next-line node/handle-callback-err
d.getCandidates({ r: 6, tf: { $lte: 9, $gte: 6 } }, function (err, data) {
const doc2 = _.find(data, function (d) { return d._id === _doc2._id })
const doc4 = _.find(data, function (d) { return d._id === _doc4._id })
const doc2 = data.find(function (d) { return d._id === _doc2._id })
const doc4 = data.find(function (d) { return d._id === _doc4._id })
data.length.should.equal(2)
assert.deepEqual(doc2, { _id: doc2._id, tf: 6 })
assert.deepEqual(doc4, { _id: doc4._id, tf: 9 })
assert.deepStrictEqual(doc2, { _id: doc2._id, tf: 6 })
assert.deepStrictEqual(doc4, { _id: doc4._id, tf: 9 })
done()
})
@ -698,10 +697,10 @@ describe('Database', function () {
d.find({}, function (err, docs) {
assert.isNull(err)
docs.length.should.equal(3)
_.pluck(docs, 'somedata').should.contain('ok')
_.pluck(docs, 'somedata').should.contain('another')
_.find(docs, function (d) { return d.somedata === 'another' }).plus.should.equal('additional data')
_.pluck(docs, 'somedata').should.contain('again')
docs.map(x => x.somedata).should.contain('ok')
docs.map(x => x.somedata).should.contain('another')
docs.find(function (d) { return d.somedata === 'another' }).plus.should.equal('additional data')
docs.map(x => x.somedata).should.contain('again')
return cb()
})
}
@ -723,7 +722,7 @@ describe('Database', function () {
d.find({ somedata: 'again' }, function (err, docs) {
assert.isNull(err)
docs.length.should.equal(2)
_.pluck(docs, 'somedata').should.not.contain('ok')
docs.map(x => x.somedata).should.not.contain('ok')
return cb()
})
},
@ -827,14 +826,14 @@ describe('Database', function () {
d.find({ fruits: 'pear' }, function (err, docs) {
assert.isNull(err)
docs.length.should.equal(2)
_.pluck(docs, '_id').should.contain(doc1._id)
_.pluck(docs, '_id').should.contain(doc2._id)
docs.map(x => x._id).should.contain(doc1._id)
docs.map(x => x._id).should.contain(doc2._id)
d.find({ fruits: 'banana' }, function (err, docs) {
assert.isNull(err)
docs.length.should.equal(2)
_.pluck(docs, '_id').should.contain(doc1._id)
_.pluck(docs, '_id').should.contain(doc3._id)
docs.map(x => x._id).should.contain(doc1._id)
docs.map(x => x._id).should.contain(doc3._id)
d.find({ fruits: 'doesntexist' }, function (err, docs) {
assert.isNull(err)
@ -953,12 +952,12 @@ describe('Database', function () {
d.find({ a: 2 }, { a: 0, _id: 0 }, function (err, docs) {
assert.isNull(err)
docs.length.should.equal(1)
assert.deepEqual(docs[0], { hello: 'world' })
assert.deepStrictEqual(docs[0], { hello: 'world' })
d.find({ a: 2 }, { a: 0, _id: 0 }).exec(function (err, docs) {
assert.isNull(err)
docs.length.should.equal(1)
assert.deepEqual(docs[0], { hello: 'world' })
assert.deepStrictEqual(docs[0], { hello: 'world' })
// Can't use both modes at once if not _id
d.find({ a: 2 }, { a: 0, hello: 1 }, function (err, docs) {
@ -985,11 +984,11 @@ describe('Database', function () {
d.insert({ a: 24, hello: 'earth' }, function (err, doc1) {
d.findOne({ a: 2 }, { a: 0, _id: 0 }, function (err, doc) {
assert.isNull(err)
assert.deepEqual(doc, { hello: 'world' })
assert.deepStrictEqual(doc, { hello: 'world' })
d.findOne({ a: 2 }, { a: 0, _id: 0 }).exec(function (err, doc) {
assert.isNull(err)
assert.deepEqual(doc, { hello: 'world' })
assert.deepStrictEqual(doc, { hello: 'world' })
// Can't use both modes at once if not _id
d.findOne({ a: 2 }, { a: 0, hello: 1 }, function (err, doc) {
@ -1119,16 +1118,16 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
const doc1 = _.find(docs, function (d) { return d.somedata === 'ok' })
const doc2 = _.find(docs, function (d) { return d.somedata === 'again' })
const doc3 = _.find(docs, function (d) { return d.somedata === 'another' })
const doc1 = docs.find(function (d) { return d.somedata === 'ok' })
const doc2 = docs.find(function (d) { return d.somedata === 'again' })
const doc3 = docs.find(function (d) { return d.somedata === 'another' })
docs.length.should.equal(3)
assert.isUndefined(_.find(docs, function (d) { return d.newDoc === 'yes' }))
assert.isUndefined(docs.find(function (d) { return d.newDoc === 'yes' }))
assert.deepEqual(doc1, { _id: doc1._id, somedata: 'ok' })
assert.deepEqual(doc2, { _id: doc2._id, somedata: 'again', plus: 'additional data' })
assert.deepEqual(doc3, { _id: doc3._id, somedata: 'another' })
assert.deepStrictEqual(doc1, { _id: doc1._id, somedata: 'ok' })
assert.deepStrictEqual(doc2, { _id: doc2._id, somedata: 'again', plus: 'additional data' })
assert.deepStrictEqual(doc3, { _id: doc3._id, somedata: 'another' })
return cb()
})
@ -1176,9 +1175,9 @@ describe('Database', function () {
function testPostUpdateState (cb) {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
const doc1 = _.find(docs, function (d) { return d._id === id1 })
const doc2 = _.find(docs, function (d) { return d._id === id2 })
const doc3 = _.find(docs, function (d) { return d._id === id3 })
const doc1 = docs.find(function (d) { return d._id === id1 })
const doc2 = docs.find(function (d) { return d._id === id2 })
const doc3 = docs.find(function (d) { return d._id === id3 })
docs.length.should.equal(3)
@ -1238,22 +1237,22 @@ describe('Database', function () {
function testPostUpdateState (cb) {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
const doc1 = _.find(docs, function (d) { return d._id === id1 })
const doc2 = _.find(docs, function (d) { return d._id === id2 })
const doc3 = _.find(docs, function (d) { return d._id === id3 })
const doc1 = docs.find(function (d) { return d._id === id1 })
const doc2 = docs.find(function (d) { return d._id === id2 })
const doc3 = docs.find(function (d) { return d._id === id3 })
docs.length.should.equal(3)
assert.deepEqual(doc1, { somedata: 'ok', _id: doc1._id })
assert.deepStrictEqual(doc1, { somedata: 'ok', _id: doc1._id })
// doc2 or doc3 was modified. Since we sort on _id and it is random
// it can be either of two situations
try {
assert.deepEqual(doc2, { newDoc: 'yes', _id: doc2._id })
assert.deepEqual(doc3, { somedata: 'again', _id: doc3._id })
assert.deepStrictEqual(doc2, { newDoc: 'yes', _id: doc2._id })
assert.deepStrictEqual(doc3, { somedata: 'again', _id: doc3._id })
} catch (e) {
assert.deepEqual(doc2, { somedata: 'again', plus: 'additional data', _id: doc2._id })
assert.deepEqual(doc3, { newDoc: 'yes', _id: doc3._id })
assert.deepStrictEqual(doc2, { somedata: 'again', plus: 'additional data', _id: doc2._id })
assert.deepStrictEqual(doc3, { newDoc: 'yes', _id: doc3._id })
}
return cb()
@ -1500,7 +1499,7 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
assert.deepEqual(docs, [{ _id: newDoc._id, hello: 'world' }])
assert.deepStrictEqual(docs, [{ _id: newDoc._id, hello: 'world' }])
done()
})
@ -1551,8 +1550,8 @@ describe('Database', function () {
d.find({}, function (err, docs) {
docs.sort(function (a, b) { return a.a - b.a })
docs.length.should.equal(2)
_.isEqual(docs[0], { _id: doc1._id, a: 1, hello: 'world' }).should.equal(true)
_.isEqual(docs[1], { _id: doc2._id, a: 2, hello: 'changed' }).should.equal(true)
assert.deepStrictEqual(docs[0], { _id: doc1._id, a: 1, hello: 'world' })
assert.deepStrictEqual(docs[1], { _id: doc2._id, a: 2, hello: 'changed' })
// Even after a reload the database state hasn't changed
d.loadDatabase(function (err) {
@ -1562,8 +1561,8 @@ describe('Database', function () {
d.find({}, function (err, docs) {
docs.sort(function (a, b) { return a.a - b.a })
docs.length.should.equal(2)
_.isEqual(docs[0], { _id: doc1._id, a: 1, hello: 'world' }).should.equal(true)
_.isEqual(docs[1], { _id: doc2._id, a: 2, hello: 'changed' }).should.equal(true)
assert.deepStrictEqual(docs[0], { _id: doc1._id, a: 1, hello: 'world' })
assert.deepStrictEqual(docs[1], { _id: doc2._id, a: 2, hello: 'changed' })
done()
})
@ -1588,9 +1587,9 @@ describe('Database', function () {
d.find({}, function (err, docs) {
docs.sort(function (a, b) { return a.a - b.a })
docs.length.should.equal(3)
_.isEqual(docs[0], { _id: doc1._id, a: 1, hello: 'changed' }).should.equal(true)
_.isEqual(docs[1], { _id: doc2._id, a: 2, hello: 'changed' }).should.equal(true)
_.isEqual(docs[2], { _id: doc3._id, a: 5, hello: 'pluton' }).should.equal(true)
assert.deepStrictEqual(docs[0], { _id: doc1._id, a: 1, hello: 'changed' })
assert.deepStrictEqual(docs[1], { _id: doc2._id, a: 2, hello: 'changed' })
assert.deepStrictEqual(docs[2], { _id: doc3._id, a: 5, hello: 'pluton' })
// Even after a reload the database state hasn't changed
d.loadDatabase(function (err) {
@ -1600,9 +1599,9 @@ describe('Database', function () {
d.find({}, function (err, docs) {
docs.sort(function (a, b) { return a.a - b.a })
docs.length.should.equal(3)
_.isEqual(docs[0], { _id: doc1._id, a: 1, hello: 'changed' }).should.equal(true)
_.isEqual(docs[1], { _id: doc2._id, a: 2, hello: 'changed' }).should.equal(true)
_.isEqual(docs[2], { _id: doc3._id, a: 5, hello: 'pluton' }).should.equal(true)
assert.deepStrictEqual(docs[0], { _id: doc1._id, a: 1, hello: 'changed' })
assert.deepStrictEqual(docs[1], { _id: doc2._id, a: 2, hello: 'changed' })
assert.deepStrictEqual(docs[2], { _id: doc3._id, a: 5, hello: 'pluton' })
done()
})
@ -1626,9 +1625,9 @@ describe('Database', function () {
nr.should.equal(1)
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
const d1 = _.find(docs, function (doc) { return doc._id === doc1._id })
const d2 = _.find(docs, function (doc) { return doc._id === doc2._id })
const d3 = _.find(docs, function (doc) { return doc._id === doc3._id })
const d1 = docs.find(function (doc) { return doc._id === doc1._id })
const d2 = docs.find(function (doc) { return doc._id === doc2._id })
const d3 = docs.find(function (doc) { return doc._id === doc3._id })
d1.a.should.equal(1)
d2.a.should.equal(12)
@ -1655,18 +1654,20 @@ describe('Database', function () {
assert.isDefined(err)
// No index modified
_.each(d.indexes, function (index) {
for (const key in d.indexes) {
if (Object.prototype.hasOwnProperty.call(d.indexes, key)) {
const index = d.indexes[key]
const docs = index.getAll()
const d1 = _.find(docs, function (doc) { return doc._id === doc1._id })
const d2 = _.find(docs, function (doc) { return doc._id === doc2._id })
const d3 = _.find(docs, function (doc) { return doc._id === doc3._id })
const d1 = docs.find(function (doc) { return doc._id === doc1._id })
const d2 = docs.find(function (doc) { return doc._id === doc2._id })
const d3 = docs.find(function (doc) { return doc._id === doc3._id })
// All changes rolled back, including those that didn't trigger an error
d1.a.should.equal(4)
d2.a.should.equal(5)
d3.a.should.equal('abc')
})
}
}
done()
})
})
@ -1685,15 +1686,17 @@ describe('Database', function () {
assert.isDefined(err)
// Check that no index was modified
_.each(d.indexes, function (index) {
for (const key in d.indexes) {
if (Object.prototype.hasOwnProperty.call(d.indexes, key)) {
const index = d.indexes[key]
const docs = index.getAll()
const d1 = _.find(docs, function (doc) { return doc._id === doc1._id })
const d2 = _.find(docs, function (doc) { return doc._id === doc2._id })
const d1 = docs.find(function (doc) { return doc._id === doc1._id })
const d2 = docs.find(function (doc) { return doc._id === doc2._id })
d1.a.should.equal(4)
d2.a.should.equal(5)
})
}
}
done()
})
})
@ -1960,8 +1963,8 @@ describe('Database', function () {
d.find({}, function (err, docs) {
docs.sort(function (a, b) { return a.a - b.a })
docs.length.should.equal(2)
_.isEqual(docs[0], { _id: doc1._id, a: 1, hello: 'world' }).should.equal(true)
_.isEqual(docs[1], { _id: doc3._id, a: 3, hello: 'moto' }).should.equal(true)
assert.deepStrictEqual(docs[0], { _id: doc1._id, a: 1, hello: 'world' })
assert.deepStrictEqual(docs[1], { _id: doc3._id, a: 3, hello: 'moto' })
// Even after a reload the database state hasn't changed
d.loadDatabase(function (err) {
@ -1971,8 +1974,8 @@ describe('Database', function () {
d.find({}, function (err, docs) {
docs.sort(function (a, b) { return a.a - b.a })
docs.length.should.equal(2)
_.isEqual(docs[0], { _id: doc1._id, a: 1, hello: 'world' }).should.equal(true)
_.isEqual(docs[1], { _id: doc3._id, a: 3, hello: 'moto' }).should.equal(true)
assert.deepStrictEqual(docs[0], { _id: doc1._id, a: 1, hello: 'world' })
assert.deepStrictEqual(docs[1], { _id: doc3._id, a: 3, hello: 'moto' })
done()
})
@ -1997,7 +2000,7 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
docs.length.should.equal(1)
_.isEqual(docs[0], { _id: doc2._id, a: 2, hello: 'earth' }).should.equal(true)
assert.deepStrictEqual(docs[0], { _id: doc2._id, a: 2, hello: 'earth' })
// Even after a reload the database state hasn't changed
d.loadDatabase(function (err) {
@ -2006,7 +2009,7 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
docs.length.should.equal(1)
_.isEqual(docs[0], { _id: doc2._id, a: 2, hello: 'earth' }).should.equal(true)
assert.deepStrictEqual(docs[0], { _id: doc2._id, a: 2, hello: 'earth' })
done()
})
@ -2030,9 +2033,9 @@ describe('Database', function () {
nr.should.equal(1)
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
const d1 = _.find(docs, function (doc) { return doc._id === doc1._id })
const d2 = _.find(docs, function (doc) { return doc._id === doc2._id })
const d3 = _.find(docs, function (doc) { return doc._id === doc3._id })
const d1 = docs.find(function (doc) { return doc._id === doc1._id })
const d2 = docs.find(function (doc) { return doc._id === doc2._id })
const d3 = docs.find(function (doc) { return doc._id === doc3._id })
d1.a.should.equal(1)
assert.isUndefined(d2)
@ -2061,7 +2064,7 @@ describe('Database', function () {
d.loadDatabase(function () {
d.getAllData().length.should.equal(3)
assert.deepEqual(Object.keys(d.indexes), ['_id'])
assert.deepStrictEqual(Object.keys(d.indexes), ['_id'])
d.ensureIndex({ fieldName: 'z' })
d.indexes.z.fieldName.should.equal('z')
@ -2122,7 +2125,7 @@ describe('Database', function () {
d.loadDatabase(function () {
d.getAllData().length.should.equal(2)
assert.deepEqual(Object.keys(d.indexes), ['_id'])
assert.deepStrictEqual(Object.keys(d.indexes), ['_id'])
// eslint-disable-next-line node/handle-callback-err
d.insert({ z: '12', yes: 'yes' }, function (err, newDoc1) {
@ -2130,7 +2133,7 @@ describe('Database', function () {
d.insert({ z: '14', nope: 'nope' }, function (err, newDoc2) {
d.remove({ z: '2' }, {}, function () {
d.update({ z: '1' }, { $set: { yes: 'yep' } }, {}, function () {
assert.deepEqual(Object.keys(d.indexes), ['_id'])
assert.deepStrictEqual(Object.keys(d.indexes), ['_id'])
d.ensureIndex({ fieldName: 'z' })
d.indexes.z.fieldName.should.equal('z')
@ -2146,15 +2149,15 @@ describe('Database', function () {
// The data in the z index is correct
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
const doc0 = _.find(docs, function (doc) { return doc._id === 'aaa' })
const doc1 = _.find(docs, function (doc) { return doc._id === newDoc1._id })
const doc2 = _.find(docs, function (doc) { return doc._id === newDoc2._id })
const doc0 = docs.find(function (doc) { return doc._id === 'aaa' })
const doc1 = docs.find(function (doc) { return doc._id === newDoc1._id })
const doc2 = docs.find(function (doc) { return doc._id === newDoc2._id })
docs.length.should.equal(3)
assert.deepEqual(doc0, { _id: 'aaa', z: '1', a: 2, ages: [1, 5, 12], yes: 'yep' })
assert.deepEqual(doc1, { _id: newDoc1._id, z: '12', yes: 'yes' })
assert.deepEqual(doc2, { _id: newDoc2._id, z: '14', nope: 'nope' })
assert.deepStrictEqual(doc0, { _id: 'aaa', z: '1', a: 2, ages: [1, 5, 12], yes: 'yep' })
assert.deepStrictEqual(doc1, { _id: newDoc1._id, z: '12', yes: 'yes' })
assert.deepStrictEqual(doc2, { _id: newDoc2._id, z: '14', nope: 'nope' })
done()
})
@ -2182,9 +2185,9 @@ describe('Database', function () {
fs.writeFile(testDb, rawData, 'utf8', function () {
d.loadDatabase(function () {
const doc1 = _.find(d.getAllData(), function (doc) { return doc.z === '1' })
const doc2 = _.find(d.getAllData(), function (doc) { return doc.z === '2' })
const doc3 = _.find(d.getAllData(), function (doc) { return doc.z === '3' })
const doc1 = d.getAllData().find(function (doc) { return doc.z === '1' })
const doc2 = d.getAllData().find(function (doc) { return doc.z === '2' })
const doc3 = d.getAllData().find(function (doc) { return doc.z === '3' })
d.getAllData().length.should.equal(3)
@ -2212,9 +2215,9 @@ describe('Database', function () {
fs.writeFile(testDb, rawData, 'utf8', function () {
d.loadDatabase(function (err) {
const doc1 = _.find(d.getAllData(), function (doc) { return doc.z === '1' })
const doc2 = _.find(d.getAllData(), function (doc) { return doc.z === '2' })
const doc3 = _.find(d.getAllData(), function (doc) { return doc.z === '3' })
const doc1 = d.getAllData().find(function (doc) { return doc.z === '1' })
const doc2 = d.getAllData().find(function (doc) { return doc.z === '2' })
const doc3 = d.getAllData().find(function (doc) { return doc.z === '3' })
assert.isNull(err)
d.getAllData().length.should.equal(3)
@ -2268,7 +2271,7 @@ describe('Database', function () {
d.ensureIndex({ fieldName: 'a', unique: true }, function (err) {
err.errorType.should.equal('uniqueViolated')
assert.deepEqual(Object.keys(d.indexes), ['_id', 'b'])
assert.deepStrictEqual(Object.keys(d.indexes), ['_id', 'b'])
done()
})
@ -2304,12 +2307,12 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.insert({ a: 2, z: 'yes' }, function (err, newDoc) {
d.indexes.z.tree.getNumberOfKeys().should.equal(1)
assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc])
assert.deepStrictEqual(d.indexes.z.getMatching('yes'), [newDoc])
// eslint-disable-next-line node/handle-callback-err
d.insert({ a: 5, z: 'nope' }, function (err, newDoc) {
d.indexes.z.tree.getNumberOfKeys().should.equal(2)
assert.deepEqual(d.indexes.z.getMatching('nope'), [newDoc])
assert.deepStrictEqual(d.indexes.z.getMatching('nope'), [newDoc])
done()
})
@ -2325,15 +2328,15 @@ describe('Database', function () {
d.insert({ a: 2, z: 'yes', ya: 'indeed' }, function (err, newDoc) {
d.indexes.z.tree.getNumberOfKeys().should.equal(1)
d.indexes.ya.tree.getNumberOfKeys().should.equal(1)
assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc])
assert.deepEqual(d.indexes.ya.getMatching('indeed'), [newDoc])
assert.deepStrictEqual(d.indexes.z.getMatching('yes'), [newDoc])
assert.deepStrictEqual(d.indexes.ya.getMatching('indeed'), [newDoc])
// eslint-disable-next-line node/handle-callback-err
d.insert({ a: 5, z: 'nope', ya: 'sure' }, function (err, newDoc2) {
d.indexes.z.tree.getNumberOfKeys().should.equal(2)
d.indexes.ya.tree.getNumberOfKeys().should.equal(2)
assert.deepEqual(d.indexes.z.getMatching('nope'), [newDoc2])
assert.deepEqual(d.indexes.ya.getMatching('sure'), [newDoc2])
assert.deepStrictEqual(d.indexes.z.getMatching('nope'), [newDoc2])
assert.deepStrictEqual(d.indexes.ya.getMatching('sure'), [newDoc2])
done()
})
@ -2347,12 +2350,12 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.insert({ a: 2, z: 'yes' }, function (err, newDoc) {
d.indexes.z.tree.getNumberOfKeys().should.equal(1)
assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc])
assert.deepStrictEqual(d.indexes.z.getMatching('yes'), [newDoc])
// eslint-disable-next-line node/handle-callback-err
d.insert({ a: 5, z: 'yes' }, function (err, newDoc2) {
d.indexes.z.tree.getNumberOfKeys().should.equal(1)
assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc, newDoc2])
assert.deepStrictEqual(d.indexes.z.getMatching('yes'), [newDoc, newDoc2])
done()
})
@ -2366,7 +2369,7 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.insert({ a: 2, z: 'yes' }, function (err, newDoc) {
d.indexes.z.tree.getNumberOfKeys().should.equal(1)
assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc])
assert.deepStrictEqual(d.indexes.z.getMatching('yes'), [newDoc])
d.insert({ a: 5, z: 'yes' }, function (err) {
err.errorType.should.equal('uniqueViolated')
@ -2374,13 +2377,13 @@ describe('Database', function () {
// Index didn't change
d.indexes.z.tree.getNumberOfKeys().should.equal(1)
assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc])
assert.deepStrictEqual(d.indexes.z.getMatching('yes'), [newDoc])
// Data didn't change
assert.deepEqual(d.getAllData(), [newDoc])
assert.deepStrictEqual(d.getAllData(), [newDoc])
d.loadDatabase(function () {
d.getAllData().length.should.equal(1)
assert.deepEqual(d.getAllData()[0], newDoc)
assert.deepStrictEqual(d.getAllData()[0], newDoc)
done()
})
@ -2407,9 +2410,9 @@ describe('Database', function () {
d.indexes.uni.tree.getNumberOfKeys().should.equal(1)
d.indexes.nonu2.tree.getNumberOfKeys().should.equal(1)
assert.deepEqual(d.indexes.nonu1.getMatching('yes'), [newDoc])
assert.deepEqual(d.indexes.uni.getMatching('willfail'), [newDoc])
assert.deepEqual(d.indexes.nonu2.getMatching('yes2'), [newDoc])
assert.deepStrictEqual(d.indexes.nonu1.getMatching('yes'), [newDoc])
assert.deepStrictEqual(d.indexes.uni.getMatching('willfail'), [newDoc])
assert.deepStrictEqual(d.indexes.nonu2.getMatching('yes2'), [newDoc])
done()
})
@ -2423,7 +2426,7 @@ describe('Database', function () {
// eslint-disable-next-line node/handle-callback-err
d.insert({ a: 2, z: 'yes' }, function (err, newDoc) {
d.indexes.zzz.tree.getNumberOfKeys().should.equal(1)
assert.deepEqual(d.indexes.zzz.getMatching(undefined), [newDoc])
assert.deepStrictEqual(d.indexes.zzz.getMatching(undefined), [newDoc])
d.insert({ a: 5, z: 'other' }, function (err) {
err.errorType.should.equal('uniqueViolated')
@ -2452,8 +2455,8 @@ describe('Database', function () {
d.insert({ a: 2, b: 'si' }, function (err, doc2) {
// eslint-disable-next-line node/handle-callback-err
d.find({}, function (err, docs) {
assert.deepEqual(doc1, _.find(docs, function (d) { return d._id === doc1._id }))
assert.deepEqual(doc2, _.find(docs, function (d) { return d._id === doc2._id }))
assert.deepStrictEqual(doc1, docs.find(function (d) { return d._id === doc1._id }))
assert.deepStrictEqual(doc2, docs.find(function (d) { return d._id === doc2._id }))
done()
})
@ -2523,27 +2526,27 @@ describe('Database', function () {
d.insert({ a: 2, b: 'si' }, function (err, _doc2) {
d.update({ a: 1 }, { $set: { a: 456, b: 'no' } }, {}, function (err, nr) {
const data = d.getAllData()
const doc1 = _.find(data, function (doc) { return doc._id === _doc1._id })
const doc2 = _.find(data, function (doc) { return doc._id === _doc2._id })
const doc1 = data.find(function (doc) { return doc._id === _doc1._id })
const doc2 = data.find(function (doc) { return doc._id === _doc2._id })
assert.isNull(err)
nr.should.equal(1)
data.length.should.equal(2)
assert.deepEqual(doc1, { a: 456, b: 'no', _id: _doc1._id })
assert.deepEqual(doc2, { a: 2, b: 'si', _id: _doc2._id })
assert.deepStrictEqual(doc1, { a: 456, b: 'no', _id: _doc1._id })
assert.deepStrictEqual(doc2, { a: 2, b: 'si', _id: _doc2._id })
d.update({}, { $inc: { a: 10 }, $set: { b: 'same' } }, { multi: true }, function (err, nr) {
const data = d.getAllData()
const doc1 = _.find(data, function (doc) { return doc._id === _doc1._id })
const doc2 = _.find(data, function (doc) { return doc._id === _doc2._id })
const doc1 = data.find(function (doc) { return doc._id === _doc1._id })
const doc2 = data.find(function (doc) { return doc._id === _doc2._id })
assert.isNull(err)
nr.should.equal(2)
data.length.should.equal(2)
assert.deepEqual(doc1, { a: 466, b: 'same', _id: _doc1._id })
assert.deepEqual(doc2, { a: 12, b: 'same', _id: _doc2._id })
assert.deepStrictEqual(doc1, { a: 466, b: 'same', _id: _doc1._id })
assert.deepStrictEqual(doc2, { a: 12, b: 'same', _id: _doc2._id })
done()
})
@ -2593,8 +2596,8 @@ describe('Database', function () {
d.indexes.b.tree.getNumberOfKeys().should.equal(1)
d.indexes.b.getMatching('same').length.should.equal(2)
_.pluck(d.indexes.b.getMatching('same'), '_id').should.contain(doc1._id)
_.pluck(d.indexes.b.getMatching('same'), '_id').should.contain(doc2._id)
d.indexes.b.getMatching('same').map(x => x._id).should.contain(doc1._id)
d.indexes.b.getMatching('same').map(x => x._id).should.contain(doc2._id)
// The same pointers are shared between all indexes
d.indexes.a.tree.getNumberOfKeys().should.equal(2)
@ -2626,17 +2629,17 @@ describe('Database', function () {
// Will conflict with doc3
d.update({ a: 2 }, { $inc: { a: 10, c: 1000 }, $set: { b: 30 } }, {}, function (err) {
const data = d.getAllData()
const doc1 = _.find(data, function (doc) { return doc._id === _doc1._id })
const doc2 = _.find(data, function (doc) { return doc._id === _doc2._id })
const doc3 = _.find(data, function (doc) { return doc._id === _doc3._id })
const doc1 = data.find(function (doc) { return doc._id === _doc1._id })
const doc2 = data.find(function (doc) { return doc._id === _doc2._id })
const doc3 = data.find(function (doc) { return doc._id === _doc3._id })
err.errorType.should.equal('uniqueViolated')
// Data left unchanged
data.length.should.equal(3)
assert.deepEqual(doc1, { a: 1, b: 10, c: 100, _id: _doc1._id })
assert.deepEqual(doc2, { a: 2, b: 20, c: 200, _id: _doc2._id })
assert.deepEqual(doc3, { a: 3, b: 30, c: 300, _id: _doc3._id })
assert.deepStrictEqual(doc1, { a: 1, b: 10, c: 100, _id: _doc1._id })
assert.deepStrictEqual(doc2, { a: 2, b: 20, c: 200, _id: _doc2._id })
assert.deepStrictEqual(doc3, { a: 3, b: 30, c: 300, _id: _doc3._id })
// All indexes left unchanged and pointing to the same docs
d.indexes.a.tree.getNumberOfKeys().should.equal(3)
@ -2678,17 +2681,17 @@ describe('Database', function () {
$set: { b: 30 }
}, { multi: true }, function (err) {
const data = d.getAllData()
const doc1 = _.find(data, function (doc) { return doc._id === _doc1._id })
const doc2 = _.find(data, function (doc) { return doc._id === _doc2._id })
const doc3 = _.find(data, function (doc) { return doc._id === _doc3._id })
const doc1 = data.find(function (doc) { return doc._id === _doc1._id })
const doc2 = data.find(function (doc) { return doc._id === _doc2._id })
const doc3 = data.find(function (doc) { return doc._id === _doc3._id })
err.errorType.should.equal('uniqueViolated')
// Data left unchanged
data.length.should.equal(3)
assert.deepEqual(doc1, { a: 1, b: 10, c: 100, _id: _doc1._id })
assert.deepEqual(doc2, { a: 2, b: 20, c: 200, _id: _doc2._id })
assert.deepEqual(doc3, { a: 3, b: 30, c: 300, _id: _doc3._id })
assert.deepStrictEqual(doc1, { a: 1, b: 10, c: 100, _id: _doc1._id })
assert.deepStrictEqual(doc2, { a: 2, b: 20, c: 200, _id: _doc2._id })
assert.deepStrictEqual(doc3, { a: 3, b: 30, c: 300, _id: _doc3._id })
// All indexes left unchanged and pointing to the same docs
d.indexes.a.tree.getNumberOfKeys().should.equal(3)
@ -2726,15 +2729,15 @@ describe('Database', function () {
d.insert({ a: 3, b: 'coin' }, function (err, _doc3) {
d.remove({ a: 1 }, {}, function (err, nr) {
const data = d.getAllData()
const doc2 = _.find(data, function (doc) { return doc._id === _doc2._id })
const doc3 = _.find(data, function (doc) { return doc._id === _doc3._id })
const doc2 = data.find(function (doc) { return doc._id === _doc2._id })
const doc3 = data.find(function (doc) { return doc._id === _doc3._id })
assert.isNull(err)
nr.should.equal(1)
data.length.should.equal(2)
assert.deepEqual(doc2, { a: 2, b: 'si', _id: _doc2._id })
assert.deepEqual(doc3, { a: 3, b: 'coin', _id: _doc3._id })
assert.deepStrictEqual(doc2, { a: 2, b: 'si', _id: _doc2._id })
assert.deepStrictEqual(doc3, { a: 3, b: 'coin', _id: _doc3._id })
d.remove({ a: { $in: [2, 3] } }, { multi: true }, function (err, nr) {
const data = d.getAllData()

@ -19,9 +19,9 @@ describe('Indexes', function () {
// The underlying BST now has 3 nodes which contain the docs where it's expected
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('hello'), [{ a: 5, tf: 'hello' }])
assert.deepEqual(idx.tree.search('world'), [{ a: 8, tf: 'world' }])
assert.deepEqual(idx.tree.search('bloup'), [{ a: 2, tf: 'bloup' }])
assert.deepStrictEqual(idx.tree.search('hello'), [{ a: 5, tf: 'hello' }])
assert.deepStrictEqual(idx.tree.search('world'), [{ a: 8, tf: 'world' }])
assert.deepStrictEqual(idx.tree.search('bloup'), [{ a: 2, tf: 'bloup' }])
// The nodes contain pointers to the actual documents
idx.tree.search('world')[0].should.equal(doc2)
@ -70,9 +70,9 @@ describe('Indexes', function () {
// The underlying BST now has 3 nodes which contain the docs where it's expected
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('hello'), [doc1])
assert.deepEqual(idx.tree.search('world'), [doc2])
assert.deepEqual(idx.tree.search('bloup'), [doc3])
assert.deepStrictEqual(idx.tree.search('hello'), [doc1])
assert.deepStrictEqual(idx.tree.search('world'), [doc2])
assert.deepStrictEqual(idx.tree.search('bloup'), [doc3])
// The nodes contain pointers to the actual documents
idx.tree.search('bloup')[0].a = 42
@ -87,9 +87,9 @@ describe('Indexes', function () {
idx.insert([doc1, doc2, doc3])
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('hello'), [doc1])
assert.deepEqual(idx.tree.search('world'), [doc2])
assert.deepEqual(idx.tree.search('bloup'), [doc3])
assert.deepStrictEqual(idx.tree.search('hello'), [doc1])
assert.deepStrictEqual(idx.tree.search('world'), [doc2])
assert.deepStrictEqual(idx.tree.search('bloup'), [doc3])
})
it('When inserting an array of elements, if an error is thrown all inserts need to be rolled back', function () {
@ -105,9 +105,9 @@ describe('Indexes', function () {
e.errorType.should.equal('uniqueViolated')
}
idx.tree.getNumberOfKeys().should.equal(0)
assert.deepEqual(idx.tree.search('hello'), [])
assert.deepEqual(idx.tree.search('world'), [])
assert.deepEqual(idx.tree.search('bloup'), [])
assert.deepStrictEqual(idx.tree.search('hello'), [])
assert.deepStrictEqual(idx.tree.search('world'), [])
assert.deepStrictEqual(idx.tree.search('bloup'), [])
})
describe('Array fields', function () {
@ -274,9 +274,9 @@ describe('Indexes', function () {
idx.tree.getNumberOfKeys().should.equal(3)
idx.remove([doc1, doc3])
idx.tree.getNumberOfKeys().should.equal(1)
assert.deepEqual(idx.tree.search('hello'), [])
assert.deepEqual(idx.tree.search('world'), [doc2])
assert.deepEqual(idx.tree.search('bloup'), [])
assert.deepStrictEqual(idx.tree.search('hello'), [])
assert.deepStrictEqual(idx.tree.search('world'), [doc2])
assert.deepStrictEqual(idx.tree.search('bloup'), [])
})
}) // ==== End of 'Removal' ==== //
@ -293,16 +293,16 @@ describe('Indexes', function () {
idx.insert(doc2)
idx.insert(doc3)
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('world'), [doc2])
assert.deepStrictEqual(idx.tree.search('world'), [doc2])
idx.update(doc2, doc4)
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('world'), [doc4])
assert.deepStrictEqual(idx.tree.search('world'), [doc4])
idx.update(doc1, doc5)
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('hello'), [])
assert.deepEqual(idx.tree.search('changed'), [doc5])
assert.deepStrictEqual(idx.tree.search('hello'), [])
assert.deepStrictEqual(idx.tree.search('changed'), [doc5])
})
it('If a simple update violates a unique constraint, changes are rolled back and an error thrown', function () {
@ -317,9 +317,9 @@ describe('Indexes', function () {
idx.insert(doc3)
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('hello'), [doc1])
assert.deepEqual(idx.tree.search('world'), [doc2])
assert.deepEqual(idx.tree.search('bloup'), [doc3])
assert.deepStrictEqual(idx.tree.search('hello'), [doc1])
assert.deepStrictEqual(idx.tree.search('world'), [doc2])
assert.deepStrictEqual(idx.tree.search('bloup'), [doc3])
try {
idx.update(doc3, bad)
@ -329,9 +329,9 @@ describe('Indexes', function () {
// No change
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('hello'), [doc1])
assert.deepEqual(idx.tree.search('world'), [doc2])
assert.deepEqual(idx.tree.search('bloup'), [doc3])
assert.deepStrictEqual(idx.tree.search('hello'), [doc1])
assert.deepStrictEqual(idx.tree.search('world'), [doc2])
assert.deepStrictEqual(idx.tree.search('bloup'), [doc3])
})
it('Can update an array of documents', function () {
@ -413,11 +413,11 @@ describe('Indexes', function () {
idx.insert(doc2)
idx.insert(doc3)
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('world'), [doc2])
assert.deepStrictEqual(idx.tree.search('world'), [doc2])
idx.update(doc2, noChange) // No error thrown
idx.tree.getNumberOfKeys().should.equal(3)
assert.deepEqual(idx.tree.search('world'), [noChange])
assert.deepStrictEqual(idx.tree.search('world'), [noChange])
})
it('Can revert simple and batch updates', function () {
@ -494,9 +494,9 @@ describe('Indexes', function () {
idx.insert(doc3)
idx.insert(doc4)
assert.deepEqual(idx.getMatching('bloup'), [doc3])
assert.deepEqual(idx.getMatching('world'), [doc2, doc4])
assert.deepEqual(idx.getMatching('nope'), [])
assert.deepStrictEqual(idx.getMatching('bloup'), [doc3])
assert.deepStrictEqual(idx.getMatching('world'), [doc2, doc4])
assert.deepStrictEqual(idx.getMatching('nope'), [])
})
it('Can get all documents for a given key in a unique index', function () {
@ -509,9 +509,9 @@ describe('Indexes', function () {
idx.insert(doc2)
idx.insert(doc3)
assert.deepEqual(idx.getMatching('bloup'), [doc3])
assert.deepEqual(idx.getMatching('world'), [doc2])
assert.deepEqual(idx.getMatching('nope'), [])
assert.deepStrictEqual(idx.getMatching('bloup'), [doc3])
assert.deepStrictEqual(idx.getMatching('world'), [doc2])
assert.deepStrictEqual(idx.getMatching('nope'), [])
})
it('Can get all documents for which a field is undefined', function () {
@ -525,19 +525,19 @@ describe('Indexes', function () {
idx.insert(doc2)
idx.insert(doc3)
assert.deepEqual(idx.getMatching('bloup'), [])
assert.deepEqual(idx.getMatching('hello'), [doc1])
assert.deepEqual(idx.getMatching('world'), [doc3])
assert.deepEqual(idx.getMatching('yes'), [])
assert.deepEqual(idx.getMatching(undefined), [doc2])
assert.deepStrictEqual(idx.getMatching('bloup'), [])
assert.deepStrictEqual(idx.getMatching('hello'), [doc1])
assert.deepStrictEqual(idx.getMatching('world'), [doc3])
assert.deepStrictEqual(idx.getMatching('yes'), [])
assert.deepStrictEqual(idx.getMatching(undefined), [doc2])
idx.insert(doc4)
assert.deepEqual(idx.getMatching('bloup'), [])
assert.deepEqual(idx.getMatching('hello'), [doc1])
assert.deepEqual(idx.getMatching('world'), [doc3])
assert.deepEqual(idx.getMatching('yes'), [])
assert.deepEqual(idx.getMatching(undefined), [doc2, doc4])
assert.deepStrictEqual(idx.getMatching('bloup'), [])
assert.deepStrictEqual(idx.getMatching('hello'), [doc1])
assert.deepStrictEqual(idx.getMatching('world'), [doc3])
assert.deepStrictEqual(idx.getMatching('yes'), [])
assert.deepStrictEqual(idx.getMatching(undefined), [doc2, doc4])
})
it('Can get all documents for which a field is null', function () {
@ -551,19 +551,19 @@ describe('Indexes', function () {
idx.insert(doc2)
idx.insert(doc3)
assert.deepEqual(idx.getMatching('bloup'), [])
assert.deepEqual(idx.getMatching('hello'), [doc1])
assert.deepEqual(idx.getMatching('world'), [doc3])
assert.deepEqual(idx.getMatching('yes'), [])
assert.deepEqual(idx.getMatching(null), [doc2])
assert.deepStrictEqual(idx.getMatching('bloup'), [])
assert.deepStrictEqual(idx.getMatching('hello'), [doc1])
assert.deepStrictEqual(idx.getMatching('world'), [doc3])
assert.deepStrictEqual(idx.getMatching('yes'), [])
assert.deepStrictEqual(idx.getMatching(null), [doc2])
idx.insert(doc4)
assert.deepEqual(idx.getMatching('bloup'), [])
assert.deepEqual(idx.getMatching('hello'), [doc1])
assert.deepEqual(idx.getMatching('world'), [doc3])
assert.deepEqual(idx.getMatching('yes'), [])
assert.deepEqual(idx.getMatching(null), [doc2, doc4])
assert.deepStrictEqual(idx.getMatching('bloup'), [])
assert.deepStrictEqual(idx.getMatching('hello'), [doc1])
assert.deepStrictEqual(idx.getMatching('world'), [doc3])
assert.deepStrictEqual(idx.getMatching('yes'), [])
assert.deepStrictEqual(idx.getMatching(null), [doc2, doc4])
})
it('Can get all documents for a given key in a sparse index, but not unindexed docs (= field undefined)', function () {
@ -578,11 +578,11 @@ describe('Indexes', function () {
idx.insert(doc3)
idx.insert(doc4)
assert.deepEqual(idx.getMatching('bloup'), [])
assert.deepEqual(idx.getMatching('hello'), [doc1])
assert.deepEqual(idx.getMatching('world'), [doc3])
assert.deepEqual(idx.getMatching('yes'), [])
assert.deepEqual(idx.getMatching(undefined), [])
assert.deepStrictEqual(idx.getMatching('bloup'), [])
assert.deepStrictEqual(idx.getMatching('hello'), [doc1])
assert.deepStrictEqual(idx.getMatching('world'), [doc3])
assert.deepStrictEqual(idx.getMatching('yes'), [])
assert.deepStrictEqual(idx.getMatching(undefined), [])
})
it('Can get all documents whose key is in an array of keys', function () {
@ -603,11 +603,11 @@ describe('Indexes', function () {
idx.insert(doc4)
idx.insert(doc5)
assert.deepEqual(idx.getMatching([]), [])
assert.deepEqual(idx.getMatching(['bloup']), [doc2])
assert.deepEqual(idx.getMatching(['bloup', 'yes']), [doc2, doc4, doc5])
assert.deepEqual(idx.getMatching(['hello', 'no']), [doc1])
assert.deepEqual(idx.getMatching(['nope', 'no']), [])
assert.deepStrictEqual(idx.getMatching([]), [])
assert.deepStrictEqual(idx.getMatching(['bloup']), [doc2])
assert.deepStrictEqual(idx.getMatching(['bloup', 'yes']), [doc2, doc4, doc5])
assert.deepStrictEqual(idx.getMatching(['hello', 'no']), [doc1])
assert.deepStrictEqual(idx.getMatching(['nope', 'no']), [])
})
it('Can get all documents whose key is between certain bounds', function () {
@ -624,9 +624,9 @@ describe('Indexes', function () {
idx.insert(doc4)
idx.insert(doc5)
assert.deepEqual(idx.getBetweenBounds({ $lt: 10, $gte: 5 }), [doc1, doc4, doc3])
assert.deepEqual(idx.getBetweenBounds({ $lte: 8 }), [doc2, doc1, doc4, doc3])
assert.deepEqual(idx.getBetweenBounds({ $gt: 7 }), [doc3, doc5])
assert.deepStrictEqual(idx.getBetweenBounds({ $lt: 10, $gte: 5 }), [doc1, doc4, doc3])
assert.deepStrictEqual(idx.getBetweenBounds({ $lte: 8 }), [doc2, doc1, doc4, doc3])
assert.deepStrictEqual(idx.getBetweenBounds({ $gt: 7 }), [doc3, doc5])
})
}) // ==== End of 'Get matching documents' ==== //
@ -713,6 +713,6 @@ describe('Indexes', function () {
idx.insert(doc2)
idx.insert(doc3)
assert.deepEqual(idx.getAll(), [{ a: 2, tf: 'bloup' }, { a: 5, tf: 'hello' }, { a: 8, tf: 'world' }])
assert.deepStrictEqual(idx.getAll(), [{ a: 2, tf: 'bloup' }, { a: 5, tf: 'hello' }, { a: 8, tf: 'world' }])
})
})

@ -1,7 +1,6 @@
/* eslint-env mocha */
const model = require('../lib/model')
const chai = require('chai')
const _ = require('underscore')
const util = require('util')
const Datastore = require('../lib/datastore')
const fs = require('fs')
@ -256,7 +255,7 @@ describe('Model', function () {
}
const b = model.deepCopy(a)
assert.deepEqual(a, b)
assert.deepStrictEqual(a, b)
})
it('With the strictKeys option, only valid keys gets deep copied', function () {
@ -269,7 +268,7 @@ describe('Model', function () {
}
const b = model.deepCopy(a, true)
assert.deepEqual(b, { a: 4, nested: { yes: 1 }, array: [{}, { yes: true }, {}] })
assert.deepStrictEqual(b, { a: 4, nested: { yes: 1 }, array: [{}, { yes: true }, {}] })
})
}) // ==== End of 'Deep copying' ==== //
@ -366,10 +365,10 @@ describe('Model', function () {
}
const modified = model.modify(obj, updateQuery)
_.isEqual(modified, {
assert.deepStrictEqual(modified, {
yup: { subfield: 'changed', yop: 'yes indeed' },
totally: { doesnt: { exist: 'now it does' } }
}).should.equal(true)
})
})
it('Doesn\'t replace a falsy field by an object when recursively following dot notation', function () {
@ -377,7 +376,7 @@ describe('Model', function () {
const updateQuery = { $set: { 'nested.now': 'it is' } }
const modified = model.modify(obj, updateQuery)
assert.deepEqual(modified, { nested: false }) // Object not modified as the nested field doesn't exist
assert.deepStrictEqual(modified, { nested: false }) // Object not modified as the nested field doesn't exist
})
}) // End of '$set modifier'
@ -390,17 +389,17 @@ describe('Model', function () {
obj = { yup: 'yes', other: 'also' }
updateQuery = { $unset: { yup: true } }
modified = model.modify(obj, updateQuery)
assert.deepEqual(modified, { other: 'also' })
assert.deepStrictEqual(modified, { other: 'also' })
obj = { yup: 'yes', other: 'also' }
updateQuery = { $unset: { nope: true } }
modified = model.modify(obj, updateQuery)
assert.deepEqual(modified, obj)
assert.deepStrictEqual(modified, obj)
obj = { yup: 'yes', other: 'also' }
updateQuery = { $unset: { nope: true, other: true } }
modified = model.modify(obj, updateQuery)
assert.deepEqual(modified, { yup: 'yes' })
assert.deepStrictEqual(modified, { yup: 'yes' })
})
it('Can unset sub-fields and entire nested documents', function () {
@ -411,28 +410,28 @@ describe('Model', function () {
obj = { yup: 'yes', nested: { a: 'also', b: 'yeah' } }
updateQuery = { $unset: { nested: true } }
modified = model.modify(obj, updateQuery)
assert.deepEqual(modified, { yup: 'yes' })
assert.deepStrictEqual(modified, { yup: 'yes' })
obj = { yup: 'yes', nested: { a: 'also', b: 'yeah' } }
updateQuery = { $unset: { 'nested.a': true } }
modified = model.modify(obj, updateQuery)
assert.deepEqual(modified, { yup: 'yes', nested: { b: 'yeah' } })
assert.deepStrictEqual(modified, { yup: 'yes', nested: { b: 'yeah' } })
obj = { yup: 'yes', nested: { a: 'also', b: 'yeah' } }
updateQuery = { $unset: { 'nested.a': true, 'nested.b': true } }
modified = model.modify(obj, updateQuery)
assert.deepEqual(modified, { yup: 'yes', nested: {} })
assert.deepStrictEqual(modified, { yup: 'yes', nested: {} })
})
it('When unsetting nested fields, should not create an empty parent to nested field', function () {
let obj = model.modify({ argh: true }, { $unset: { 'bad.worse': true } })
assert.deepEqual(obj, { argh: true })
assert.deepStrictEqual(obj, { argh: true })
obj = model.modify({ argh: true, bad: { worse: 'oh' } }, { $unset: { 'bad.worse': true } })
assert.deepEqual(obj, { argh: true, bad: {} })
assert.deepStrictEqual(obj, { argh: true, bad: {} })
obj = model.modify({ argh: true, bad: {} }, { $unset: { 'bad.worse': true } })
assert.deepEqual(obj, { argh: true, bad: {} })
assert.deepStrictEqual(obj, { argh: true, bad: {} })
})
}) // End of '$unset modifier'
@ -456,18 +455,18 @@ describe('Model', function () {
let modified
modified = model.modify(obj, { $inc: { nay: 2 } })
_.isEqual(modified, { some: 'thing', nay: 42 }).should.equal(true)
assert.deepStrictEqual(modified, { some: 'thing', nay: 42 })
// Incidentally, this tests that obj was not modified
modified = model.modify(obj, { $inc: { inexistent: -6 } })
_.isEqual(modified, { some: 'thing', nay: 40, inexistent: -6 }).should.equal(true)
assert.deepStrictEqual(modified, { some: 'thing', nay: 40, inexistent: -6 })
})
it('Works recursively', function () {
const obj = { some: 'thing', nay: { nope: 40 } }
const modified = model.modify(obj, { $inc: { 'nay.nope': -2, 'blip.blop': 123 } })
_.isEqual(modified, { some: 'thing', nay: { nope: 38 }, blip: { blop: 123 } }).should.equal(true)
assert.deepStrictEqual(modified, { some: 'thing', nay: { nope: 38 }, blip: { blop: 123 } })
})
}) // End of '$inc modifier'
@ -476,14 +475,14 @@ describe('Model', function () {
const obj = { arr: ['hello'] }
const modified = model.modify(obj, { $push: { arr: 'world' } })
assert.deepEqual(modified, { arr: ['hello', 'world'] })
assert.deepStrictEqual(modified, { arr: ['hello', 'world'] })
})
it('Can push an element to a non-existent field and will create the array', function () {
const obj = {}
const modified = model.modify(obj, { $push: { arr: 'world' } })
assert.deepEqual(modified, { arr: ['world'] })
assert.deepStrictEqual(modified, { arr: ['world'] })
})
it('Can push on nested fields', function () {
@ -491,11 +490,11 @@ describe('Model', function () {
let modified
modified = model.modify(obj, { $push: { 'arr.nested': 'world' } })
assert.deepEqual(modified, { arr: { nested: ['hello', 'world'] } })
assert.deepStrictEqual(modified, { arr: { nested: ['hello', 'world'] } })
obj = { arr: { a: 2 } }
modified = model.modify(obj, { $push: { 'arr.nested': 'world' } })
assert.deepEqual(modified, { arr: { a: 2, nested: ['world'] } })
assert.deepStrictEqual(modified, { arr: { a: 2, nested: ['world'] } })
})
it('Throw if we try to push to a non-array', function () {
@ -515,7 +514,7 @@ describe('Model', function () {
const obj = { arr: ['hello'] }
const modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'] } } })
assert.deepEqual(modified, { arr: ['hello', 'world', 'earth', 'everything'] });
assert.deepStrictEqual(modified, { arr: ['hello', 'world', 'earth', 'everything'] });
(function () {
model.modify(obj, { $push: { arr: { $each: 45 } } })
@ -531,32 +530,32 @@ describe('Model', function () {
let modified
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: 1 } } })
assert.deepEqual(modified, { arr: ['hello'] })
assert.deepStrictEqual(modified, { arr: ['hello'] })
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: -1 } } })
assert.deepEqual(modified, { arr: ['everything'] })
assert.deepStrictEqual(modified, { arr: ['everything'] })
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: 0 } } })
assert.deepEqual(modified, { arr: [] })
assert.deepStrictEqual(modified, { arr: [] })
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: 2 } } })
assert.deepEqual(modified, { arr: ['hello', 'world'] })
assert.deepStrictEqual(modified, { arr: ['hello', 'world'] })
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: -2 } } })
assert.deepEqual(modified, { arr: ['earth', 'everything'] })
assert.deepStrictEqual(modified, { arr: ['earth', 'everything'] })
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: -20 } } })
assert.deepEqual(modified, { arr: ['hello', 'world', 'earth', 'everything'] })
assert.deepStrictEqual(modified, { arr: ['hello', 'world', 'earth', 'everything'] })
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: 20 } } })
assert.deepEqual(modified, { arr: ['hello', 'world', 'earth', 'everything'] })
assert.deepStrictEqual(modified, { arr: ['hello', 'world', 'earth', 'everything'] })
modified = model.modify(obj, { $push: { arr: { $each: [], $slice: 1 } } })
assert.deepEqual(modified, { arr: ['hello'] })
assert.deepStrictEqual(modified, { arr: ['hello'] })
// $each not specified, but $slice is
modified = model.modify(obj, { $push: { arr: { $slice: 1 } } })
assert.deepEqual(modified, { arr: ['hello'] });
assert.deepStrictEqual(modified, { arr: ['hello'] });
(function () {
modified = model.modify(obj, { $push: { arr: { $slice: 1, unauthorized: true } } })
@ -574,18 +573,18 @@ describe('Model', function () {
let modified
modified = model.modify(obj, { $addToSet: { arr: 'world' } })
assert.deepEqual(modified, { arr: ['hello', 'world'] })
assert.deepStrictEqual(modified, { arr: ['hello', 'world'] })
obj = { arr: ['hello'] }
modified = model.modify(obj, { $addToSet: { arr: 'hello' } })
assert.deepEqual(modified, { arr: ['hello'] })
assert.deepStrictEqual(modified, { arr: ['hello'] })
})
it('Can add an element to a non-existent set and will create the array', function () {
const obj = { arr: [] }
const modified = model.modify(obj, { $addToSet: { arr: 'world' } })
assert.deepEqual(modified, { arr: ['world'] })
assert.deepStrictEqual(modified, { arr: ['world'] })
})
it('Throw if we try to addToSet to a non-array', function () {
@ -601,11 +600,11 @@ describe('Model', function () {
let modified
modified = model.modify(obj, { $addToSet: { arr: { b: 3 } } })
assert.deepEqual(modified, { arr: [{ b: 2 }, { b: 3 }] })
assert.deepStrictEqual(modified, { arr: [{ b: 2 }, { b: 3 }] })
obj = { arr: [{ b: 2 }] }
modified = model.modify(obj, { $addToSet: { arr: { b: 2 } } })
assert.deepEqual(modified, { arr: [{ b: 2 }] })
assert.deepStrictEqual(modified, { arr: [{ b: 2 }] })
})
it('Can use the $each modifier to add multiple values to a set at once', function () {
@ -613,7 +612,7 @@ describe('Model', function () {
let modified
modified = model.modify(obj, { $addToSet: { arr: { $each: ['world', 'earth', 'hello', 'earth'] } } })
assert.deepEqual(modified, { arr: ['hello', 'world', 'earth'] });
assert.deepStrictEqual(modified, { arr: ['hello', 'world', 'earth'] });
(function () {
modified = model.modify(obj, { $addToSet: { arr: { $each: 45 } } })
@ -650,18 +649,18 @@ describe('Model', function () {
obj = { arr: [1, 4, 8] }
modified = model.modify(obj, { $pop: { arr: 1 } })
assert.deepEqual(modified, { arr: [1, 4] })
assert.deepStrictEqual(modified, { arr: [1, 4] })
obj = { arr: [1, 4, 8] }
modified = model.modify(obj, { $pop: { arr: -1 } })
assert.deepEqual(modified, { arr: [4, 8] })
assert.deepStrictEqual(modified, { arr: [4, 8] })
// Empty arrays are not changed
obj = { arr: [] }
modified = model.modify(obj, { $pop: { arr: 1 } })
assert.deepEqual(modified, { arr: [] })
assert.deepStrictEqual(modified, { arr: [] })
modified = model.modify(obj, { $pop: { arr: -1 } })
assert.deepEqual(modified, { arr: [] })
assert.deepStrictEqual(modified, { arr: [] })
})
}) // End of '$pop modifier'
@ -671,18 +670,18 @@ describe('Model', function () {
let modified
modified = model.modify(obj, { $pull: { arr: 'world' } })
assert.deepEqual(modified, { arr: ['hello'] })
assert.deepStrictEqual(modified, { arr: ['hello'] })
obj = { arr: ['hello'] }
modified = model.modify(obj, { $pull: { arr: 'world' } })
assert.deepEqual(modified, { arr: ['hello'] })
assert.deepStrictEqual(modified, { arr: ['hello'] })
})
it('Can remove multiple matching elements', function () {
const obj = { arr: ['hello', 'world', 'hello', 'world'] }
const modified = model.modify(obj, { $pull: { arr: 'world' } })
assert.deepEqual(modified, { arr: ['hello', 'hello'] })
assert.deepStrictEqual(modified, { arr: ['hello', 'hello'] })
})
it('Throw if we try to pull from a non-array', function () {
@ -698,11 +697,11 @@ describe('Model', function () {
let modified
modified = model.modify(obj, { $pull: { arr: { b: 3 } } })
assert.deepEqual(modified, { arr: [{ b: 2 }] })
assert.deepStrictEqual(modified, { arr: [{ b: 2 }] })
obj = { arr: [{ b: 2 }] }
modified = model.modify(obj, { $pull: { arr: { b: 3 } } })
assert.deepEqual(modified, { arr: [{ b: 2 }] })
assert.deepStrictEqual(modified, { arr: [{ b: 2 }] })
})
it('Can use any kind of nedb query with $pull', function () {
@ -710,11 +709,11 @@ describe('Model', function () {
let modified
modified = model.modify(obj, { $pull: { arr: { $gte: 5 } } })
assert.deepEqual(modified, { arr: [4, 2], other: 'yup' })
assert.deepStrictEqual(modified, { arr: [4, 2], other: 'yup' })
obj = { arr: [{ b: 4 }, { b: 7 }, { b: 1 }], other: 'yeah' }
modified = model.modify(obj, { $pull: { arr: { b: { $gte: 5 } } } })
assert.deepEqual(modified, { arr: [{ b: 4 }, { b: 1 }], other: 'yeah' })
assert.deepStrictEqual(modified, { arr: [{ b: 4 }, { b: 1 }], other: 'yeah' })
})
}) // End of '$pull modifier'
@ -989,14 +988,14 @@ describe('Model', function () {
number: 9
}]
}, 'planets.name')
assert.deepEqual(dv, ['Earth', 'Mars', 'Pluton'])
assert.deepStrictEqual(dv, ['Earth', 'Mars', 'Pluton'])
// Nested array of subdocuments
dv = model.getDotValue({
nedb: true,
data: { planets: [{ name: 'Earth', number: 3 }, { name: 'Mars', number: 2 }, { name: 'Pluton', number: 9 }] }
}, 'data.planets.number')
assert.deepEqual(dv, [3, 2, 9])
assert.deepStrictEqual(dv, [3, 2, 9])
// Nested array in a subdocument of an array (yay, inception!)
// TODO: make sure MongoDB doesn't flatten the array (it wouldn't make sense)
@ -1009,7 +1008,7 @@ describe('Model', function () {
}]
}
}, 'data.planets.numbers')
assert.deepEqual(dv, [[1, 3], [7], [9, 5, 1]])
assert.deepStrictEqual(dv, [[1, 3], [7], [9, 5, 1]])
})
it('Can get a single value out of an array using its index', function () {
@ -1022,7 +1021,7 @@ describe('Model', function () {
number: 9
}]
}, 'planets.1')
assert.deepEqual(dv, { name: 'Mars', number: 2 })
assert.deepStrictEqual(dv, { name: 'Mars', number: 2 })
// Out of bounds index
dv = model.getDotValue({
@ -1038,7 +1037,7 @@ describe('Model', function () {
nedb: true,
data: { planets: [{ name: 'Earth', number: 3 }, { name: 'Mars', number: 2 }, { name: 'Pluton', number: 9 }] }
}, 'data.planets.2')
assert.deepEqual(dv, { name: 'Pluton', number: 9 })
assert.deepStrictEqual(dv, { name: 'Pluton', number: 9 })
// Dot notation with index in the middle
dv = model.getDotValue({

@ -3,7 +3,6 @@ const chai = require('chai')
const testDb = 'workspace/test.db'
const fs = require('fs')
const path = require('path')
const _ = require('underscore')
const async = require('async')
const model = require('../lib/model')
const Datastore = require('../lib/datastore')
@ -51,9 +50,9 @@ describe('Persistence', function () {
treatedData.sort(function (a, b) { return a._id - b._id })
treatedData.length.should.equal(3)
_.isEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] }).should.equal(true)
_.isEqual(treatedData[1], { _id: '2', hello: 'world' }).should.equal(true)
_.isEqual(treatedData[2], { _id: '3', nested: { today: now } }).should.equal(true)
assert.deepStrictEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] })
assert.deepStrictEqual(treatedData[1], { _id: '2', hello: 'world' })
assert.deepStrictEqual(treatedData[2], { _id: '3', nested: { today: now } })
})
it('Badly formatted lines have no impact on the treated data', function () {
@ -65,8 +64,8 @@ describe('Persistence', function () {
treatedData.sort(function (a, b) { return a._id - b._id })
treatedData.length.should.equal(2)
_.isEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] }).should.equal(true)
_.isEqual(treatedData[1], { _id: '3', nested: { today: now } }).should.equal(true)
assert.deepStrictEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] })
assert.deepStrictEqual(treatedData[1], { _id: '3', nested: { today: now } })
})
it('Well formatted lines that have no _id are not included in the data', function () {
@ -78,8 +77,8 @@ describe('Persistence', function () {
treatedData.sort(function (a, b) { return a._id - b._id })
treatedData.length.should.equal(2)
_.isEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] }).should.equal(true)
_.isEqual(treatedData[1], { _id: '2', hello: 'world' }).should.equal(true)
assert.deepStrictEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] })
assert.deepStrictEqual(treatedData[1], { _id: '2', hello: 'world' })
})
it('If two lines concern the same doc (= same _id), the last one is the good version', function () {
@ -91,8 +90,8 @@ describe('Persistence', function () {
treatedData.sort(function (a, b) { return a._id - b._id })
treatedData.length.should.equal(2)
_.isEqual(treatedData[0], { _id: '1', nested: { today: now } }).should.equal(true)
_.isEqual(treatedData[1], { _id: '2', hello: 'world' }).should.equal(true)
assert.deepStrictEqual(treatedData[0], { _id: '1', nested: { today: now } })
assert.deepStrictEqual(treatedData[1], { _id: '2', hello: 'world' })
})
it('If a doc contains $$deleted: true, that means we need to remove it from the data', function () {
@ -105,8 +104,8 @@ describe('Persistence', function () {
treatedData.sort(function (a, b) { return a._id - b._id })
treatedData.length.should.equal(2)
_.isEqual(treatedData[0], { _id: '2', hello: 'world' }).should.equal(true)
_.isEqual(treatedData[1], { _id: '3', today: now }).should.equal(true)
assert.deepStrictEqual(treatedData[0], { _id: '2', hello: 'world' })
assert.deepStrictEqual(treatedData[1], { _id: '3', today: now })
})
it('If a doc contains $$deleted: true, no error is thrown if the doc wasnt in the list before', function () {
@ -118,8 +117,8 @@ describe('Persistence', function () {
treatedData.sort(function (a, b) { return a._id - b._id })
treatedData.length.should.equal(2)
_.isEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] }).should.equal(true)
_.isEqual(treatedData[1], { _id: '3', today: now }).should.equal(true)
assert.deepStrictEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] })
assert.deepStrictEqual(treatedData[1], { _id: '3', today: now })
})
it('If a doc contains $$indexCreated, no error is thrown during treatRawData and we can get the index options', function () {
@ -131,12 +130,12 @@ describe('Persistence', function () {
const indexes = d.persistence.treatRawData(rawData).indexes
Object.keys(indexes).length.should.equal(1)
assert.deepEqual(indexes.test, { fieldName: 'test', unique: true })
assert.deepStrictEqual(indexes.test, { fieldName: 'test', unique: true })
treatedData.sort(function (a, b) { return a._id - b._id })
treatedData.length.should.equal(2)
_.isEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] }).should.equal(true)
_.isEqual(treatedData[1], { _id: '3', today: now }).should.equal(true)
assert.deepStrictEqual(treatedData[0], { _id: '1', a: 2, ages: [1, 5, 12] })
assert.deepStrictEqual(treatedData[1], { _id: '3', today: now })
})
it('Compact database on load', function (done) {
@ -173,8 +172,8 @@ describe('Persistence', function () {
assert.isNull(err)
d.insert({ a: 2 }, function (err) {
const data = d.getAllData()
const doc1 = _.find(data, function (doc) { return doc.a === 1 })
const doc2 = _.find(data, function (doc) { return doc.a === 2 })
const doc1 = data.find(function (doc) { return doc.a === 1 })
const doc2 = data.find(function (doc) { return doc.a === 2 })
assert.isNull(err)
data.length.should.equal(2)
doc1.a.should.equal(1)
@ -182,8 +181,8 @@ describe('Persistence', function () {
d.loadDatabase(function (err) {
const data = d.getAllData()
const doc1 = _.find(data, function (doc) { return doc.a === 1 })
const doc2 = _.find(data, function (doc) { return doc.a === 2 })
const doc1 = data.find(function (doc) { return doc.a === 1 })
const doc2 = data.find(function (doc) { return doc.a === 2 })
assert.isNull(err)
data.length.should.equal(2)
doc1.a.should.equal(1)
@ -202,8 +201,8 @@ describe('Persistence', function () {
assert.isNull(err)
d.insert({ a: 2 }, function (err) {
const data = d.getAllData()
const doc1 = _.find(data, function (doc) { return doc.a === 1 })
const doc2 = _.find(data, function (doc) { return doc.a === 2 })
const doc1 = data.find(function (doc) { return doc.a === 1 })
const doc2 = data.find(function (doc) { return doc.a === 2 })
assert.isNull(err)
data.length.should.equal(2)
doc1.a.should.equal(1)
@ -229,8 +228,8 @@ describe('Persistence', function () {
assert.isNull(err)
d.insert({ a: 2 }, function (err) {
const data = d.getAllData()
const doc1 = _.find(data, function (doc) { return doc.a === 1 })
const doc2 = _.find(data, function (doc) { return doc.a === 2 })
const doc1 = data.find(function (doc) { return doc.a === 1 })
const doc2 = data.find(function (doc) { return doc.a === 2 })
assert.isNull(err)
data.length.should.equal(2)
doc1.a.should.equal(1)
@ -240,9 +239,9 @@ describe('Persistence', function () {
assert.isNull(err)
d.loadDatabase(function (err) {
const data = d.getAllData()
const doc1 = _.find(data, function (doc) { return doc.a === 1 })
const doc2 = _.find(data, function (doc) { return doc.a === 2 })
const doc3 = _.find(data, function (doc) { return doc.a === 3 })
const doc1 = data.find(function (doc) { return doc.a === 1 })
const doc2 = data.find(function (doc) { return doc.a === 2 })
const doc3 = data.find(function (doc) { return doc.a === 3 })
assert.isNull(err)
data.length.should.equal(1)
doc3.a.should.equal(3)
@ -422,7 +421,7 @@ describe('Persistence', function () {
doc1.p.should.equal('Mars')
idx = model.deserialize(idx)
assert.deepEqual(idx, { $$indexCreated: { fieldName: 'idefix' } })
assert.deepStrictEqual(idx, { $$indexCreated: { fieldName: 'idefix' } })
done()
})
@ -464,7 +463,7 @@ describe('Persistence', function () {
const _id = doc0._id
idx = model.deserialize(idx)
assert.deepEqual(idx, { $$indexCreated: { fieldName: 'idefix' } })
assert.deepStrictEqual(idx, { $$indexCreated: { fieldName: 'idefix' } })
d.persistence.persistCachedDatabase(function () {
const _data = fs.readFileSync(hookTestFilename, 'utf8')
@ -481,7 +480,7 @@ describe('Persistence', function () {
doc0._id.should.equal(_id)
idx = model.deserialize(idx)
assert.deepEqual(idx, { $$indexCreated: { fieldName: 'idefix', unique: false, sparse: false } })
assert.deepStrictEqual(idx, { $$indexCreated: { fieldName: 'idefix', unique: false, sparse: false } })
done()
})
@ -787,8 +786,8 @@ describe('Persistence', function () {
theDb.find({}, function (err, docs) {
assert.isNull(err)
docs.length.should.equal(2)
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello')
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world')
docs.find(function (item) { return item._id === doc1._id }).a.should.equal('hello')
docs.find(function (item) { return item._id === doc2._id }).a.should.equal('world')
return cb()
})
},
@ -799,8 +798,8 @@ describe('Persistence', function () {
theDb.find({}, function (err, docs) {
assert.isNull(err)
docs.length.should.equal(2)
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello')
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world')
docs.find(function (item) { return item._id === doc1._id }).a.should.equal('hello')
docs.find(function (item) { return item._id === doc2._id }).a.should.equal('world')
return cb()
})
},
@ -817,8 +816,8 @@ describe('Persistence', function () {
theDb2.find({}, function (err, docs) {
assert.isNull(err)
docs.length.should.equal(2)
_.find(docs, function (item) { return item._id === doc1._id }).a.should.equal('hello')
_.find(docs, function (item) { return item._id === doc2._id }).a.should.equal('world')
docs.find(function (item) { return item._id === doc1._id }).a.should.equal('hello')
docs.find(function (item) { return item._id === doc2._id }).a.should.equal('world')
return cb()
})
},
@ -872,9 +871,9 @@ describe('Persistence', function () {
db.find({}, function (err, docs) {
docs.length.should.equal(N)
for (i = 0; i < N; i += 1) {
docI = _.find(docs, function (d) { return d._id === 'anid_' + i })
docI = docs.find(function (d) { return d._id === 'anid_' + i })
assert.isDefined(docI)
assert.deepEqual({ hello: 'world', _id: 'anid_' + i }, docI)
assert.deepStrictEqual({ hello: 'world', _id: 'anid_' + i }, docI)
}
return done()
})

Loading…
Cancel
Save