js-data-rethinkdb
Advanced tools
Comparing version 2.2.0 to 3.0.0-alpha.1
@@ -0,1 +1,12 @@ | ||
##### 3.0.0-alpha.1 - 26 February 2016 | ||
###### Breaking API changes | ||
- Now depends on js-data 3.x | ||
- Now longer uses internal `defaults` property, settings are on the adapter instance itself | ||
###### Backwards compatible API changes | ||
- Added createMany and updateMany methods | ||
- Added lifecycle hooks for all methods | ||
- Added contains, notContains, |contains, and |notContains operators | ||
##### 2.2.0 - 25 February 2016 | ||
@@ -2,0 +13,0 @@ |
'use strict'; | ||
var jsData = require('js-data'); | ||
var babelHelpers = {}; | ||
@@ -10,8 +12,2 @@ babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
babelHelpers.classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
babelHelpers.defineProperty = function (obj, key, value) { | ||
@@ -35,15 +31,12 @@ if (key in obj) { | ||
var rethinkdbdash = require('rethinkdbdash'); | ||
var JSData = require('js-data'); | ||
var DSUtils = JSData.DSUtils; | ||
var upperCase = DSUtils.upperCase; | ||
var contains = DSUtils.contains; | ||
var forOwn = DSUtils.forOwn; | ||
var isEmpty = DSUtils.isEmpty; | ||
var keys = DSUtils.keys; | ||
var deepMixIn = DSUtils.deepMixIn; | ||
var forEach = DSUtils.forEach; | ||
var isObject = DSUtils.isObject; | ||
var isString = DSUtils.isString; | ||
var removeCircular = DSUtils.removeCircular; | ||
var omit = DSUtils.omit; | ||
var addHiddenPropsToTarget = jsData.utils.addHiddenPropsToTarget; | ||
var fillIn = jsData.utils.fillIn; | ||
var forEachRelation = jsData.utils.forEachRelation; | ||
var forOwn = jsData.utils.forOwn; | ||
var get = jsData.utils.get; | ||
var isArray = jsData.utils.isArray; | ||
var isObject = jsData.utils.isObject; | ||
var isString = jsData.utils.isString; | ||
var isUndefined = jsData.utils.isUndefined; | ||
var resolve = jsData.utils.resolve; | ||
@@ -55,20 +48,2 @@ | ||
var addHiddenPropsToTarget = function addHiddenPropsToTarget(target, props) { | ||
DSUtils.forOwn(props, function (value, key) { | ||
props[key] = { | ||
writable: true, | ||
value: value | ||
}; | ||
}); | ||
Object.defineProperties(target, props); | ||
}; | ||
var fillIn = function fillIn(dest, src) { | ||
DSUtils.forOwn(src, function (value, key) { | ||
if (!dest.hasOwnProperty(key) || dest[key] === undefined) { | ||
dest[key] = value; | ||
} | ||
}); | ||
}; | ||
var unique = function unique(array) { | ||
@@ -87,16 +62,117 @@ var seen = {}; | ||
var Defaults = function Defaults() { | ||
babelHelpers.classCallCheck(this, Defaults); | ||
var noop = function noop() { | ||
var self = this; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var opts = args[args.length - 1]; | ||
self.dbg.apply(self, [opts.op].concat(args)); | ||
return resolve(); | ||
}; | ||
addHiddenPropsToTarget(Defaults.prototype, { | ||
host: 'localhost', | ||
port: 28015, | ||
var noop2 = function noop2() { | ||
var self = this; | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
var opts = args[args.length - 2]; | ||
self.dbg.apply(self, [opts.op].concat(args)); | ||
return resolve(); | ||
}; | ||
var DEFAULTS = { | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#authKey | ||
* @type {string} | ||
*/ | ||
authKey: '', | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#bufferSize | ||
* @type {number} | ||
* @default 10 | ||
*/ | ||
bufferSize: 10, | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#db | ||
* @type {string} | ||
* @default "test" | ||
*/ | ||
db: 'test', | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#debug | ||
* @type {boolean} | ||
* @default false | ||
*/ | ||
debug: false, | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#host | ||
* @type {string} | ||
* @default "localhost" | ||
*/ | ||
host: 'localhost', | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#min | ||
* @type {number} | ||
* @default 10 | ||
*/ | ||
min: 10, | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#max | ||
* @type {number} | ||
* @default 50 | ||
*/ | ||
max: 50, | ||
bufferSize: 10 | ||
}); | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#port | ||
* @type {number} | ||
* @default 10 | ||
*/ | ||
port: 28015, | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#raw | ||
* @type {boolean} | ||
* @default false | ||
*/ | ||
raw: false, | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#returnDeletedIds | ||
* @type {boolean} | ||
* @default false | ||
*/ | ||
returnDeletedIds: false | ||
}; | ||
/** | ||
@@ -124,7 +200,6 @@ * RethinkDBAdapter class. | ||
var self = this; | ||
self.defaults = new Defaults(); | ||
deepMixIn(self.defaults, opts); | ||
opts || (opts = {}); | ||
fillIn(opts, DEFAULTS); | ||
fillIn(self, opts); | ||
self.r = rethinkdbdash(self.defaults); | ||
self.r = rethinkdbdash(opts); | ||
self.databases = {}; | ||
@@ -144,4 +219,127 @@ self.tables = {}; | ||
}, | ||
/** | ||
* @name RethinkDBAdapter#afterCreate | ||
* @method | ||
*/ | ||
afterCreate: noop2, | ||
/** | ||
* @name RethinkDBAdapter#afterCreateMany | ||
* @method | ||
*/ | ||
afterCreateMany: noop2, | ||
/** | ||
* @name RethinkDBAdapter#afterDestroy | ||
* @method | ||
*/ | ||
afterDestroy: noop2, | ||
/** | ||
* @name RethinkDBAdapter#afterDestroyAll | ||
* @method | ||
*/ | ||
afterDestroyAll: noop2, | ||
/** | ||
* @name RethinkDBAdapter#afterFind | ||
* @method | ||
*/ | ||
afterFind: noop2, | ||
/** | ||
* @name RethinkDBAdapter#afterFindAll | ||
* @method | ||
*/ | ||
afterFindAll: noop2, | ||
/** | ||
* @name RethinkDBAdapter#afterUpdate | ||
* @method | ||
*/ | ||
afterUpdate: noop2, | ||
/** | ||
* @name RethinkDBAdapter#afterUpdateAll | ||
* @method | ||
*/ | ||
afterUpdateAll: noop2, | ||
/** | ||
* @name RethinkDBAdapter#afterUpdateMany | ||
* @method | ||
*/ | ||
afterUpdateMany: noop2, | ||
/** | ||
* @name RethinkDBAdapter#beforeCreate | ||
* @method | ||
*/ | ||
beforeCreate: noop, | ||
/** | ||
* @name RethinkDBAdapter#beforeCreateMany | ||
* @method | ||
*/ | ||
beforeCreateMany: noop, | ||
/** | ||
* @name RethinkDBAdapter#beforeDestroy | ||
* @method | ||
*/ | ||
beforeDestroy: noop, | ||
/** | ||
* @name RethinkDBAdapter#beforeDestroyAll | ||
* @method | ||
*/ | ||
beforeDestroyAll: noop, | ||
/** | ||
* @name RethinkDBAdapter#beforeFind | ||
* @method | ||
*/ | ||
beforeFind: noop, | ||
/** | ||
* @name RethinkDBAdapter#beforeFindAll | ||
* @method | ||
*/ | ||
beforeFindAll: noop, | ||
/** | ||
* @name RethinkDBAdapter#beforeUpdate | ||
* @method | ||
*/ | ||
beforeUpdate: noop, | ||
/** | ||
* @name RethinkDBAdapter#beforeUpdateAll | ||
* @method | ||
*/ | ||
beforeUpdateAll: noop, | ||
/** | ||
* @name RethinkDBAdapter#beforeUpdateMany | ||
* @method | ||
*/ | ||
beforeUpdateMany: noop, | ||
/** | ||
* @name RethinkDBAdapter#dbg | ||
* @method | ||
*/ | ||
dbg: function dbg() { | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
this.log.apply(this, ['debug'].concat(args)); | ||
}, | ||
selectDb: function selectDb(opts) { | ||
return this.r.db(isUndefined(opts.db) ? this.db : opts.db); | ||
}, | ||
selectTable: function selectTable(Resource, opts) { | ||
return this.r.db(opts.db || this.defaults.db).table(Resource.table || underscore(Resource.name)); | ||
return this.selectDb(opts).table(Resource.table || underscore(Resource.name)); | ||
}, | ||
@@ -155,5 +353,5 @@ filterSequence: function filterSequence(sequence, params) { | ||
forEach(keys(params), function (k) { | ||
Object.keys(params).forEach(function (k) { | ||
var v = params[k]; | ||
if (!contains(reserved, k)) { | ||
if (reserved.indexOf(k) === -1) { | ||
if (isObject(v)) { | ||
@@ -172,3 +370,3 @@ params.where[k] = v; | ||
if (!isEmpty(params.where)) { | ||
if (Object.keys(params.where).length !== 0) { | ||
query = query.filter(function (row) { | ||
@@ -201,2 +399,6 @@ var subQuery = undefined; | ||
subQuery = subQuery ? subQuery.and(r.expr(v).default(r.expr([])).contains(row(field).default(null)).not()) : r.expr(v).default(r.expr([])).contains(row(field).default(null)).not(); | ||
} else if (op === 'contains') { | ||
subQuery = subQuery ? subQuery.and(row(field).default([]).contains(v)) : row(field).default([]).contains(v); | ||
} else if (op === 'notContains') { | ||
subQuery = subQuery ? subQuery.and(row(field).default([]).contains(v).not()) : row(field).default([]).contains(v).not(); | ||
} else if (op === '|==' || op === '|===') { | ||
@@ -222,6 +424,10 @@ subQuery = subQuery ? subQuery.or(row(field).default(null).eq(v)) : row(field).default(null).eq(v); | ||
subQuery = subQuery ? subQuery.or(r.expr(v).default(r.expr([])).contains(row(field).default(null)).not()) : r.expr(v).default(r.expr([])).contains(row(field).default(null)).not(); | ||
} else if (op === '|contains') { | ||
subQuery = subQuery ? subQuery.or(row(field).default([]).contains(v)) : row(field).default([]).contains(v); | ||
} else if (op === '|notContains') { | ||
subQuery = subQuery ? subQuery.or(row(field).default([]).contains(v).not()) : row(field).default([]).contains(v).not(); | ||
} | ||
}); | ||
}); | ||
return subQuery; | ||
return subQuery || true; | ||
}); | ||
@@ -238,3 +444,3 @@ } | ||
} | ||
query = upperCase(params.orderBy[i][1]) === 'DESC' ? query.orderBy(r.desc(params.orderBy[i][0])) : query.orderBy(params.orderBy[i][0]); | ||
query = (params.orderBy[i][1] || '').toUpperCase() === 'DESC' ? query.orderBy(r.desc(params.orderBy[i][0])) : query.orderBy(params.orderBy[i][0]); | ||
} | ||
@@ -255,4 +461,4 @@ } | ||
var self = this; | ||
opts = opts || {}; | ||
var db = opts.db || self.defaults.db; | ||
opts || (opts = {}); | ||
var db = isUndefined(opts.db) ? self.db : opts.db; | ||
if (!self.databases[db]) { | ||
@@ -270,17 +476,39 @@ self.databases[db] = self.r.branch(self.r.dbList().contains(db), true, self.r.dbCreate(db)).run(); | ||
* @method | ||
* @param {Object} Resource The Resource. | ||
* @param {Object} mapper The mapper. | ||
* @param {Object} props The record to be created. | ||
* @param {Object} [opts] Configuration options. | ||
* @param {boolean} [opts.raw=false] TODO | ||
* @return {Promise} | ||
*/ | ||
create: function create(Resource, props, opts) { | ||
create: function create(mapper, props, opts) { | ||
var self = this; | ||
props = removeCircular(omit(props, Resource.relationFields || [])); | ||
var op = undefined; | ||
props || (props = {}); | ||
opts || (opts = {}); | ||
return self.waitForTable(Resource.table || underscore(Resource.name), opts).then(function () { | ||
return self.selectTable(Resource, opts).insert(props, { returnChanges: true }).run(); | ||
return self.waitForTable(mapper.table || underscore(mapper.name), opts).then(function () { | ||
// beforeCreate lifecycle hook | ||
op = opts.op = 'beforeCreate'; | ||
return resolve(self[op](mapper, props, opts)); | ||
}).then(function (_props) { | ||
// Allow for re-assignment from lifecycle hook | ||
_props = isUndefined(_props) ? props : _props; | ||
return self.selectTable(mapper, opts).insert(_props, { returnChanges: true }).run(); | ||
}).then(function (cursor) { | ||
self._handleErrors(cursor); | ||
return cursor.changes[0].new_val; | ||
var record = undefined; | ||
if (cursor && cursor.changes && cursor.changes.length && cursor.changes[0].new_val) { | ||
record = cursor.changes[0].new_val; | ||
} | ||
// afterCreate lifecycle hook | ||
op = opts.op = 'afterCreate'; | ||
return self[op](mapper, props, opts, record).then(function (_record) { | ||
// Allow for re-assignment from lifecycle hook | ||
record = isUndefined(_record) ? record : _record; | ||
var result = {}; | ||
fillIn(result, cursor); | ||
result.data = record; | ||
result.created = record ? 1 : 0; | ||
return self.getRaw(opts) ? result : result.data; | ||
}); | ||
}); | ||
@@ -291,2 +519,50 @@ }, | ||
/** | ||
* Create multiple records in a single batch. | ||
* | ||
* @name RethinkDBAdapter#createMany | ||
* @method | ||
* @param {Object} mapper The mapper. | ||
* @param {Object} props The records to be created. | ||
* @param {Object} [opts] Configuration options. | ||
* @param {boolean} [opts.raw=false] TODO | ||
* @return {Promise} | ||
*/ | ||
createMany: function createMany(mapper, props, opts) { | ||
var self = this; | ||
var op = undefined; | ||
props || (props = {}); | ||
opts || (opts = {}); | ||
return self.waitForTable(mapper.table || underscore(mapper.name), opts).then(function () { | ||
// beforeCreateMany lifecycle hook | ||
op = opts.op = 'beforeCreateMany'; | ||
return resolve(self[op](mapper, props, opts)); | ||
}).then(function (_props) { | ||
// Allow for re-assignment from lifecycle hook | ||
_props = isUndefined(_props) ? props : _props; | ||
return self.selectTable(mapper, opts).insert(_props, { returnChanges: true }).run(); | ||
}).then(function (cursor) { | ||
self._handleErrors(cursor); | ||
var records = []; | ||
if (cursor && cursor.changes && cursor.changes.length && cursor.changes) { | ||
records = cursor.changes.map(function (change) { | ||
return change.new_val; | ||
}); | ||
} | ||
// afterCreateMany lifecycle hook | ||
op = opts.op = 'afterCreateMany'; | ||
return self[op](mapper, props, opts, records).then(function (_records) { | ||
// Allow for re-assignment from lifecycle hook | ||
records = isUndefined(_records) ? records : _records; | ||
var result = {}; | ||
fillIn(result, cursor); | ||
result.data = records; | ||
result.created = records.length; | ||
return self.getRaw(opts) ? result : result.data; | ||
}); | ||
}); | ||
}, | ||
/** | ||
* Destroy the record with the given primary key. | ||
@@ -296,15 +572,39 @@ * | ||
* @method | ||
* @param {Object} Resource The Resource. | ||
* @param {Object} mapper The mapper. | ||
* @param {(string|number)} id Primary key of the record to destroy. | ||
* @param {Object} [opts] Configuration options. | ||
* @param {boolean} [opts.raw=false] TODO | ||
* @param {boolean} [opts.returnDeletedIds=false] Whether to return the | ||
* primary keys of any deleted records. | ||
* @return {Promise} | ||
*/ | ||
destroy: function destroy(Resource, id, opts) { | ||
destroy: function destroy(mapper, id, opts) { | ||
var self = this; | ||
var op = undefined; | ||
opts || (opts = {}); | ||
var returnDeletedIds = isUndefined(opts.returnDeletedIds) ? self.returnDeletedIds : !!opts.returnDeletedIds; | ||
return self.waitForTable(Resource.table || underscore(Resource.name), opts).then(function () { | ||
return self.selectTable(Resource, opts).get(id).delete().run(); | ||
return self.waitForTable(mapper.table || underscore(mapper.name), opts).then(function () { | ||
// beforeDestroy lifecycle hook | ||
op = opts.op = 'beforeDestroy'; | ||
return resolve(self[op](mapper, id, opts)); | ||
}).then(function () { | ||
return undefined; | ||
op = opts.op = 'destroy'; | ||
self.dbg(op, id, opts); | ||
return self.selectTable(mapper, opts).get(id).delete().run(); | ||
}).then(function (cursor) { | ||
var deleted = 0; | ||
if (cursor && cursor.deleted) { | ||
deleted = cursor.deleted; | ||
} | ||
// afterDestroy lifecycle hook | ||
op = opts.op = 'afterDestroy'; | ||
return resolve(self[op](mapper, id, opts, deleted ? id : undefined)).then(function (_id) { | ||
// Allow for re-assignment from lifecycle hook | ||
id = isUndefined(_id) && returnDeletedIds ? id : _id; | ||
var result = {}; | ||
fillIn(result, cursor); | ||
result.data = deleted ? id : undefined; | ||
return self.getRaw(opts) ? result : result.data; | ||
}); | ||
}); | ||
@@ -319,16 +619,50 @@ }, | ||
* @method | ||
* @param {Object} Resource the Resource. | ||
* @param {Object} mapper the mapper. | ||
* @param {Object} [query] Selection query. | ||
* @param {Object} [opts] Configuration options. | ||
* @param {boolean} [opts.raw=false] TODO | ||
* @param {boolean} [opts.returnDeletedIds=false] Whether to return the | ||
* primary keys of any deleted records. | ||
* @return {Promise} | ||
*/ | ||
destroyAll: function destroyAll(Resource, query, opts) { | ||
destroyAll: function destroyAll(mapper, query, opts) { | ||
var self = this; | ||
var idAttribute = mapper.idAttribute; | ||
var op = undefined; | ||
query || (query = {}); | ||
opts || (opts = {}); | ||
var returnDeletedIds = isUndefined(opts.returnDeletedIds) ? self.returnDeletedIds : !!opts.returnDeletedIds; | ||
return self.waitForTable(Resource.table || underscore(Resource.name), opts).then(function () { | ||
return self.filterSequence(self.selectTable(Resource, opts), query).delete().run(); | ||
return self.waitForTable(mapper.table || underscore(mapper.name), opts).then(function () { | ||
// beforeDestroyAll lifecycle hook | ||
op = opts.op = 'beforeDestroyAll'; | ||
return resolve(self[op](mapper, query, opts)); | ||
}).then(function () { | ||
return undefined; | ||
op = opts.op = 'destroyAll'; | ||
self.dbg(op, query, opts); | ||
return self.filterSequence(self.selectTable(mapper, opts), query).delete({ returnChanges: returnDeletedIds }).merge(function (cursor) { | ||
return { | ||
changes: cursor('changes').default([]).map(function (record) { | ||
return record('old_val').default({})(idAttribute).default({}); | ||
}).filter(function (id) { | ||
return id; | ||
}) | ||
}; | ||
}).run(); | ||
}).then(function (cursor) { | ||
var deletedIds = undefined; | ||
if (cursor && cursor.changes && returnDeletedIds) { | ||
deletedIds = cursor.changes; | ||
delete cursor.changes; | ||
} | ||
// afterDestroyAll lifecycle hook | ||
op = opts.op = 'afterDestroyAll'; | ||
return resolve(self[op](mapper, query, opts, deletedIds)).then(function (_deletedIds) { | ||
// Allow for re-assignment from lifecycle hook | ||
deletedIds = isUndefined(_deletedIds) ? deletedIds : _deletedIds; | ||
var result = {}; | ||
fillIn(result, cursor); | ||
result.data = deletedIds; | ||
return self.getRaw(opts) ? result : result.data; | ||
}); | ||
}); | ||
@@ -349,3 +683,3 @@ }, | ||
makeHasManyForeignKey: function makeHasManyForeignKey(Resource, def, record) { | ||
return DSUtils.get(record, Resource.idAttribute); | ||
return def.getForeignKey(record); | ||
}, | ||
@@ -361,7 +695,7 @@ | ||
*/ | ||
loadHasMany: function loadHasMany(Resource, def, records, __options) { | ||
loadHasMany: function loadHasMany(Resource, def, records, __opts) { | ||
var self = this; | ||
var singular = false; | ||
if (DSUtils.isObject(records) && !DSUtils.isArray(records)) { | ||
if (isObject(records) && !isArray(records)) { | ||
singular = true; | ||
@@ -383,3 +717,3 @@ records = [records]; | ||
} | ||
return self.findAll(Resource.getResource(def.relation), query, __options).then(function (relatedItems) { | ||
return self.findAll(def.getRelation(), query, __opts).then(function (relatedItems) { | ||
records.forEach(function (record) { | ||
@@ -392,3 +726,3 @@ var attached = []; | ||
relatedItems.forEach(function (relatedItem) { | ||
if (DSUtils.get(relatedItem, def.foreignKey) === record[Resource.idAttribute]) { | ||
if (get(relatedItem, def.foreignKey) === record[Resource.idAttribute]) { | ||
attached.push(relatedItem); | ||
@@ -398,3 +732,3 @@ } | ||
} | ||
DSUtils.set(record, def.localField, attached); | ||
def.setLocalField(record, attached); | ||
}); | ||
@@ -412,11 +746,11 @@ }); | ||
*/ | ||
loadHasOne: function loadHasOne(Resource, def, records, __options) { | ||
if (DSUtils.isObject(records) && !DSUtils.isArray(records)) { | ||
loadHasOne: function loadHasOne(Resource, def, records, __opts) { | ||
if (isObject(records) && !isArray(records)) { | ||
records = [records]; | ||
} | ||
return this.loadHasMany(Resource, def, records, __options).then(function () { | ||
return this.loadHasMany(Resource, def, records, __opts).then(function () { | ||
records.forEach(function (record) { | ||
var relatedData = DSUtils.get(record, def.localField); | ||
if (DSUtils.isArray(relatedData) && relatedData.length) { | ||
DSUtils.set(record, def.localField, relatedData[0]); | ||
var relatedData = def.getLocalField(record); | ||
if (isArray(relatedData) && relatedData.length) { | ||
def.setLocalField(record, relatedData[0]); | ||
} | ||
@@ -436,3 +770,3 @@ }); | ||
makeBelongsToForeignKey: function makeBelongsToForeignKey(Resource, def, record) { | ||
return DSUtils.get(record, def.localKey); | ||
return def.getForeignKey(record); | ||
}, | ||
@@ -448,12 +782,12 @@ | ||
*/ | ||
loadBelongsTo: function loadBelongsTo(Resource, def, records, __options) { | ||
loadBelongsTo: function loadBelongsTo(mapper, def, records, __opts) { | ||
var self = this; | ||
var relationDef = Resource.getResource(def.relation); | ||
var relationDef = def.getRelation(); | ||
if (DSUtils.isObject(records) && !DSUtils.isArray(records)) { | ||
if (isObject(records) && !isArray(records)) { | ||
var _ret = function () { | ||
var record = records; | ||
return { | ||
v: self.find(relationDef, self.makeBelongsToForeignKey(Resource, def, record), __options).then(function (relatedItem) { | ||
DSUtils.set(record, def.localField, relatedItem); | ||
v: self.find(relationDef, self.makeBelongsToForeignKey(mapper, def, record), __opts).then(function (relatedItem) { | ||
def.setLocalField(record, relatedItem); | ||
}) | ||
@@ -465,4 +799,4 @@ }; | ||
} else { | ||
var _keys = records.map(function (record) { | ||
return self.makeBelongsToForeignKey(Resource, def, record); | ||
var keys = records.map(function (record) { | ||
return self.makeBelongsToForeignKey(mapper, def, record); | ||
}).filter(function (key) { | ||
@@ -473,9 +807,9 @@ return key; | ||
where: babelHelpers.defineProperty({}, relationDef.idAttribute, { | ||
'in': _keys | ||
'in': keys | ||
}) | ||
}, __options).then(function (relatedItems) { | ||
}, __opts).then(function (relatedItems) { | ||
records.forEach(function (record) { | ||
relatedItems.forEach(function (relatedItem) { | ||
if (relatedItem[relationDef.idAttribute] === record[def.localKey]) { | ||
DSUtils.set(record, def.localField, relatedItem); | ||
if (relatedItem[relationDef.idAttribute] === record[def.foreignKey]) { | ||
def.setLocalField(record, relatedItem); | ||
} | ||
@@ -494,16 +828,18 @@ }); | ||
* @method | ||
* @param {Object} Resource The Resource. | ||
* @param {Object} mapper The mapper. | ||
* @param {(string|number)} id Primary key of the record to retrieve. | ||
* @param {Object} [opts] Configuration options. | ||
* @param {boolean} [opts.raw=false] TODO | ||
* @param {string[]} [opts.with=[]] TODO | ||
* @return {Promise} | ||
*/ | ||
find: function find(Resource, id, opts) { | ||
find: function find(mapper, id, opts) { | ||
var self = this; | ||
var record = undefined, | ||
op = undefined; | ||
opts || (opts = {}); | ||
opts.with || (opts.with = []); | ||
var instance = undefined; | ||
var table = Resource.table || underscore(Resource.name); | ||
var relationList = Resource.relationList || []; | ||
var table = mapper.table || underscore(mapper.name); | ||
var relationList = mapper.relationList || []; | ||
var tasks = [self.waitForTable(table, opts)]; | ||
@@ -513,83 +849,81 @@ | ||
var relationName = def.relation; | ||
var relationDef = Resource.getResource(relationName); | ||
if (!relationDef) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
} else if (!opts.with || !contains(opts.with, relationName)) { | ||
var relationDef = def.getRelation(); | ||
if (!opts.with || opts.with.indexOf(relationName) === -1) { | ||
return; | ||
} | ||
if (def.foreignKey) { | ||
tasks.push(self.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)); | ||
} else if (def.localKey) { | ||
tasks.push(self.waitForIndex(Resource.table || underscore(Resource.name), def.localKey, opts)); | ||
if (def.foreignKey && def.type !== 'belongsTo') { | ||
if (def.type === 'belongsTo') { | ||
tasks.push(self.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)); | ||
} else { | ||
tasks.push(self.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)); | ||
} | ||
} | ||
}); | ||
return DSUtils.Promise.all(tasks).then(function () { | ||
return self.selectTable(Resource, opts).get(id).run(); | ||
}).then(function (_instance) { | ||
if (!_instance) { | ||
throw new Error('Not Found!'); | ||
return Promise.all(tasks).then(function () { | ||
// beforeFind lifecycle hook | ||
op = opts.op = 'beforeFind'; | ||
return resolve(self[op](mapper, id, opts)).then(function () { | ||
op = opts.op = 'find'; | ||
self.dbg(op, id, opts); | ||
return self.selectTable(mapper, opts).get(id).run(); | ||
}); | ||
}).then(function (_record) { | ||
if (!_record) { | ||
return; | ||
} | ||
instance = _instance; | ||
record = _record; | ||
var tasks = []; | ||
relationList.forEach(function (def) { | ||
var relationName = def.relation; | ||
var relationDef = Resource.getResource(relationName); | ||
var containedName = null; | ||
if (opts.with.indexOf(relationName) !== -1) { | ||
containedName = relationName; | ||
} else if (opts.with.indexOf(def.localField) !== -1) { | ||
containedName = def.localField; | ||
} | ||
if (containedName) { | ||
(function () { | ||
var __options = DSUtils.deepMixIn({}, opts.orig ? opts.orig() : opts); | ||
__options.with = opts.with.slice(); | ||
__options = DSUtils._(relationDef, __options); | ||
DSUtils.remove(__options.with, containedName); | ||
__options.with.forEach(function (relation, i) { | ||
if (relation && relation.indexOf(containedName) === 0 && relation.length >= containedName.length && relation[containedName.length] === '.') { | ||
__options.with[i] = relation.substr(containedName.length + 1); | ||
} else { | ||
__options.with[i] = ''; | ||
} | ||
}); | ||
forEachRelation(mapper, opts, function (def, __opts) { | ||
var relatedMapper = def.getRelation(); | ||
var task = undefined; | ||
var task = undefined; | ||
if (def.foreignKey && (def.type === 'hasOne' || def.type === 'hasMany')) { | ||
if (def.type === 'hasOne') { | ||
task = self.loadHasOne(Resource, def, instance, __options); | ||
} else { | ||
task = self.loadHasMany(Resource, def, instance, __options); | ||
} | ||
} else if (def.type === 'hasMany' && def.localKeys) { | ||
var localKeys = []; | ||
var itemKeys = instance[def.localKeys] || []; | ||
itemKeys = DSUtils.isArray(itemKeys) ? itemKeys : DSUtils.keys(itemKeys); | ||
localKeys = localKeys.concat(itemKeys || []); | ||
task = self.findAll(Resource.getResource(relationName), { | ||
where: babelHelpers.defineProperty({}, relationDef.idAttribute, { | ||
'in': unique(localKeys).filter(function (x) { | ||
return x; | ||
}) | ||
}) | ||
}, __options).then(function (relatedItems) { | ||
DSUtils.set(instance, def.localField, relatedItems); | ||
return relatedItems; | ||
}); | ||
} else if (def.type === 'belongsTo' || def.type === 'hasOne' && def.localKey) { | ||
task = self.loadBelongsTo(Resource, def, instance, __options); | ||
} | ||
if (task) { | ||
tasks.push(task); | ||
} | ||
})(); | ||
if (def.foreignKey && (def.type === 'hasOne' || def.type === 'hasMany')) { | ||
if (def.type === 'hasOne') { | ||
task = self.loadHasOne(mapper, def, record, __opts); | ||
} else { | ||
task = self.loadHasMany(mapper, def, record, __opts); | ||
} | ||
} else if (def.type === 'hasMany' && def.localKeys) { | ||
var localKeys = []; | ||
var itemKeys = get(record, def.localKeys) || []; | ||
itemKeys = isArray(itemKeys) ? itemKeys : Object.keys(itemKeys); | ||
localKeys = localKeys.concat(itemKeys); | ||
task = self.findAll(relatedMapper, { | ||
where: babelHelpers.defineProperty({}, relatedMapper.idAttribute, { | ||
'in': unique(localKeys).filter(function (x) { | ||
return x; | ||
}) | ||
}) | ||
}, __opts).then(function (relatedItems) { | ||
def.setLocalField(record, relatedItems); | ||
}); | ||
} else if (def.type === 'hasMany' && def.foreignKeys) { | ||
task = self.findAll(relatedMapper, { | ||
where: babelHelpers.defineProperty({}, def.foreignKeys, { | ||
'contains': get(record, mapper.idAttribute) | ||
}) | ||
}, __opts).then(function (relatedItems) { | ||
def.setLocalField(record, relatedItems); | ||
}); | ||
} else if (def.type === 'belongsTo') { | ||
task = self.loadBelongsTo(mapper, def, record, __opts); | ||
} | ||
if (task) { | ||
tasks.push(task); | ||
} | ||
}); | ||
return DSUtils.Promise.all(tasks); | ||
return Promise.all(tasks); | ||
}).then(function () { | ||
return instance; | ||
// afterFind lifecycle hook | ||
op = opts.op = 'afterFind'; | ||
return resolve(self[op](mapper, id, opts, record)).then(function (_record) { | ||
// Allow for re-assignment from lifecycle hook | ||
record = isUndefined(_record) ? record : _record; | ||
return self.getRaw(opts) ? { | ||
data: record, | ||
found: record ? 1 : 0 | ||
} : record; | ||
}); | ||
}); | ||
@@ -604,9 +938,10 @@ }, | ||
* @method | ||
* @param {Object} Resource The Resource. | ||
* @param {Object} mapper The mapper. | ||
* @param {Object} query Selection query. | ||
* @param {Object} [opts] Configuration options. | ||
* @param {boolean} [opts.raw=false] TODO | ||
* @param {string[]} [opts.with=[]] TODO | ||
* @return {Promise} | ||
*/ | ||
findAll: function findAll(Resource, query, opts) { | ||
findAll: function findAll(mapper, query, opts) { | ||
var self = this; | ||
@@ -616,5 +951,6 @@ opts || (opts = {}); | ||
var items = null; | ||
var table = Resource.table || underscore(Resource.name); | ||
var relationList = Resource.relationList || []; | ||
var records = []; | ||
var op = undefined; | ||
var table = mapper.table || underscore(mapper.name); | ||
var relationList = mapper.relationList || []; | ||
var tasks = [self.waitForTable(table, opts)]; | ||
@@ -624,93 +960,104 @@ | ||
var relationName = def.relation; | ||
var relationDef = Resource.getResource(relationName); | ||
if (!relationDef) { | ||
throw new JSData.DSErrors.NER(relationName); | ||
} else if (!opts.with || !contains(opts.with, relationName)) { | ||
var relationDef = def.getRelation(); | ||
if (!opts.with || opts.with.indexOf(relationName) === -1) { | ||
return; | ||
} | ||
if (def.foreignKey) { | ||
tasks.push(self.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)); | ||
} else if (def.localKey) { | ||
tasks.push(self.waitForIndex(Resource.table || underscore(Resource.name), def.localKey, opts)); | ||
if (def.foreignKey && def.type !== 'belongsTo') { | ||
if (def.type === 'belongsTo') { | ||
tasks.push(self.waitForIndex(mapper.table || underscore(mapper.name), def.foreignKey, opts)); | ||
} else { | ||
tasks.push(self.waitForIndex(relationDef.table || underscore(relationDef.name), def.foreignKey, opts)); | ||
} | ||
} | ||
}); | ||
return DSUtils.Promise.all(tasks).then(function () { | ||
return self.filterSequence(self.selectTable(Resource, opts), query).run(); | ||
}).then(function (_items) { | ||
items = _items; | ||
return Promise.all(tasks).then(function () { | ||
// beforeFindAll lifecycle hook | ||
op = opts.op = 'beforeFindAll'; | ||
return resolve(self[op](mapper, query, opts)); | ||
}).then(function () { | ||
op = opts.op = 'findAll'; | ||
self.dbg(op, query, opts); | ||
return self.filterSequence(self.selectTable(mapper, opts), query).run(); | ||
}).then(function (_records) { | ||
records = _records; | ||
var tasks = []; | ||
var relationList = Resource.relationList || []; | ||
relationList.forEach(function (def) { | ||
var relationName = def.relation; | ||
var relationDef = Resource.getResource(relationName); | ||
var containedName = null; | ||
if (opts.with.indexOf(relationName) !== -1) { | ||
containedName = relationName; | ||
} else if (opts.with.indexOf(def.localField) !== -1) { | ||
containedName = def.localField; | ||
} | ||
if (containedName) { | ||
forEachRelation(mapper, opts, function (def, __opts) { | ||
var relatedMapper = def.getRelation(); | ||
var idAttribute = mapper.idAttribute; | ||
var task = undefined; | ||
if (def.foreignKey && (def.type === 'hasOne' || def.type === 'hasMany')) { | ||
if (def.type === 'hasMany') { | ||
task = self.loadHasMany(mapper, def, records, __opts); | ||
} else { | ||
task = self.loadHasOne(mapper, def, records, __opts); | ||
} | ||
} else if (def.type === 'hasMany' && def.localKeys) { | ||
(function () { | ||
var __options = DSUtils.deepMixIn({}, opts.orig ? opts.orig() : opts); | ||
__options.with = opts.with.slice(); | ||
__options = DSUtils._(relationDef, __options); | ||
DSUtils.remove(__options.with, containedName); | ||
__options.with.forEach(function (relation, i) { | ||
if (relation && relation.indexOf(containedName) === 0 && relation.length >= containedName.length && relation[containedName.length] === '.') { | ||
__options.with[i] = relation.substr(containedName.length + 1); | ||
} else { | ||
__options.with[i] = ''; | ||
} | ||
var localKeys = []; | ||
records.forEach(function (item) { | ||
var itemKeys = item[def.localKeys] || []; | ||
itemKeys = isArray(itemKeys) ? itemKeys : Object.keys(itemKeys); | ||
localKeys = localKeys.concat(itemKeys); | ||
}); | ||
var task = undefined; | ||
if (def.foreignKey && (def.type === 'hasOne' || def.type === 'hasMany')) { | ||
if (def.type === 'hasMany') { | ||
task = self.loadHasMany(Resource, def, items, __options); | ||
} else { | ||
task = self.loadHasOne(Resource, def, items, __options); | ||
} | ||
} else if (def.type === 'hasMany' && def.localKeys) { | ||
(function () { | ||
var localKeys = []; | ||
items.forEach(function (item) { | ||
var itemKeys = item[def.localKeys] || []; | ||
itemKeys = DSUtils.isArray(itemKeys) ? itemKeys : Object.keys(itemKeys); | ||
localKeys = localKeys.concat(itemKeys || []); | ||
task = self.findAll(relatedMapper, { | ||
where: babelHelpers.defineProperty({}, relatedMapper.idAttribute, { | ||
'in': unique(localKeys).filter(function (x) { | ||
return x; | ||
}) | ||
}) | ||
}, __opts).then(function (relatedItems) { | ||
records.forEach(function (item) { | ||
var attached = []; | ||
var itemKeys = get(item, def.localKeys) || []; | ||
itemKeys = isArray(itemKeys) ? itemKeys : Object.keys(itemKeys); | ||
relatedItems.forEach(function (relatedItem) { | ||
if (itemKeys && itemKeys.indexOf(relatedItem[relatedMapper.idAttribute]) !== -1) { | ||
attached.push(relatedItem); | ||
} | ||
}); | ||
task = self.findAll(Resource.getResource(relationName), { | ||
where: babelHelpers.defineProperty({}, relationDef.idAttribute, { | ||
'in': unique(localKeys).filter(function (x) { | ||
return x; | ||
}) | ||
}) | ||
}, __options).then(function (relatedItems) { | ||
items.forEach(function (item) { | ||
var attached = []; | ||
var itemKeys = item[def.localKeys] || []; | ||
itemKeys = DSUtils.isArray(itemKeys) ? itemKeys : DSUtils.keys(itemKeys); | ||
relatedItems.forEach(function (relatedItem) { | ||
if (itemKeys && itemKeys.indexOf(relatedItem[relationDef.idAttribute]) !== -1) { | ||
attached.push(relatedItem); | ||
} | ||
}); | ||
DSUtils.set(item, def.localField, attached); | ||
}); | ||
return relatedItems; | ||
}); | ||
})(); | ||
} else if (def.type === 'belongsTo' || def.type === 'hasOne' && def.localKey) { | ||
task = self.loadBelongsTo(Resource, def, items, __options); | ||
} | ||
if (task) { | ||
tasks.push(task); | ||
} | ||
def.setLocalField(item, attached); | ||
}); | ||
return relatedItems; | ||
}); | ||
})(); | ||
} else if (def.type === 'hasMany' && def.foreignKeys) { | ||
task = self.findAll(relatedMapper, { | ||
where: babelHelpers.defineProperty({}, def.foreignKeys, { | ||
'isectNotEmpty': records.map(function (record) { | ||
return get(record, idAttribute); | ||
}) | ||
}) | ||
}, __opts).then(function (relatedItems) { | ||
var foreignKeysField = def.foreignKeys; | ||
records.forEach(function (record) { | ||
var _relatedItems = []; | ||
var id = get(record, idAttribute); | ||
relatedItems.forEach(function (relatedItem) { | ||
var foreignKeys = get(relatedItems, foreignKeysField) || []; | ||
if (foreignKeys.indexOf(id) !== -1) { | ||
_relatedItems.push(relatedItem); | ||
} | ||
}); | ||
def.setLocalField(record, _relatedItems); | ||
}); | ||
}); | ||
} else if (def.type === 'belongsTo') { | ||
task = self.loadBelongsTo(mapper, def, records, __opts); | ||
} | ||
if (task) { | ||
tasks.push(task); | ||
} | ||
}); | ||
return DSUtils.Promise.all(tasks); | ||
return Promise.all(tasks); | ||
}).then(function () { | ||
return items; | ||
// afterFindAll lifecycle hook | ||
op = opts.op = 'afterFindAll'; | ||
return resolve(self[op](mapper, query, opts, records)).then(function (_records) { | ||
// Allow for re-assignment from lifecycle hook | ||
records = isUndefined(_records) ? records : _records; | ||
return self.getRaw(opts) ? { | ||
data: records, | ||
found: records.length | ||
} : records; | ||
}); | ||
}); | ||
@@ -721,2 +1068,45 @@ }, | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#getRaw | ||
* @method | ||
*/ | ||
getRaw: function getRaw(opts) { | ||
opts || (opts = {}); | ||
return !!(isUndefined(opts.raw) ? this.raw : opts.raw); | ||
}, | ||
/** | ||
* TODO | ||
* | ||
* @name RethinkDBAdapter#log | ||
* @method | ||
*/ | ||
log: function log(level) { | ||
for (var _len4 = arguments.length, args = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { | ||
args[_key4 - 1] = arguments[_key4]; | ||
} | ||
if (level && !args.length) { | ||
args.push(level); | ||
level = 'debug'; | ||
} | ||
if (level === 'debug' && !this.debug) { | ||
return; | ||
} | ||
var prefix = level.toUpperCase() + ': (RethinkDBAdapter)'; | ||
if (console[level]) { | ||
var _console; | ||
(_console = console)[level].apply(_console, [prefix].concat(args)); | ||
} else { | ||
var _console2; | ||
(_console2 = console).log.apply(_console2, [prefix].concat(args)); | ||
} | ||
}, | ||
/** | ||
* Apply the given update to the record with the specified primary key. | ||
@@ -726,22 +1116,43 @@ * | ||
* @method | ||
* @param {Object} Resource The Resource. | ||
* @param {Object} mapper The mapper. | ||
* @param {(string|number)} id The primary key of the record to be updated. | ||
* @param {Object} props The update to apply to the record. | ||
* @param {Object} [opts] Configuration options. | ||
* @param {boolean} [opts.raw=false] TODO | ||
* @return {Promise} | ||
*/ | ||
update: function update(resourceConfig, id, attrs, options) { | ||
var _this = this; | ||
update: function update(mapper, id, props, opts) { | ||
var self = this; | ||
props || (props = {}); | ||
opts || (opts = {}); | ||
var op = undefined; | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return _this.r.db(options.db || _this.defaults.db).table(resourceConfig.table || underscore(resourceConfig.name)).get(id).update(attrs, { returnChanges: true }).run(); | ||
return self.waitForTable(mapper.table || underscore(mapper.name), opts).then(function () { | ||
// beforeUpdate lifecycle hook | ||
op = opts.op = 'beforeUpdate'; | ||
return resolve(self[op](mapper, id, props, opts)); | ||
}).then(function (_props) { | ||
// Allow for re-assignment from lifecycle hook | ||
_props = isUndefined(_props) ? props : _props; | ||
return self.selectTable(mapper, opts).get(id).update(_props, { returnChanges: true }).run(); | ||
}).then(function (cursor) { | ||
_this._handleErrors(cursor); | ||
if (cursor.changes && cursor.changes.length && cursor.changes[0].new_val) { | ||
return cursor.changes[0].new_val; | ||
var record = undefined; | ||
self._handleErrors(cursor); | ||
if (cursor && cursor.changes && cursor.changes.length && cursor.changes[0].new_val) { | ||
record = cursor.changes[0].new_val; | ||
} else { | ||
return _this.selectTable(resourceConfig, options).get(id).run(); | ||
throw new Error('Not Found'); | ||
} | ||
// afterUpdate lifecycle hook | ||
op = opts.op = 'afterUpdate'; | ||
return resolve(self[op](mapper, id, props, opts, record)).then(function (_record) { | ||
// Allow for re-assignment from lifecycle hook | ||
record = isUndefined(_record) ? record : _record; | ||
var result = {}; | ||
fillIn(result, cursor); | ||
result.data = record; | ||
result.updated = record ? 1 : 0; | ||
return self.getRaw(opts) ? result : result.data; | ||
}); | ||
}); | ||
@@ -756,64 +1167,128 @@ }, | ||
* @method | ||
* @param {Object} Resource The Resource. | ||
* @param {Object} mapper The mapper. | ||
* @param {Object} props The update to apply to the selected records. | ||
* @param {Object} [query] Selection query. | ||
* @param {Object} [opts] Configuration options. | ||
* @param {boolean} [opts.raw=false] TODO | ||
* @return {Promise} | ||
*/ | ||
updateAll: function updateAll(resourceConfig, attrs, params, options) { | ||
var _this2 = this; | ||
updateAll: function updateAll(mapper, props, query, opts) { | ||
var self = this; | ||
props || (props = {}); | ||
query || (query = {}); | ||
opts || (opts = {}); | ||
var op = undefined; | ||
attrs = removeCircular(omit(attrs, resourceConfig.relationFields || [])); | ||
options = options || {}; | ||
params = params || {}; | ||
return this.waitForTable(resourceConfig.table || underscore(resourceConfig.name), options).then(function () { | ||
return _this2.filterSequence(_this2.selectTable(resourceConfig, options), params).update(attrs, { returnChanges: true }).run(); | ||
return self.waitForTable(mapper.table || underscore(mapper.name), opts).then(function () { | ||
// beforeUpdateAll lifecycle hook | ||
op = opts.op = 'beforeUpdateAll'; | ||
return resolve(self[op](mapper, props, query, opts)); | ||
}).then(function (_props) { | ||
// Allow for re-assignment from lifecycle hook | ||
_props = isUndefined(_props) ? props : _props; | ||
return self.filterSequence(self.selectTable(mapper, opts), query).update(_props, { returnChanges: true }).run(); | ||
}).then(function (cursor) { | ||
_this2._handleErrors(cursor); | ||
var records = []; | ||
self._handleErrors(cursor); | ||
if (cursor && cursor.changes && cursor.changes.length) { | ||
var _ret5 = function () { | ||
var items = []; | ||
cursor.changes.forEach(function (change) { | ||
return items.push(change.new_val); | ||
}); | ||
return { | ||
v: items | ||
}; | ||
}(); | ||
records = cursor.changes.map(function (change) { | ||
return change.new_val; | ||
}); | ||
} | ||
// afterUpdateAll lifecycle hook | ||
op = opts.op = 'afterUpdateAll'; | ||
return self[op](mapper, props, query, opts, records).then(function (_records) { | ||
// Allow for re-assignment from lifecycle hook | ||
records = isUndefined(_records) ? records : _records; | ||
var result = {}; | ||
fillIn(result, cursor); | ||
result.data = records; | ||
result.updated = records.length; | ||
return self.getRaw(opts) ? result : result.data; | ||
}); | ||
}); | ||
}, | ||
if ((typeof _ret5 === 'undefined' ? 'undefined' : babelHelpers.typeof(_ret5)) === "object") return _ret5.v; | ||
} else { | ||
return _this2.filterSequence(_this2.selectTable(resourceConfig, options), params).run(); | ||
/** | ||
* Update the given records in a single batch. | ||
* | ||
* @name RethinkDBAdapter#updateMany | ||
* @method | ||
* @param {Object} mapper The mapper. | ||
* @param {Object[]} records The records to update. | ||
* @param {Object} [opts] Configuration options. | ||
* @param {boolean} [opts.raw=false] TODO | ||
* @return {Promise} | ||
*/ | ||
updateMany: function updateMany(mapper, records, opts) { | ||
var self = this; | ||
records || (records = []); | ||
opts || (opts = {}); | ||
var op = undefined; | ||
var idAttribute = mapper.idAttribute; | ||
records = records.filter(function (record) { | ||
return get(record, idAttribute); | ||
}); | ||
return self.waitForTable(mapper.table || underscore(mapper.name), opts).then(function () { | ||
// beforeUpdateMany lifecycle hook | ||
op = opts.op = 'beforeUpdateMany'; | ||
return resolve(self[op](mapper, records, opts)); | ||
}).then(function (_records) { | ||
// Allow for re-assignment from lifecycle hook | ||
_records = isUndefined(_records) ? records : _records; | ||
return self.selectTable(mapper, opts).insert(_records, { returnChanges: true, conflict: 'update' }).run(); | ||
}).then(function (cursor) { | ||
var updatedRecords = undefined; | ||
self._handleErrors(cursor); | ||
if (cursor && cursor.changes && cursor.changes.length) { | ||
updatedRecords = cursor.changes.map(function (change) { | ||
return change.new_val; | ||
}); | ||
} | ||
// afterUpdateMany lifecycle hook | ||
op = opts.op = 'afterUpdateMany'; | ||
return resolve(self[op](mapper, records, opts, updatedRecords)).then(function (_records) { | ||
// Allow for re-assignment from lifecycle hook | ||
records = isUndefined(_records) ? updatedRecords : _records; | ||
var result = {}; | ||
fillIn(result, cursor); | ||
result.data = records; | ||
result.updated = records.length; | ||
return self.getRaw(opts) ? result : result.data; | ||
}); | ||
}); | ||
}, | ||
waitForTable: function waitForTable(table, options) { | ||
var _this3 = this; | ||
var _this = this; | ||
options = options || {}; | ||
var db = options.db || this.defaults.db; | ||
var db = isUndefined(options.db) ? this.db : options.db; | ||
return this.waitForDb(options).then(function () { | ||
_this3.tables[db] = _this3.tables[db] || {}; | ||
if (!_this3.tables[db][table]) { | ||
_this3.tables[db][table] = _this3.r.branch(_this3.r.db(db).tableList().contains(table), true, _this3.r.db(db).tableCreate(table)).run(); | ||
_this.tables[db] = _this.tables[db] || {}; | ||
if (!_this.tables[db][table]) { | ||
_this.tables[db][table] = _this.r.branch(_this.r.db(db).tableList().contains(table), true, _this.r.db(db).tableCreate(table)).run(); | ||
} | ||
return _this3.tables[db][table]; | ||
return _this.tables[db][table]; | ||
}); | ||
}, | ||
waitForIndex: function waitForIndex(table, index, options) { | ||
var _this4 = this; | ||
var _this2 = this; | ||
options = options || {}; | ||
var db = options.db || this.defaults.db; | ||
var db = isUndefined(options.db) ? this.db : options.db; | ||
return this.waitForDb(options).then(function () { | ||
return _this4.waitForTable(table, options); | ||
return _this2.waitForTable(table, options); | ||
}).then(function () { | ||
_this4.indices[db] = _this4.indices[db] || {}; | ||
_this4.indices[db][table] = _this4.indices[db][table] || {}; | ||
if (!_this4.tables[db][table][index]) { | ||
_this4.tables[db][table][index] = _this4.r.branch(_this4.r.db(db).table(table).indexList().contains(index), true, _this4.r.db(db).table(table).indexCreate(index)).run().then(function () { | ||
return _this4.r.db(db).table(table).indexWait(index).run(); | ||
_this2.indices[db] = _this2.indices[db] || {}; | ||
_this2.indices[db][table] = _this2.indices[db][table] || {}; | ||
if (!_this2.tables[db][table][index]) { | ||
_this2.tables[db][table][index] = _this2.r.branch(_this2.r.db(db).table(table).indexList().contains(index), true, _this2.r.db(db).table(table).indexCreate(index)).run().then(function () { | ||
return _this2.r.db(db).table(table).indexWait(index).run(); | ||
}); | ||
} | ||
return _this4.tables[db][table][index]; | ||
return _this2.tables[db][table][index]; | ||
}); | ||
@@ -820,0 +1295,0 @@ } |
{ | ||
"name": "js-data-rethinkdb", | ||
"description": "RethinkDB adapter for js-data.", | ||
"version": "2.2.0", | ||
"version": "3.0.0-alpha.1", | ||
"homepage": "https://github.com/js-data/js-data-rethinkdb", | ||
@@ -39,4 +39,4 @@ "repository": { | ||
"lint": "standard src/index.js mocha.start.js test/**/*.js", | ||
"mocha": "mocha -t 20000 -R dot mocha.start.js test/**/*.js", | ||
"cover": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -t 20000 -R dot mocha.start.js test/**/*.js", | ||
"mocha": "mocha -t 20000 -R dot -r source-map-support/register mocha.start.js test/**/*.js", | ||
"cover": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -t 20000 -R dot -r source-map-support/register mocha.start.js test/**/*.js", | ||
"test": "npm run lint && npm run bundle && npm run cover", | ||
@@ -50,3 +50,3 @@ "release": "npm test && npm run doc && node scripts/authors.js", | ||
"peerDependencies": { | ||
"js-data": ">=2.0.0 <3", | ||
"js-data": "^3.0.0-alpha.15", | ||
"rethinkdbdash": ">=1.15.0" | ||
@@ -67,3 +67,3 @@ }, | ||
"istanbul": "0.4.2", | ||
"js-data-adapter-tests": "1.5.0", | ||
"js-data-adapter-tests": "^2.0.0-alpha.6", | ||
"jsdoc": "3.4.0", | ||
@@ -70,0 +70,0 @@ "mocha": "2.4.5", |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
126148
1235
1
1