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 4.0.1 to 4.1.0

112

lib/criteria.js

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

const Hoek = require('hoek');
const RethinkDB = require('rethinkdb');
// Declare internals

@@ -18,2 +21,6 @@

this.type = type || 'filter';
if (this.type === 'filter') {
this._compile();
}
}

@@ -34,2 +41,107 @@

}
static rule(type, values, options) {
const rule = function () { }; // Return function because 1. typeof fastest 2. illegal database type
rule.type = type;
rule.values = values;
rule.flags = Hoek.clone(options || {});
return rule;
}
_compile() {
/*
const criteria = {
a: {
b: 1
},
c: 2
};
const filter = Rethinkdb.and(Rethinkdb.row('a')('b').eq(1), Rethinkdb.row('c').eq(2));
*/
const lines = internals.flatten(this.criteria, []);
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));
}
else {
if (value.type === 'contains') {
// Contains
if (value.flags.keys) {
row = row.keys();
}
if (!Array.isArray(value.values)) {
tests.push(row.contains(value.values));
}
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)));
}
}
else {
// Or
const ors = [];
for (let j = 0; j < value.values.length; ++j) {
ors.push(row.eq(value.values[j]));
}
tests.push(RethinkDB.or(RethinkDB.args(ors)));
}
}
}
this.criteria = (tests.length === 1 ? tests[0] : RethinkDB.and(RethinkDB.args(tests)));
}
};
internals.flatten = function (criteria, path) {
const keys = Object.keys(criteria);
let lines = [];
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
const value = criteria[key];
const location = path.concat(key);
if (typeof value === 'object') {
lines = lines.concat(internals.flatten(value, location));
}
else {
lines.push({ path: location, value });
}
}
return lines;
};
internals.row = function (path) {
let row = RethinkDB.row(path[0]);
for (let i = 1; i < path.length; ++i) {
row = row(path[i]);
}
return row;
};

@@ -293,2 +293,12 @@ 'use strict';

or(values) {
return Criteria.rule('or', values);
}
contains(values, options) {
return Criteria.rule('contains', values, options);
}
increment(value) {

@@ -295,0 +305,0 @@

6

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

@@ -23,9 +23,9 @@ "repository": "git://github.com/hueniverse/penseur",

"code": "2.x.x",
"lab": "9.x.x"
"lab": "10.x.x"
},
"scripts": {
"test": "lab -a code -t 100 -L -m 5000",
"test-cov-html": "lab -a code -r html -o coverage.html"
"test-cov-html": "lab -a code -r html -o coverage.html -m 5000"
},
"license": "BSD-3-Clause"
}

@@ -26,15 +26,15 @@ 'use strict';

describe('get()', () => {
it('exposes table name', (done) => {
it('exposes table name', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
expect(db.test.name).to.equal('test');
done();
});
expect(err).to.not.exist();
expect(db.test.name).to.equal('test');
done();
});
});
describe('get()', () => {
it('fails on database error', (done) => {

@@ -68,3 +68,3 @@

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

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

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

@@ -171,2 +171,222 @@ });

});
it('returns the requested objects (empty criteria)', (done) => {
const db = new Penseur.Db('penseurtest');
db.establish(['test'], (err) => {
expect(err).to.not.exist();
db.test.insert([{ id: 1, a: 1 }, { id: 2, a: 2 }, { id: 3, a: 1 }], (err, keys) => {
expect(err).to.not.exist();
db.test.query({}, (err, result) => {
expect(err).to.not.exist();
expect(result.length).to.equal(3);
done();
});
});
});
});
it('returns the requested objects (multiple keys)', (done) => {
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();
});
});
});
});
it('returns the requested objects (nested keys)', (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: 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: 1 } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 3, a: 1, b: { c: 1 } }]);
done();
});
});
});
});
it('returns the requested objects (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: 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();
});
});
});
});
it('returns the requested objects (contains)', (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: { 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) } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 1, a: 1, b: { c: [1, 2] } }]);
done();
});
});
});
});
it('returns the requested objects (contains 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: { 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();
});
});
});
});
it('returns the requested objects (contains 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: { 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: 'and' }) } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 1, a: 1, b: { c: [1, 2] } }]);
done();
});
});
});
});
it('returns the requested objects (contains and default)', (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: { 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]) } }, (err, result) => {
expect(err).to.not.exist();
expect(result).to.deep.equal([{ id: 1, a: 1, b: { c: [1, 2] } }]);
done();
});
});
});
});
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();
});
});
});
});
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();
});
});
});
});
});

@@ -173,0 +393,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