Comparing version 7.2.2 to 7.3.0
@@ -16,3 +16,13 @@ 'use strict'; | ||
return table.filter(internals.compile(criteria)); | ||
// Optimize secondary index query | ||
if (typeof criteria === 'function' && | ||
criteria.type === 'by') { | ||
return table._table.getAll(RethinkDB.args(criteria.value), { index: criteria.flags.index }); | ||
} | ||
// Construct query | ||
return table._table.filter(internals.compile(criteria)); | ||
}; | ||
@@ -19,0 +29,0 @@ |
@@ -43,2 +43,16 @@ 'use strict'; | ||
internals.secondaryIndex = Joi.object({ | ||
name: Joi.string().required(), | ||
source: Joi.alternatives([ | ||
Joi.array().items(Joi.string()).min(2), // List of row fields | ||
Joi.func() // A function (row) => { } | ||
]), | ||
options: Joi.object({ | ||
multi: Joi.boolean().default(false), | ||
geo: Joi.boolean().default(false) | ||
}) | ||
.default({}) | ||
}); | ||
internals.schema = { | ||
@@ -72,3 +86,3 @@ db: Joi.object({ | ||
purge: Joi.boolean().default(true), | ||
secondary: Joi.array().items(Joi.string()).single().default([]).allow(false), | ||
secondary: Joi.array().items(Joi.string(), internals.secondaryIndex).single().default([]).allow(false), | ||
id: [ | ||
@@ -404,3 +418,3 @@ { | ||
const finalize = (err, indexes) => { | ||
const finalize = (err) => { | ||
@@ -411,7 +425,7 @@ if (err) { | ||
if (indexes) { | ||
return this.tables[name].index(indexes, next); | ||
if (!tableOptions.secondary) { | ||
return next(); | ||
} | ||
return next(); | ||
return this.tables[name].index(tableOptions.secondary, next); | ||
}; | ||
@@ -437,3 +451,3 @@ | ||
const change = (drop ? RethinkDB.and(drop, create) : create); | ||
return change.run(this._connection, (err) => finalize(err, tableOptions.secondary)); | ||
return change.run(this._connection, (err) => finalize(err)); | ||
} | ||
@@ -445,17 +459,8 @@ | ||
if (tableOptions.secondary === false) { // Defaults to [] | ||
if (tableOptions.secondary === false) { // false means leave as-is (vs null or empty array which drops existing) | ||
return finalize(); | ||
} | ||
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); | ||
const eachIndex = (index, nextIndex) => { | ||
RethinkDB.db(this.name).table(name).indexDrop(index).run(this._connection, nextIndex); | ||
}; | ||
Items.parallel(removeIndexes, eachIndex, (err) => finalize(err, createIndexes)); | ||
const eachIndex = (index, nextIndex) => RethinkDB.db(this.name).table(name).indexDrop(index).run(this._connection, nextIndex); | ||
Items.parallel(existingConfig.indexes, eachIndex, (err) => finalize(err)); | ||
}; | ||
@@ -542,2 +547,7 @@ | ||
static by(index, values) { | ||
return internals.special('by', [].concat(values), { index }); | ||
} | ||
// Criteria or Modifier | ||
@@ -577,17 +587,5 @@ | ||
internals.Db.prototype.is = internals.Db.is; | ||
internals.Db.prototype.by = internals.Db.by; | ||
internals.difference = function (superset, subset) { | ||
const result = []; | ||
for (let i = 0; i < superset.length; ++i) { | ||
if (subset.indexOf(superset[i]) === -1) { | ||
result.push(superset[i]); | ||
} | ||
} | ||
return result; | ||
}; | ||
internals.disable = function (table, method, options) { | ||
@@ -594,0 +592,0 @@ |
@@ -84,3 +84,3 @@ 'use strict'; | ||
let selection = Criteria.select(criteria, this._table); | ||
let selection = Criteria.select(criteria, this); | ||
@@ -110,3 +110,3 @@ if (options.sort) { | ||
this._run(Criteria.select(criteria, this._table), 'single', criteria, callback, (ignore, result) => { | ||
this._run(Criteria.select(criteria, this), 'single', criteria, callback, (ignore, result) => { | ||
@@ -127,3 +127,3 @@ if (!result) { | ||
this._run(Criteria.select(criteria, this._table).count(), 'count', { criteria }, callback); | ||
this._run(Criteria.select(criteria, this).count(), 'count', { criteria }, callback); | ||
} | ||
@@ -232,4 +232,4 @@ | ||
const selection = (isSingle ? this._table.get(criteria) | ||
: (Array.isArray(criteria) ? this._table.getAll(RethinkDB.args(criteria)) | ||
: this._table.filter(criteria))); | ||
: (Array.isArray(criteria) ? this._table.getAll(RethinkDB.args(criteria)) | ||
: this._table.filter(criteria))); | ||
@@ -272,14 +272,31 @@ this._run(selection.delete(), 'remove', criteria, callback, (ignore, result) => { | ||
index(names, callback) { | ||
index(indexes, callback) { | ||
names = [].concat(names); | ||
const each = (name, next) => { | ||
const names = []; | ||
const each = (index, next) => { | ||
this._run(this._table.indexCreate(name), 'index', null, (err, result) => { | ||
if (typeof index === 'string') { | ||
index = { name: index }; | ||
} | ||
return next(err); | ||
}); | ||
const { name, source, options } = index; | ||
names.push(name); | ||
const args = [name]; | ||
if (source) { | ||
args.push(Array.isArray(source) ? source.map((row) => RethinkDB.row(row)) : source); | ||
} | ||
args.push(options); | ||
this._run(this._table.indexCreate.apply(this._table, args), 'index', null, next); | ||
}; | ||
Items.parallel(names, each, callback); | ||
Items.parallel([].concat(indexes), each, (err) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
this._run(this._table.indexWait(RethinkDB.args(names)), 'indexWait', null, callback); | ||
}); | ||
} | ||
@@ -286,0 +303,0 @@ |
{ | ||
"name": "penseur", | ||
"description": "Lightweight RethinkDB wrapper", | ||
"version": "7.2.2", | ||
"version": "7.3.0", | ||
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)", | ||
@@ -23,3 +23,3 @@ "repository": "git://github.com/hueniverse/penseur", | ||
"devDependencies": { | ||
"code": "3.x.x", | ||
"code": "4.x.x", | ||
"lab": "11.x.x" | ||
@@ -26,0 +26,0 @@ }, |
56242
1451