Comparing version 8.7.2 to 8.7.3
@@ -29,4 +29,30 @@ 'use strict'; | ||
exports.normalize = function (ids, allowArray) { | ||
exports.is = function (id) { | ||
if (id === null || | ||
id === undefined) { | ||
return false; | ||
} | ||
if (typeof id !== 'object') { | ||
return true; | ||
} | ||
return Object.keys(id).length === 1 && id.id !== undefined; | ||
}; | ||
exports.extract = function (items, table) { | ||
return items.map((item) => { | ||
const id = item[table.primary]; | ||
return (typeof id === 'object' ? { id } : id); | ||
}); | ||
}; | ||
exports.normalize = function (ids, { allowArray }) { | ||
if (!Array.isArray(ids)) { | ||
@@ -44,9 +70,3 @@ return internals.validate(ids); | ||
const normalized = []; | ||
for (let i = 0; i < ids.length; ++i) { | ||
const id = internals.validate(ids[i]); | ||
normalized.push(id); | ||
} | ||
return normalized; | ||
return ids.map(internals.validate); | ||
}; | ||
@@ -57,6 +77,12 @@ | ||
if (id && // Id expressed as { id } (unrelated to the id field name of the table) | ||
typeof id === 'object') { | ||
if (id === null || | ||
id === undefined) { | ||
if (id.id === undefined) { | ||
throw new Boom('Invalid null or undefined id'); | ||
} | ||
if (typeof id === 'object') { // Id expressed as { id } (unrelated to the id field name of the table) | ||
if (id.id === undefined || | ||
Object.keys(id).length > 1) { | ||
throw new Boom('Invalid object id'); | ||
@@ -68,8 +94,2 @@ } | ||
if (id === null || | ||
id === undefined) { | ||
throw new Boom('Invalid null or undefined id'); | ||
} | ||
if (typeof id === 'string' && | ||
@@ -76,0 +96,0 @@ id.length > 127) { |
@@ -16,2 +16,18 @@ 'use strict'; | ||
const pairs = {}; | ||
if (Array.isArray(changes)) { | ||
for (const change of changes) { | ||
const id = JSON.stringify(change[table.primary]); | ||
const existing = pairs[id]; | ||
if (!existing) { | ||
pairs[id] = change; | ||
} | ||
else { | ||
const base = Hoek.clone(existing); | ||
pairs[id] = Hoek.merge(base, change, true, false); | ||
} | ||
} | ||
} | ||
return function (item) { | ||
@@ -44,8 +60,6 @@ | ||
const pairs = {}; | ||
changes.forEach((change) => { | ||
for (const id in pairs) { | ||
pairs[id] = each(pairs[id]); | ||
} | ||
pairs[change[table.primary]] = each(change); | ||
}); | ||
return RethinkDB.expr(pairs)(item(table.primary).coerceTo('string')); | ||
@@ -52,0 +66,0 @@ }; |
@@ -52,3 +52,3 @@ 'use strict'; | ||
const batch = Array.isArray(ids); | ||
ids = Id.normalize(ids, true); | ||
ids = Id.normalize(ids, { allowArray: true }); | ||
const query = (batch ? this.raw.getAll(RethinkDB.args(ids)) : this.raw.get(ids)); | ||
@@ -109,3 +109,3 @@ const items = await this._db._run(this._refine(query, options), track); | ||
try { | ||
id = Id.normalize(id, false); | ||
id = Id.normalize(id, { allowArray: false }); | ||
return await this._db._run(this.raw.get(id).ne(null), track); | ||
@@ -297,3 +297,3 @@ } | ||
const updates = ids; | ||
return this._update(updates.map((item) => item[this.primary]), updates, track); | ||
return this._update(Id.extract(updates, this), updates, track); | ||
} | ||
@@ -303,3 +303,3 @@ | ||
if (typeof ids[0] !== 'object') { | ||
if (Id.is(ids[0])) { | ||
Hoek.assert(!Array.isArray(changes), 'Changes cannot be an array when ids is an array'); | ||
@@ -317,4 +317,3 @@ return this._update(ids, changes, track); | ||
ids = changes.map((item) => item[this.primary]); | ||
return this._update(ids, changes, track); | ||
return this._update(Id.extract(changes, this), changes, track); | ||
} | ||
@@ -333,3 +332,3 @@ | ||
const batch = batches[i]; | ||
const bIds = batch.map((item) => item[this.primary]); | ||
const bIds = Id.extract(batch, this); | ||
await this._update(bIds, batch, track); | ||
@@ -346,7 +345,10 @@ | ||
// (id, changes) | ||
if (!Array.isArray(ids)) { | ||
return [changes]; | ||
} | ||
// ([ids], changes) | ||
if (!Array.isArray(ids) || | ||
(ids[0] && typeof ids[0] !== 'object')) { | ||
if (Id.is(ids[0])) { | ||
return [changes]; | ||
@@ -365,3 +367,3 @@ } | ||
const batch = Array.isArray(ids); | ||
ids = Id.normalize(ids, true); | ||
ids = Id.normalize(ids, { allowArray: true }); | ||
@@ -391,3 +393,3 @@ const postUnique = await Unique.reserve(this, changes, (batch ? true : ids)); | ||
id = Id.normalize(id, false); | ||
id = Id.normalize(id, { allowArray: false }); | ||
const result = await this._db._run(this.raw.get(id).update(changes, { returnChanges: true }), track); | ||
@@ -414,3 +416,3 @@ if (!result.replaced) { | ||
if (isIds) { | ||
criteria = Id.normalize(criteria, true); | ||
criteria = Id.normalize(criteria, { allowArray: true }); | ||
} | ||
@@ -417,0 +419,0 @@ |
{ | ||
"name": "penseur", | ||
"description": "Lightweight RethinkDB wrapper", | ||
"version": "8.7.2", | ||
"version": "8.7.3", | ||
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/hueniverse/penseur", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74786
1845