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

penseur

Package Overview
Dependencies
Maintainers
1
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

penseur - npm Package Compare versions

Comparing version 5.3.1 to 6.0.1

test/criteria.js

167

lib/criteria.js

@@ -14,111 +14,115 @@ 'use strict';

exports = module.exports = internals.Criteria = class {
exports.select = function (criteria, table) {
constructor(criteria, type) {
return table.filter(internals.compile(criteria));
};
this.criteria = criteria;
this.type = type || 'filter';
if (this.type === 'filter') {
this._compile();
}
}
internals.compile = function (criteria, relative) {
select(table) {
/*
const criteria = {
a: {
b: 1
},
c: 2
};
return table[this.type === 'fields' ? 'hasFields' : 'filter'](this.criteria);
const filter = Rethinkdb.and(Rethinkdb.row('a')('b').eq(1), Rethinkdb.row('c').eq(2));
*/
const edges = [];
const lines = internals.flatten(criteria, relative || [], edges);
if (!lines.length) {
return criteria;
}
static wrap(criteria) {
const tests = [];
for (let i = 0; i < lines.length; ++i) {
const path = lines[i].path;
let row = exports.row(path);
const value = lines[i].value;
if (criteria instanceof internals.Criteria) {
return criteria;
}
if (typeof value === 'function') {
return new internals.Criteria(criteria);
}
// Special rule
static rule(type, values, options) {
Hoek.assert(['contains', 'not', 'or', 'unset'].indexOf(value.type) !== -1, `Unknown criteria value type ${value.type}`);
const rule = function () { }; // Return function because 1. typeof fastest 2. illegal database type
rule.type = type;
rule.values = values;
rule.flags = Hoek.clone(options || {});
if (value.type === 'contains') {
return rule;
}
// Contains
_compile() {
if (value.flags.keys ||
!path) {
/*
const criteria = {
a: {
b: 1
},
c: 2
};
row = row.keys();
}
const filter = Rethinkdb.and(Rethinkdb.row('a')('b').eq(1), Rethinkdb.row('c').eq(2));
*/
if (!Array.isArray(value.value)) {
tests.push(row.contains(value.value));
}
else {
const conditions = [];
for (let j = 0; j < value.value.length; ++j) {
conditions.push(row.contains(value.value[j]));
}
const edges = [];
const lines = internals.flatten(this.criteria, [], edges);
if (!lines.length) {
return;
}
const tests = [];
for (let i = 0; i < lines.length; ++i) {
let row = internals.row(lines[i].path);
const value = lines[i].value;
if (typeof value !== 'function') {
tests.push(row.eq(value));
tests.push(RethinkDB[value.flags.condition || 'and'].apply(RethinkDB, conditions));
}
}
else {
if (value.type === 'contains') {
else if (value.type === 'or') {
// Contains
// Or
if (value.flags.keys) {
row = row.keys();
const ors = [];
for (let j = 0; j < value.value.length; ++j) {
const orValue = value.value[j];
if (typeof orValue === 'function') {
Hoek.assert(orValue.type === 'unset', `Unknown or criteria value type ${orValue.type}`);
ors.push(exports.row(path.slice(0, -1)).hasFields(path[path.length - 1]).not());
}
if (!Array.isArray(value.values)) {
tests.push(row.contains(value.values));
else if (typeof orValue === 'object') {
ors.push(internals.compile(orValue, path));
}
else {
const conditions = [];
for (let j = 0; j < value.values.length; ++j) {
conditions.push(row.contains(value.values[j]));
}
tests.push(RethinkDB[value.flags.condition || 'and'](RethinkDB.args(conditions)));
ors.push(row.eq(orValue).default(null));
}
}
else {
// Or
let test = RethinkDB.or.apply(RethinkDB, ors);
if (value.flags.not) {
test = test.not();
}
const ors = [];
for (let j = 0; j < value.values.length; ++j) {
ors.push(row.eq(value.values[j]));
}
tests.push(test);
}
else {
tests.push(RethinkDB.or(RethinkDB.args(ors)));
}
// Unset
tests.push(exports.row(path.slice(0, -1)).hasFields(path[path.length - 1]).not());
}
}
else {
this.criteria = (tests.length === 1 ? tests[0] : RethinkDB.and(RethinkDB.args(tests)));
// Simple value
if (edges.length) {
let typeCheck = internals.row(edges[0]).typeOf().eq('OBJECT');
tests.push(row.eq(value).default(null));
}
}
for (let i = 1; i < edges.length; ++i) {
typeCheck = RethinkDB.and(typeCheck, internals.row(edges[i]).typeOf().eq('OBJECT'));
}
criteria = (tests.length === 1 ? tests[0] : RethinkDB.and.apply(RethinkDB, tests));
this.criteria = typeCheck.and(this.criteria);
if (edges.length) {
let typeCheck = exports.row(edges[0]).typeOf().eq('OBJECT');
for (let i = 1; i < edges.length; ++i) {
typeCheck = RethinkDB.and(typeCheck, exports.row(edges[i]).typeOf().eq('OBJECT'));
}
criteria = typeCheck.and(criteria);
}
return criteria;
};

@@ -129,2 +133,6 @@

if (typeof criteria === 'function') {
return [{ value: criteria }];
}
const keys = Object.keys(criteria);

@@ -150,4 +158,13 @@ let lines = [];

internals.row = function (path) {
exports.row = function (path) {
if (!path) {
return RethinkDB.row;
}
path = [].concat(path);
if (!path.length) {
return RethinkDB.row;
}
let row = RethinkDB.row(path[0]);

@@ -154,0 +171,0 @@ for (let i = 1; i < path.length; ++i) {

@@ -10,5 +10,3 @@ 'use strict';

const RethinkDB = require('rethinkdb');
const Criteria = require('./criteria');
const Id = require('./id');
const Modifier = require('./modifier');
const Table = require('./table');

@@ -69,3 +67,4 @@ const Unique = require('./unique');

],
unique: Joi.array().items(internals.unique).min(1).single()
unique: Joi.array().items(internals.unique).min(1).single(),
primary: Joi.string().max(127)
})

@@ -113,3 +112,3 @@ .allow(true, false)

this.verify((err) => {
this._verify((err) => {

@@ -332,3 +331,3 @@ if (err) {

return this.verify(callback);
return this._verify(callback);
});

@@ -363,3 +362,3 @@ };

RethinkDB.db(this.name).tableList().run(this._connection, (err, existing) => {
RethinkDB.db(this.name).tableList().map((table) => RethinkDB.db(this.name).table(table).config()).run(this._connection, (err, configs) => {

@@ -370,2 +369,8 @@ if (err) {

const existing = {};
configs.forEach((config) => {
existing[config.name] = config;
});
const each = (name, next) => {

@@ -395,6 +400,21 @@

// Check primary key
const primaryKey = tableOptions.primary || 'id';
const existingConfig = existing[name];
let drop = false;
if (existingConfig &&
existingConfig.primary_key !== primaryKey) {
drop = RethinkDB.db(this.name).tableDrop(name);
}
// Create new table
if (existing.indexOf(name) === -1) {
return RethinkDB.db(this.name).tableCreate(name).run(this._connection, (err) => finalize(err, tableOptions.secondary));
if (!existingConfig ||
drop) {
const create = RethinkDB.db(this.name).tableCreate(name, { primaryKey });
const change = (drop ? RethinkDB.and(drop, create) : create);
return change.run(this._connection, (err) => finalize(err, tableOptions.secondary));
}

@@ -410,20 +430,13 @@

RethinkDB.db(this.name).table(name).indexList().run(this._connection, (err, currentIndexes) => {
const requestedIndexes = [].concat(tableOptions.secondary || []);
const intersection = Hoek.intersect(existingConfig.indexes, requestedIndexes);
const createIndexes = internals.difference(requestedIndexes, intersection);
const removeIndexes = internals.difference(existingConfig.indexes, intersection);
if (err) {
return next(err);
}
const eachIndex = (index, nextIndex) => {
const requestedIndexes = [].concat(tableOptions.secondary || []);
const intersection = Hoek.intersect(currentIndexes, requestedIndexes);
const createIndexes = internals.difference(requestedIndexes, intersection);
const removeIndexes = internals.difference(currentIndexes, intersection);
RethinkDB.db(this.name).table(name).indexDrop(index).run(this._connection, nextIndex);
};
const eachIndex = (index, nextIndex) => {
RethinkDB.db(this.name).table(name).indexDrop(index).run(this._connection, nextIndex);
};
Items.parallel(removeIndexes, eachIndex, (err) => finalize(err, createIndexes));
});
Items.parallel(removeIndexes, eachIndex, (err) => finalize(err, createIndexes));
};

@@ -450,3 +463,3 @@

verify(callback) {
_verify(callback) {

@@ -469,10 +482,7 @@ const each = (name, next) => {

fields(criteria) {
// Criteria
return new Criteria(criteria, 'fields');
}
or(values) {
return Criteria.rule('or', values);
return internals.special('or', values);
}

@@ -482,23 +492,27 @@

return Criteria.rule('contains', values, options);
return internals.special('contains', values, options);
}
increment(value) {
not(values) {
return Modifier.type('increment', value);
return internals.special('or', [].concat(values), { not: true });
}
append(value, options) {
// Criteria or Modifier
return Modifier.type('append', value, options); // { single: false }
unset() {
return internals.special('unset');
}
unset() {
// Modifier
return Modifier.type('unset');
increment(value) {
return internals.special('increment', value);
}
isModifier(value) {
append(value, options) {
return (typeof value === 'function' && !!value.type);
return internals.special('append', value, options); // { single: false }
}

@@ -569,1 +583,17 @@ };

};
internals.special = function (type, value, options) {
options = options || {};
const special = function () { }; // Return function because 1. typeof fastest 2. illegal database type
special.type = type;
special.flags = Hoek.clone(options);
if (value !== undefined) {
special.value = value;
}
return special;
};

@@ -62,3 +62,3 @@ 'use strict';

if (item.id === undefined) {
if (item[table.primary] === undefined) {
item = Hoek.shallow(item);

@@ -83,3 +83,3 @@ identifiers.push(item);

item.id = id;
item[table.primary] = id;
return next();

@@ -86,0 +86,0 @@ });

@@ -14,18 +14,2 @@ 'use strict';

exports.type = function (type, value, options) {
options = options || {};
const modifier = function () { }; // Return function because 1. typeof fastest 2. illegal database type
modifier.type = type;
modifier.flags = Hoek.clone(options);
if (value !== undefined) {
modifier.value = value;
}
return modifier;
};
exports.wrap = function (changes) {

@@ -32,0 +16,0 @@

@@ -18,3 +18,11 @@ 'use strict';

const internals = {};
const internals = {
changeTypes: {
add: 'insert',
remove: 'remove',
change: 'update',
initial: 'initial'
// uninitial: can only happen in sorted requests which are not supported
}
};

@@ -27,2 +35,3 @@

this.name = name;
this.primary = options.primary || 'id';
this._db = db;

@@ -56,5 +65,26 @@ this._id = Id.compile(this, options.id);

query(criteria, callback) {
query(criteria, options, callback) {
this._run(Criteria.wrap(criteria).select(this._table), 'query', criteria, callback);
if (!callback) {
callback = options;
options = {};
}
let selection = Criteria.select(criteria, this._table);
if (options.sort) {
const sort = (typeof options.sort === 'string' || Array.isArray(options.sort) ? { key: options.sort } : options.sort);
const order = (sort.order === 'descending' ? 'desc' : 'asc');
selection = selection.orderBy(RethinkDB[order](Criteria.row(sort.key)));
}
if (options.from) {
selection = selection.skip(options.from);
}
if (options.count) {
selection = selection.limit(options.count);
}
this._run(selection, 'query', { criteria, options }, callback);
}

@@ -64,3 +94,3 @@

this._run(Criteria.wrap(criteria).select(this._table), 'single', criteria, callback, (ignore, result) => {
this._run(Criteria.select(criteria, this._table), 'single', criteria, callback, (ignore, result) => {

@@ -81,3 +111,3 @@ if (!result) {

this._run(Criteria.wrap(criteria).select(this._table).count(), 'count', { criteria: criteria }, callback);
this._run(Criteria.select(criteria, this._table).count(), 'count', { criteria: criteria }, callback);
}

@@ -104,3 +134,3 @@

if (!Array.isArray(wrapped)) {
return callback(null, wrapped.id !== undefined ? wrapped.id : result.generated_keys[0]);
return callback(null, wrapped[this.primary] !== undefined ? wrapped[this.primary] : result.generated_keys[0]);
}

@@ -120,4 +150,4 @@

for (let i = 0; i < wrapped.length; ++i) {
if (wrapped[i].id !== undefined) {
ids.push(wrapped[i].id);
if (wrapped[i][this.primary] !== undefined) {
ids.push(wrapped[i][this.primary]);
}

@@ -264,4 +294,10 @@ else {

request.changes().run(this._db._connection, { includeStates: false, includeInitial: options.initial || false }, (err, dbCursor) => {
const settings = {
includeTypes: true,
includeStates: false,
includeInitial: options.initial || false
};
request.changes().run(this._db._connection, settings, (err, dbCursor) => {
if (err) {

@@ -272,46 +308,41 @@ return this._error('changes', err, criteria, callback);

const cursor = new Cursor(dbCursor, this, feedId);
const each = (item, next) => {
dbCursor.each((err, item) => {
const type = internals.changeTypes[item.type];
if (type === 'initial' &&
item.new_val === null) {
if (err) {
const disconnected = (err.msg === 'Connection is closed.');
const willReconnect = (disconnected && feedId && this._db._willReconnect()) || false;
cursor.close(false);
return next(); // Initial result for missing id
}
if (feedId &&
!willReconnect) {
const update = {
id: item.old_val ? item.old_val[this.primary] : item.new_val[this.primary],
type,
before: item.old_val || null,
after: item.new_val || null
};
delete this._db._feeds[feedId];
}
options.handler(null, update);
return next();
};
return this._error('changes', err, criteria, options.handler, { disconnected, willReconnect });
}
dbCursor.eachAsync(each, (err) => {
if (item.new_val === null &&
item.old_val === undefined) {
// Changes cursor ends only with an error
return; // Initial result for missing id
if (err.msg === 'Cursor is closed.') {
return;
}
const update = {
id: item.old_val ? item.old_val.id : item.new_val.id,
type: 'update',
before: item.old_val || null,
after: item.new_val || null
};
const disconnected = (err.msg === 'Connection is closed.');
const willReconnect = (disconnected && feedId && this._db._willReconnect()) || false;
cursor.close(false);
// If item.new_val is undefined, it means the item is "uninitial" which can
// only happen in sorted requests which are not supported at this point.
if (feedId &&
!willReconnect) {
if (item.old_val === undefined) {
update.type = 'initial';
delete this._db._feeds[feedId];
}
else if (item.old_val === null) {
update.type = 'insert';
}
else if (item.new_val === null) {
update.type = 'remove';
}
return options.handler(null, update);
return this._error('changes', err, criteria, options.handler, { disconnected, willReconnect });
});

@@ -347,3 +378,5 @@

if (typeof result.toArray !== 'function') {
if (typeof result.toArray !== 'function' ||
Array.isArray(result)) {
return next(null, result);

@@ -350,0 +383,0 @@ }

@@ -71,3 +71,3 @@ 'use strict';

if (values.length) {
reserve.push({ rule, values, id: updateId !== null ? updateId : item.id });
reserve.push({ rule, values, id: updateId !== null ? updateId : item[table.primary] });
}

@@ -74,0 +74,0 @@

{
"name": "penseur",
"description": "Lightweight RethinkDB wrapper",
"version": "5.3.1",
"version": "6.0.1",
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)",

@@ -20,6 +20,6 @@ "repository": "git://github.com/hueniverse/penseur",

"radix62": "1.x.x",
"rethinkdb": "2.2.x"
"rethinkdb": "2.3.x"
},
"devDependencies": {
"code": "2.x.x",
"code": "3.x.x",
"lab": "10.x.x"

@@ -26,0 +26,0 @@ },

@@ -350,3 +350,3 @@ 'use strict';

expect(err).to.not.exist();
expect(result).to.deep.equal(['other']);
expect(result).to.equal(['other']);
db.close(done);

@@ -468,3 +468,3 @@ });

expect(err).to.not.exist();
expect(result1).to.deep.equal(['other']);
expect(result1).to.equal(['other']);
db1.close(() => {

@@ -483,3 +483,3 @@

expect(err).to.not.exist();
expect(result2).to.deep.equal([]);
expect(result2).to.equal([]);
db2.close(done);

@@ -509,3 +509,3 @@ });

expect(err).to.not.exist();
expect(result1).to.deep.equal(['other']);
expect(result1).to.equal(['other']);
db1.close(() => {

@@ -524,3 +524,3 @@

expect(err).to.not.exist();
expect(result2).to.deep.equal(['other']);
expect(result2).to.equal(['other']);
db2.close(done);

@@ -549,3 +549,3 @@ });

expect(err).to.not.exist();
expect(result1).to.deep.equal(['a', 'b']);
expect(result1).to.equal(['a', 'b']);
db1.close(() => {

@@ -564,3 +564,3 @@

expect(err).to.not.exist();
expect(result2).to.deep.equal(['b', 'c']);
expect(result2).to.equal(['b', 'c']);
db2.close(done);

@@ -658,48 +658,2 @@ });

it('errors on database indexList() error', { parallel: false }, (done) => {
const db = new Penseur.Db('penseurtest');
const orig = RethinkDB.db;
let count = 0;
RethinkDB.db = function () {
return {
tableList: function () {
return {
run: function (connection, next) {
return next(null, ['test']);
}
};
},
table: function () {
if (++count === 1) {
return orig('penseurtest').table('test');
}
RethinkDB.db = orig;
return {
indexList: function () {
return {
run: function (connection, next) {
return next(new Error('Bad database'));
}
};
}
};
}
};
};
db.establish(['test'], (err) => {
expect(err).to.exist();
db.close(done);
});
});
it('errors creating new table', (done) => {

@@ -737,2 +691,21 @@

it('creates table with custom primary key', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish({ test: { primary: 'other', id: 'uuid' } }, (err) => {
expect(err).to.not.exist();
db.test.insert({ a: 1 }, (err, id) => {
expect(err).to.not.exist();
db.test.get(id, (err, item) => {
expect(err).to.not.exist();
expect(item).to.equal({ other: id, a: 1 });
db.close(done);
});
});
});
});
it('errors on database tableList() error', { parallel: false }, (done) => {

@@ -757,5 +730,10 @@

return {
run: function (connection, next) {
map: function () {
return next(new Error('Bad database'));
return {
run: function (connection, next) {
return next(new Error('Bad database'));
}
};
}

@@ -775,29 +753,4 @@ };

describe('verify()', () => {
describe('_verify()', () => {
it('prepares generate id table', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish({ allocate: true, test: { id: { type: 'increment', table: 'allocate' } } }, (err) => {
expect(err).to.not.exist();
db.verify((err) => {
expect(err).to.not.exist();
db.allocate.get('test', (err, record) => {
expect(err).to.not.exist();
expect(record.value).to.equal(0);
db.test.insert({ a: 1 }, (err, keys) => {
expect(err).to.not.exist();
expect(keys).to.equal('1');
done();
});
});
});
});
});
it('errors on create table error (id)', (done) => {

@@ -1005,14 +958,2 @@

});
describe('isModifier()', () => {
it('indicates if a value is a modifier', (done) => {
const db = new Penseur.Db('penseurtest');
expect(db.isModifier(1)).to.be.false();
expect(db.isModifier(() => { })).to.be.false();
expect(db.isModifier(db.increment(1))).to.be.true();
done();
});
});
});

@@ -52,3 +52,3 @@ 'use strict';

expect(err).to.not.exist();
expect(keys).to.deep.equal(['abc', 'def']);
expect(keys).to.equal(['abc', 'def']);
done();

@@ -134,3 +134,3 @@ });

expect(err).to.not.exist();
expect(keys).to.deep.equal(['1', '2']);
expect(keys).to.equal(['1', '2']);
done();

@@ -137,0 +137,0 @@ });

@@ -68,3 +68,3 @@ 'use strict';

expect(err).to.not.exist();
expect(updated).to.deep.equal({
expect(updated).to.equal({
id: 1,

@@ -127,3 +127,3 @@ a: 2,

expect(err).to.not.exist();
expect(updated).to.deep.equal({
expect(updated).to.equal({
id: 1,

@@ -177,3 +177,3 @@ a: 2,

expect(err).to.not.exist();
expect(updated).to.deep.equal({
expect(updated).to.equal({
id: 1,

@@ -180,0 +180,0 @@ b: {}

@@ -67,3 +67,3 @@ 'use strict';

expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1 }, { id: 1, a: 1 }]);
expect(result).to.equal([{ id: 3, a: 1 }, { id: 1, a: 1 }]);
done();

@@ -88,3 +88,3 @@ });

expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 1, a: 1 }]);
expect(result).to.equal([{ id: 1, a: 1 }]);
done();

@@ -212,3 +212,3 @@ });

expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1 }, { id: 1, a: 1 }]);
expect(result).to.equal([{ id: 3, a: 1 }, { id: 1, a: 1 }]);
done();

@@ -220,3 +220,3 @@ });

it('returns the requested objects (empty criteria)', (done) => {
it('sorts the requested objects (key)', (done) => {

@@ -227,31 +227,16 @@ const db = new Penseur.Db('penseurtest');

expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1 }, { id: 2, a: 2 }, { id: 3, a: 1 }], (err, keys) => {
db.test.insert([{ id: 1, a: 1, b: 1 }, { id: 2, a: 2, b: 1 }, { id: 3, a: 3, b: 1 }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ b: 1 }, {}, (err, result1) => {
db.test.query({}, (err, result) => {
expect(err).to.not.exist();
expect(result.length).to.equal(3);
done();
});
});
});
});
expect(result1).to.equal([{ id: 3, a: 3, b: 1 }, { id: 2, a: 2, b: 1 }, { id: 1, a: 1, b: 1 }]);
it('returns the requested objects (multiple keys)', (done) => {
db.test.query({ b: 1 }, { sort: 'a' }, (err, result2) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: 2 }, { id: 2, a: 2, b: 1 }, { id: 3, a: 1, b: 1 }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ a: 1, b: 1 }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1, b: 1 }]);
done();
expect(err).to.not.exist();
expect(result2).to.equal([{ id: 1, a: 1, b: 1 }, { id: 2, a: 2, b: 1 }, { id: 3, a: 3, b: 1 }]);
done();
});
});

@@ -262,3 +247,3 @@ });

it('returns the requested objects (nested keys)', (done) => {
it('sorts the requested objects (nested key)', (done) => {

@@ -269,31 +254,16 @@ const db = new Penseur.Db('penseurtest');

expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: { c: 2 } }, { id: 2, a: 2, b: { c: 1 } }, { id: 3, a: 1, b: { c: 1 } }], (err, keys) => {
db.test.insert([{ id: 1, x: { a: 1 }, b: 1 }, { id: 2, x: { a: 2 }, b: 1 }, { id: 3, x: { a: 3 }, b: 1 }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ b: 1 }, {}, (err, result1) => {
db.test.query({ a: 1, b: { c: 1 } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1, b: { c: 1 } }]);
done();
});
});
});
});
expect(result1).to.equal([{ id: 3, x: { a: 3 }, b: 1 }, { id: 2, x: { a: 2 }, b: 1 }, { id: 1, x: { a: 1 }, b: 1 }]);
it('returns the requested objects (or)', (done) => {
db.test.query({ b: 1 }, { sort: ['x', 'a'] }, (err, result2) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: { c: 2 } }, { id: 2, a: 2, b: { c: 1 } }, { id: 3, a: 1, b: { c: 1 } }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ a: 1, b: { c: db.or([1, 2]) } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1, b: { c: 1 } }, { id: 1, a: 1, b: { c: 2 } }]);
done();
expect(err).to.not.exist();
expect(result2).to.equal([{ id: 1, x: { a: 1 }, b: 1 }, { id: 2, x: { a: 2 }, b: 1 }, { id: 3, x: { a: 3 }, b: 1 }]);
done();
});
});

@@ -304,3 +274,3 @@ });

it('returns the requested objects (contains)', (done) => {
it('sorts the requested objects (object)', (done) => {

@@ -311,31 +281,16 @@ const db = new Penseur.Db('penseurtest');

expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: { c: [1, 2] } }, { id: 2, a: 2, b: { c: [3, 4] } }, { id: 3, a: 1, b: { c: [2, 3] } }], (err, keys) => {
db.test.insert([{ id: 1, a: 1, b: 1 }, { id: 2, a: 2, b: 1 }, { id: 3, a: 3, b: 1 }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ b: 1 }, {}, (err, result1) => {
db.test.query({ a: 1, b: { c: db.contains(1) } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 1, a: 1, b: { c: [1, 2] } }]);
done();
});
});
});
});
expect(result1).to.equal([{ id: 3, a: 3, b: 1 }, { id: 2, a: 2, b: 1 }, { id: 1, a: 1, b: 1 }]);
it('returns the requested objects (contains or)', (done) => {
db.test.query({ b: 1 }, { sort: { key: 'a', order: 'descending' } }, (err, result2) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: { c: [1, 2] } }, { id: 2, a: 2, b: { c: [3, 4] } }, { id: 3, a: 1, b: { c: [2, 3] } }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ a: 1, b: { c: db.contains([1, 2], { condition: 'or' }) } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1, b: { c: [2, 3] } }, { id: 1, a: 1, b: { c: [1, 2] } }]);
done();
expect(err).to.not.exist();
expect(result2).to.equal([{ id: 3, a: 3, b: 1 }, { id: 2, a: 2, b: 1 }, { id: 1, a: 1, b: 1 }]);
done();
});
});

@@ -346,3 +301,3 @@ });

it('returns the requested objects (contains and)', (done) => {
it('includes results from a given position', (done) => {

@@ -353,10 +308,9 @@ const db = new Penseur.Db('penseurtest');

expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: { c: [1, 2] } }, { id: 2, a: 2, b: { c: [3, 4] } }, { id: 3, a: 1, b: { c: [2, 3] } }], (err, keys) => {
db.test.insert([{ id: 1, a: 1, b: 1 }, { id: 2, a: 2, b: 1 }, { id: 3, a: 3, b: 1 }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ b: 1 }, { sort: 'a', from: 1 }, (err, result) => {
db.test.query({ a: 1, b: { c: db.contains([1, 2], { condition: 'and' }) } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 1, a: 1, b: { c: [1, 2] } }]);
expect(result).to.equal([{ id: 2, a: 2, b: 1 }, { id: 3, a: 3, b: 1 }]);
done();

@@ -368,3 +322,3 @@ });

it('returns the requested objects (contains and default)', (done) => {
it('includes n number of results', (done) => {

@@ -375,31 +329,15 @@ const db = new Penseur.Db('penseurtest');

expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: { c: [1, 2] } }, { id: 2, a: 2, b: { c: [3, 4] } }, { id: 3, a: 1, b: { c: [2, 3] } }], (err, keys) => {
db.test.insert([{ id: 1, a: 1, b: 1 }, { id: 2, a: 2, b: 1 }, { id: 3, a: 3, b: 1 }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ b: 1 }, { sort: 'a', count: 2 }, (err, result1) => {
db.test.query({ a: 1, b: { c: db.contains([1, 2]) } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 1, a: 1, b: { c: [1, 2] } }]);
done();
});
});
});
});
expect(result1).to.equal([{ id: 1, a: 1, b: 1 }, { id: 2, a: 2, b: 1 }]);
db.test.query({ b: 1 }, { sort: 'a', from: 1, count: 1 }, (err, result2) => {
it('returns the requested objects (contains key)', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: { c: [1, 2] } }, { id: 2, a: 2, b: { d: [3, 4] } }, { id: 3, a: 1, b: { e: [2, 3] } }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ a: 1, b: db.contains('c', { keys: true }) }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 1, a: 1, b: { c: [1, 2] } }]);
done();
expect(err).to.not.exist();
expect(result2).to.equal([{ id: 2, a: 2, b: 1 }]);
done();
});
});

@@ -409,86 +347,2 @@ });

});
it('returns the requested objects (contains keys or)', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: { c: [1, 2] } }, { id: 2, a: 2, b: { d: [3, 4] } }, { id: 3, a: 1, b: { e: [2, 3] } }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ a: 1, b: db.contains(['c', 'e'], { keys: true, condition: 'or' }) }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1, b: { e: [2, 3] } }, { id: 1, a: 1, b: { c: [1, 2] } }]);
done();
});
});
});
});
it('returns the requested objects (contains keys and)', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: { c: [1, 2] } }, { id: 2, a: 2, b: { d: [3, 4] } }, { id: 3, a: 1, b: { e: [2, 3], f: 'x' } }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ a: 1, b: db.contains(['f', 'e'], { keys: true, condition: 'and' }) }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1, b: { e: [2, 3], f: 'x' } }]);
done();
});
});
});
});
it('handles query into nested object where item is not an object', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1, b: false }, { id: 2, a: 2, b: { c: 1 } }, { id: 3, a: 1, b: { c: 1 } }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ a: 1, b: { c: 1 } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1, b: { c: 1 } }]);
done();
});
});
});
});
it('handles query into double nested object where item is not an object', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
db.test.insert([
{ id: 1, a: 1, b: false },
{ id: 2, a: 2, b: { c: { d: 4 } } },
{ id: 3, a: 1, b: { c: 1 } }
], (err, keys) => {
expect(err).to.not.exist();
db.test.query({ b: { c: { d: 4 } } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 2, a: 2, b: { c: { d: 4 } } }]);
done();
});
});
});
});
});

@@ -511,3 +365,3 @@

expect(err).to.not.exist();
expect(result).to.deep.equal({ id: 2, a: 2 });
expect(result).to.equal({ id: 2, a: 2 });
done();

@@ -592,3 +446,3 @@ });

db.test.count(db.fields(['a']), (err, result) => {
db.test.count(db.contains('a'), (err, result) => {

@@ -780,3 +634,3 @@ expect(err).to.not.exist();

expect(err).to.not.exist();
expect(item.a).to.deep.equal({});
expect(item.a).to.equal({});
done();

@@ -806,3 +660,3 @@ });

expect(err).to.not.exist();
expect(item.a.b).to.deep.equal({});
expect(item.a.b).to.equal({});
done();

@@ -867,3 +721,3 @@ });

expect(err).to.not.exist();
expect(updated).to.deep.equal({
expect(updated).to.equal({
id: 1,

@@ -919,3 +773,3 @@ a: 2,

expect(err).to.not.exist();
expect(updated).to.deep.equal({
expect(updated).to.equal({
id: 1,

@@ -971,3 +825,3 @@ a: 2,

expect(err).to.not.exist();
expect(updated).to.deep.equal({
expect(updated).to.equal({
id: 1,

@@ -1023,3 +877,3 @@ a: 2,

expect(err).to.not.exist();
expect(updated).to.deep.equal({
expect(updated).to.equal({
id: 1,

@@ -1075,3 +929,3 @@ a: 2,

expect(err).to.not.exist();
expect(updated).to.deep.equal({
expect(updated).to.equal({
id: 1,

@@ -1115,3 +969,3 @@ a: 2,

expect(err).to.not.exist();
expect(updated).to.deep.equal({ id: 1 });
expect(updated).to.equal({ id: 1 });
done();

@@ -1145,3 +999,3 @@ });

expect(err).to.not.exist();
expect(updated).to.deep.equal({ id: 1, a: 1 });
expect(updated).to.equal({ id: 1, a: 1 });
done();

@@ -1186,3 +1040,3 @@ });

expect(err).to.not.exist();
expect(updated).to.deep.equal({
expect(updated).to.equal({
id: 1,

@@ -1475,3 +1329,3 @@ a: [1, 2],

expect(changes).to.deep.equal([1, 1]);
expect(changes).to.equal([1, 1]);
db.close(done);

@@ -1510,3 +1364,3 @@ });

expect(changes).to.deep.equal([1, 1]);
expect(changes).to.equal([1, 1]);
cursor.close();

@@ -1550,3 +1404,3 @@ db.close(done);

expect(changes).to.deep.equal([1]);
expect(changes).to.equal([1]);
db.close(done);

@@ -1590,3 +1444,3 @@ });

expect(changes).to.deep.equal([1, 2]);
expect(changes).to.equal([1, 2]);
db.close(done);

@@ -1630,3 +1484,3 @@ });

expect(changes).to.deep.equal([1, 2]);
expect(changes).to.equal([1, 2]);
db.close(done);

@@ -1665,3 +1519,3 @@ });

expect(err).to.not.exist();
expect(changes).to.deep.equal(['1:true']);
expect(changes).to.equal(['1:true']);
db.close(done);

@@ -1695,3 +1549,3 @@ });

expect(err).to.not.exist();
expect(changes).to.deep.equal([1]);
expect(changes).to.equal([1]);
db.close(done);

@@ -1733,3 +1587,3 @@ });

expect(changes).to.deep.equal([1, 1]);
expect(changes).to.equal([1, 1]);
db.close(done);

@@ -1749,5 +1603,46 @@ });

expect(err).to.not.exist();
db.test.changes('1', { handler: Hoek.ignore, initial: true }, (err, cursor) => {
const changes = [];
const each = (err, item) => {
expect(err).to.not.exist();
changes.push(item.id);
};
db.test.changes(1, { handler: each, initial: true }, (err, cursor) => {
expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1 }], (err, keys1) => {
expect(err).to.not.exist();
db.test.update(1, { a: 2 }, (err, keys2) => {
expect(err).to.not.exist();
db.test.insert({ id: 2, a: 2 }, (err) => {
expect(err).to.not.exist();
expect(changes).to.equal([1, 1]);
db.close(done);
});
});
});
});
});
});
it('handles closed cursor while still processing rows', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
const each = (err, item) => {
expect(err).to.not.exist();
};
db.test.changes(1, { handler: each, initial: true }, (err, cursor) => {
expect(err).to.not.exist();
db.close(done);

@@ -1782,3 +1677,3 @@ });

expect(changes).to.deep.equal(['insert', { willReconnect: true, disconnected: true }, 'initial']);
expect(changes).to.equal(['insert', { willReconnect: true, disconnected: true }, 'initial']);
expect(count).to.equal(2);

@@ -1855,3 +1750,3 @@ db.close(done);

expect(err).to.not.exist();
expect(changes).to.deep.equal(['insert']);
expect(changes).to.equal(['insert']);
expect(count).to.equal(2);

@@ -1912,3 +1807,3 @@ db.close(done);

expect(err).to.not.exist();
expect(changes).to.deep.equal(['insert', { willReconnect: false, disconnected: true }]);
expect(changes).to.equal(['insert', { willReconnect: false, disconnected: true }]);
expect(count).to.equal(2);

@@ -1957,5 +1852,8 @@ db.close(done);

expect(err).to.not.exist();
expect(changes).to.deep.equal(['insert', { willReconnect: false, disconnected: true }]);
expect(count).to.equal(1);
db.close(done);
setTimeout(() => {
expect(changes).to.equal(['insert', { willReconnect: false, disconnected: true }]);
expect(count).to.equal(1);
db.close(done);
}, 100);
});

@@ -1962,0 +1860,0 @@ });

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc