js-data-adapter
Advanced tools
Comparing version 0.7.3 to 0.7.4
@@ -0,1 +1,6 @@ | ||
##### 0.7.4 - 07 July 2016 | ||
###### Backwards compatible changes | ||
- Updated dependencies | ||
##### 0.7.3 - 25 May 2016 | ||
@@ -2,0 +7,0 @@ |
{ | ||
"name": "js-data-adapter", | ||
"description": "Base adapter class that all other js-data adapters extend.", | ||
"version": "0.7.3", | ||
"version": "0.7.4", | ||
"homepage": "https://github.com/js-data/js-data-adapter", | ||
@@ -67,3 +67,3 @@ "repository": { | ||
"peerDependencies": { | ||
"js-data": "^3.0.0-beta.6" | ||
"js-data": "^3.0.0-beta.10" | ||
}, | ||
@@ -79,4 +79,4 @@ "dependencies": { | ||
"babel-preset-stage-0": "6.5.0", | ||
"js-data-repo-tools": "0.5.2" | ||
"js-data-repo-tools": "0.5.5" | ||
} | ||
} |
771
src/index.js
@@ -0,8 +1,6 @@ | ||
import {Component, utils} from 'js-data' | ||
import {utils} from 'js-data' | ||
export const noop = function (...args) { | ||
const self = this | ||
const opts = args[args.length - 1] | ||
self.dbg(opts.op, ...args) | ||
this.dbg(opts.op, ...args) | ||
return utils.resolve() | ||
@@ -12,5 +10,4 @@ } | ||
export const noop2 = function (...args) { | ||
const self = this | ||
const opts = args[args.length - 2] | ||
self.dbg(opts.op, ...args) | ||
this.dbg(opts.op, ...args) | ||
return utils.resolve() | ||
@@ -36,45 +33,6 @@ } | ||
const relationFields = mapper.relationFields || [] | ||
const toStrip = relationFields.filter(function (value) { | ||
return opts.with.indexOf(value) === -1 | ||
}) | ||
const toStrip = relationFields.filter((value) => opts.with.indexOf(value) === -1) | ||
return utils.omit(props, toStrip) | ||
} | ||
const DEFAULTS = { | ||
/** | ||
* Whether to log debugging information. | ||
* | ||
* @name Adapter#debug | ||
* @type {boolean} | ||
* @default false | ||
*/ | ||
debug: false, | ||
/** | ||
* Whether to return a more detailed response object. | ||
* | ||
* @name Adapter#raw | ||
* @type {boolean} | ||
* @default false | ||
*/ | ||
raw: false | ||
} | ||
/** | ||
* Abstract class meant to be extended by adapters. | ||
* | ||
* @class Adapter | ||
* @abstract | ||
* @param {Object} [opts] Configuration opts. | ||
* @param {boolean} [opts.debug=false] Whether to log debugging information. | ||
* @param {boolean} [opts.raw=false] Whether to return a more detailed response | ||
* object. | ||
*/ | ||
export function Adapter (opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
utils.fillIn(opts, DEFAULTS) | ||
utils.fillIn(self, opts) | ||
} | ||
export const reserved = [ | ||
@@ -96,3 +54,2 @@ 'orderBy', | ||
export function Response (data, meta, op) { | ||
const self = this | ||
meta || (meta = {}) | ||
@@ -106,5 +63,5 @@ | ||
*/ | ||
self.data = data | ||
this.data = data | ||
utils.fillIn(self, meta) | ||
utils.fillIn(this, meta) | ||
@@ -117,19 +74,47 @@ /** | ||
*/ | ||
self.op = op | ||
this.op = op | ||
} | ||
const DEFAULTS = { | ||
/** | ||
* Whether to log debugging information. | ||
* | ||
* @name Adapter#debug | ||
* @type {boolean} | ||
* @default false | ||
*/ | ||
debug: false, | ||
/** | ||
* Whether to return a more detailed response object. | ||
* | ||
* @name Adapter#raw | ||
* @type {boolean} | ||
* @default false | ||
*/ | ||
raw: false | ||
} | ||
/** | ||
* Alternative to ES6 class syntax for extending `Adapter`. | ||
* Abstract class meant to be extended by adapters. | ||
* | ||
* @name Adapter.extend | ||
* @method | ||
* @param {Object} [instanceProps] Properties that will be added to the | ||
* prototype of the subclass. | ||
* @param {Object} [classProps] Properties that will be added as static | ||
* properties to the subclass itself. | ||
* @return {Object} Subclass of `Adapter`. | ||
* @class Adapter | ||
* @abstract | ||
* @extends Component | ||
* @param {Object} [opts] Configuration opts. | ||
* @param {boolean} [opts.debug=false] Whether to log debugging information. | ||
* @param {boolean} [opts.raw=false] Whether to return a more detailed response | ||
* object. | ||
*/ | ||
Adapter.extend = utils.extend | ||
export function Adapter (opts) { | ||
utils.classCallCheck(this, Adapter) | ||
Component.call(this) | ||
opts || (opts = {}) | ||
utils.fillIn(opts, DEFAULTS) | ||
utils.fillIn(this, opts) | ||
} | ||
utils.addHiddenPropsToTarget(Adapter.prototype, { | ||
Component.extend({ | ||
constructor: Adapter, | ||
/** | ||
@@ -602,12 +587,2 @@ * Lifecycle method method called by <a href="#count__anchor">count</a>. | ||
/** | ||
* Shortcut for `#log('debug'[, arg1[, arg2[, argn]]])`. | ||
* | ||
* @name Adapter#dbg | ||
* @method | ||
*/ | ||
dbg (...args) { | ||
this.log('debug', ...args) | ||
}, | ||
/** | ||
* Retrieve the number of records that match the selection query. Called by | ||
@@ -632,3 +607,2 @@ * `Mapper#count`. | ||
count (mapper, query, opts) { | ||
const self = this | ||
let op | ||
@@ -640,20 +614,20 @@ query || (query = {}) | ||
op = opts.op = 'beforeCount' | ||
return utils.resolve(self[op](mapper, query, opts)).then(function () { | ||
// Allow for re-assignment from lifecycle hook | ||
op = opts.op = 'count' | ||
self.dbg(op, mapper, query, opts) | ||
return utils.resolve(self._count(mapper, query, opts)) | ||
}).then(function (results) { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, op) | ||
response = self.respond(response, opts) | ||
// afterCount lifecycle hook | ||
op = opts.op = 'afterCount' | ||
return utils.resolve(self[op](mapper, query, opts, response)).then(function (_response) { | ||
return utils.resolve(this[op](mapper, query, opts)) | ||
.then(() => { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
op = opts.op = 'count' | ||
this.dbg(op, mapper, query, opts) | ||
return utils.resolve(this._count(mapper, query, opts)) | ||
}) | ||
}) | ||
.then((results) => { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, op) | ||
response = this.respond(response, opts) | ||
// afterCount lifecycle hook | ||
op = opts.op = 'afterCount' | ||
return utils.resolve(this[op](mapper, query, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}, | ||
@@ -674,3 +648,2 @@ | ||
create (mapper, props, opts) { | ||
const self = this | ||
let op | ||
@@ -682,23 +655,23 @@ props || (props = {}) | ||
op = opts.op = 'beforeCreate' | ||
return utils.resolve(self[op](mapper, props, opts)).then(function (_props) { | ||
// Allow for re-assignment from lifecycle hook | ||
props = utils.isUndefined(_props) ? props : _props | ||
props = withoutRelations(mapper, props, opts) | ||
op = opts.op = 'create' | ||
self.dbg(op, mapper, props, opts) | ||
return utils.resolve(self._create(mapper, props, opts)) | ||
}).then(function (results) { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, 'create') | ||
response.created = data ? 1 : 0 | ||
response = self.respond(response, opts) | ||
// afterCreate lifecycle hook | ||
op = opts.op = 'afterCreate' | ||
return utils.resolve(self[op](mapper, props, opts, response)).then(function (_response) { | ||
return utils.resolve(this[op](mapper, props, opts)) | ||
.then((_props) => { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
props = _props === undefined ? props : _props | ||
props = withoutRelations(mapper, props, opts) | ||
op = opts.op = 'create' | ||
this.dbg(op, mapper, props, opts) | ||
return utils.resolve(this._create(mapper, props, opts)) | ||
}) | ||
}) | ||
.then((results) => { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, 'create') | ||
response.created = data ? 1 : 0 | ||
response = this.respond(response, opts) | ||
// afterCreate lifecycle hook | ||
op = opts.op = 'afterCreate' | ||
return utils.resolve(this[op](mapper, props, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}, | ||
@@ -719,3 +692,2 @@ | ||
createMany (mapper, props, opts) { | ||
const self = this | ||
let op | ||
@@ -727,26 +699,24 @@ props || (props = {}) | ||
op = opts.op = 'beforeCreateMany' | ||
return utils.resolve(self[op](mapper, props, opts)).then(function (_props) { | ||
// Allow for re-assignment from lifecycle hook | ||
props = utils.isUndefined(_props) ? props : _props | ||
props = props.map(function (record) { | ||
return withoutRelations(mapper, record, opts) | ||
return utils.resolve(this[op](mapper, props, opts)) | ||
.then((_props) => { | ||
// Allow for re-assignment from lifecycle hook | ||
props = _props === undefined ? props : _props | ||
props = props.map((record) => withoutRelations(mapper, record, opts)) | ||
op = opts.op = 'createMany' | ||
this.dbg(op, mapper, props, opts) | ||
return utils.resolve(this._createMany(mapper, props, opts)) | ||
}) | ||
op = opts.op = 'createMany' | ||
self.dbg(op, mapper, props, opts) | ||
return utils.resolve(self._createMany(mapper, props, opts)) | ||
}).then(function (results) { | ||
let [data, result] = results | ||
data || (data = []) | ||
result || (result = {}) | ||
let response = new Response(data, result, 'createMany') | ||
response.created = data.length | ||
response = self.respond(response, opts) | ||
.then((results) => { | ||
let [data, result] = results | ||
data || (data = []) | ||
result || (result = {}) | ||
let response = new Response(data, result, 'createMany') | ||
response.created = data.length | ||
response = this.respond(response, opts) | ||
// afterCreateMany lifecycle hook | ||
op = opts.op = 'afterCreateMany' | ||
return utils.resolve(self[op](mapper, props, opts, response)).then(function (_response) { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
// afterCreateMany lifecycle hook | ||
op = opts.op = 'afterCreateMany' | ||
return utils.resolve(this[op](mapper, props, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}) | ||
}, | ||
@@ -768,3 +738,2 @@ | ||
destroy (mapper, id, opts) { | ||
const self = this | ||
let op | ||
@@ -775,19 +744,19 @@ opts || (opts = {}) | ||
op = opts.op = 'beforeDestroy' | ||
return utils.resolve(self[op](mapper, id, opts)).then(function () { | ||
op = opts.op = 'destroy' | ||
self.dbg(op, mapper, id, opts) | ||
return utils.resolve(self._destroy(mapper, id, opts)) | ||
}).then(function (results) { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, 'destroy') | ||
response = self.respond(response, opts) | ||
return utils.resolve(this[op](mapper, id, opts)) | ||
.then(() => { | ||
op = opts.op = 'destroy' | ||
this.dbg(op, mapper, id, opts) | ||
return utils.resolve(this._destroy(mapper, id, opts)) | ||
}) | ||
.then((results) => { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, 'destroy') | ||
response = this.respond(response, opts) | ||
// afterDestroy lifecycle hook | ||
op = opts.op = 'afterDestroy' | ||
return utils.resolve(self[op](mapper, id, opts, response)).then(function (_response) { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
// afterDestroy lifecycle hook | ||
op = opts.op = 'afterDestroy' | ||
return utils.resolve(this[op](mapper, id, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}) | ||
}, | ||
@@ -815,3 +784,2 @@ | ||
destroyAll (mapper, query, opts) { | ||
const self = this | ||
let op | ||
@@ -823,19 +791,19 @@ query || (query = {}) | ||
op = opts.op = 'beforeDestroyAll' | ||
return utils.resolve(self[op](mapper, query, opts)).then(function () { | ||
op = opts.op = 'destroyAll' | ||
self.dbg(op, mapper, query, opts) | ||
return utils.resolve(self._destroyAll(mapper, query, opts)) | ||
}).then(function (results) { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, 'destroyAll') | ||
response = self.respond(response, opts) | ||
return utils.resolve(this[op](mapper, query, opts)) | ||
.then(() => { | ||
op = opts.op = 'destroyAll' | ||
this.dbg(op, mapper, query, opts) | ||
return utils.resolve(this._destroyAll(mapper, query, opts)) | ||
}) | ||
.then((results) => { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, 'destroyAll') | ||
response = this.respond(response, opts) | ||
// afterDestroyAll lifecycle hook | ||
op = opts.op = 'afterDestroyAll' | ||
return utils.resolve(self[op](mapper, query, opts, response)).then(function (_response) { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
// afterDestroyAll lifecycle hook | ||
op = opts.op = 'afterDestroyAll' | ||
return utils.resolve(this[op](mapper, query, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}) | ||
}, | ||
@@ -853,3 +821,2 @@ | ||
loadBelongsTo (mapper, def, records, __opts) { | ||
const self = this | ||
const relationDef = def.getRelation() | ||
@@ -859,12 +826,11 @@ | ||
const record = records | ||
return self.find(relationDef, self.makeBelongsToForeignKey(mapper, def, record), __opts).then(function (relatedItem) { | ||
def.setLocalField(record, relatedItem) | ||
}) | ||
return this.find(relationDef, this.makeBelongsToForeignKey(mapper, def, record), __opts) | ||
.then((relatedItem) => { | ||
def.setLocalField(record, relatedItem) | ||
}) | ||
} else { | ||
const keys = records.map(function (record) { | ||
return self.makeBelongsToForeignKey(mapper, def, record) | ||
}).filter(function (key) { | ||
return key | ||
}) | ||
return self.findAll(relationDef, { | ||
const keys = records | ||
.map((record) => this.makeBelongsToForeignKey(mapper, def, record)) | ||
.filter((key) => key) | ||
return this.findAll(relationDef, { | ||
where: { | ||
@@ -875,5 +841,5 @@ [relationDef.idAttribute]: { | ||
} | ||
}, __opts).then(function (relatedItems) { | ||
records.forEach(function (record) { | ||
relatedItems.forEach(function (relatedItem) { | ||
}, __opts).then((relatedItems) => { | ||
records.forEach((record) => { | ||
relatedItems.forEach((relatedItem) => { | ||
if (relatedItem[relationDef.idAttribute] === record[def.foreignKey]) { | ||
@@ -902,3 +868,2 @@ def.setLocalField(record, relatedItem) | ||
find (mapper, id, opts) { | ||
const self = this | ||
let record, op | ||
@@ -910,47 +875,48 @@ opts || (opts = {}) | ||
op = opts.op = 'beforeFind' | ||
return utils.resolve(self[op](mapper, id, opts)).then(function () { | ||
op = opts.op = 'find' | ||
self.dbg(op, mapper, id, opts) | ||
return utils.resolve(self._find(mapper, id, opts)) | ||
}).then(function (results) { | ||
let [_record] = results | ||
if (!_record) { | ||
return | ||
} | ||
record = _record | ||
const tasks = [] | ||
return utils.resolve(this[op](mapper, id, opts)) | ||
.then(() => { | ||
op = opts.op = 'find' | ||
this.dbg(op, mapper, id, opts) | ||
return utils.resolve(this._find(mapper, id, opts)) | ||
}) | ||
.then((results) => { | ||
let [_record] = results | ||
if (!_record) { | ||
return | ||
} | ||
record = _record | ||
const tasks = [] | ||
utils.forEachRelation(mapper, opts, function (def, __opts) { | ||
let 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) | ||
utils.forEachRelation(mapper, opts, (def, __opts) => { | ||
let task | ||
if (def.foreignKey && (def.type === 'hasOne' || def.type === 'hasMany')) { | ||
if (def.type === 'hasOne') { | ||
task = this.loadHasOne(mapper, def, record, __opts) | ||
} else { | ||
task = this.loadHasMany(mapper, def, record, __opts) | ||
} | ||
} else if (def.type === 'hasMany' && def.localKeys) { | ||
task = this.loadHasManyLocalKeys(mapper, def, record, __opts) | ||
} else if (def.type === 'hasMany' && def.foreignKeys) { | ||
task = this.loadHasManyForeignKeys(mapper, def, record, __opts) | ||
} else if (def.type === 'belongsTo') { | ||
task = this.loadBelongsTo(mapper, def, record, __opts) | ||
} | ||
} else if (def.type === 'hasMany' && def.localKeys) { | ||
task = self.loadHasManyLocalKeys(mapper, def, record, __opts) | ||
} else if (def.type === 'hasMany' && def.foreignKeys) { | ||
task = self.loadHasManyForeignKeys(mapper, def, record, __opts) | ||
} else if (def.type === 'belongsTo') { | ||
task = self.loadBelongsTo(mapper, def, record, __opts) | ||
} | ||
if (task) { | ||
tasks.push(task) | ||
} | ||
if (task) { | ||
tasks.push(task) | ||
} | ||
}) | ||
return utils.Promise.all(tasks) | ||
}) | ||
.then(() => { | ||
let response = new Response(record, {}, 'find') | ||
response.found = record ? 1 : 0 | ||
response = this.respond(response, opts) | ||
return Promise.all(tasks) | ||
}).then(function () { | ||
let response = new Response(record, {}, 'find') | ||
response.found = record ? 1 : 0 | ||
response = self.respond(response, opts) | ||
// afterFind lifecycle hook | ||
op = opts.op = 'afterFind' | ||
return utils.resolve(self[op](mapper, id, opts, response)).then(function (_response) { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
// afterFind lifecycle hook | ||
op = opts.op = 'afterFind' | ||
return utils.resolve(this[op](mapper, id, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}) | ||
}, | ||
@@ -978,3 +944,2 @@ | ||
findAll (mapper, query, opts) { | ||
const self = this | ||
opts || (opts = {}) | ||
@@ -998,43 +963,44 @@ opts.with || (opts.with = []) | ||
op = opts.op = 'beforeFindAll' | ||
return utils.resolve(self[op](mapper, query, opts)).then(function () { | ||
op = opts.op = 'findAll' | ||
self.dbg(op, mapper, query, opts) | ||
return utils.resolve(self._findAll(mapper, query, opts)) | ||
}).then(function (results) { | ||
let [_records] = results | ||
_records || (_records = []) | ||
records = _records | ||
const tasks = [] | ||
utils.forEachRelation(mapper, opts, function (def, __opts) { | ||
let task | ||
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) | ||
return utils.resolve(this[op](mapper, query, opts)) | ||
.then(() => { | ||
op = opts.op = 'findAll' | ||
this.dbg(op, mapper, query, opts) | ||
return utils.resolve(this._findAll(mapper, query, opts)) | ||
}) | ||
.then((results) => { | ||
let [_records] = results | ||
_records || (_records = []) | ||
records = _records | ||
const tasks = [] | ||
utils.forEachRelation(mapper, opts, (def, __opts) => { | ||
let task | ||
if (def.foreignKey && (def.type === 'hasOne' || def.type === 'hasMany')) { | ||
if (def.type === 'hasMany') { | ||
task = this.loadHasMany(mapper, def, records, __opts) | ||
} else { | ||
task = this.loadHasOne(mapper, def, records, __opts) | ||
} | ||
} else if (def.type === 'hasMany' && def.localKeys) { | ||
task = this.loadHasManyLocalKeys(mapper, def, records, __opts) | ||
} else if (def.type === 'hasMany' && def.foreignKeys) { | ||
task = this.loadHasManyForeignKeys(mapper, def, records, __opts) | ||
} else if (def.type === 'belongsTo') { | ||
task = this.loadBelongsTo(mapper, def, records, __opts) | ||
} | ||
} else if (def.type === 'hasMany' && def.localKeys) { | ||
task = self.loadHasManyLocalKeys(mapper, def, records, __opts) | ||
} else if (def.type === 'hasMany' && def.foreignKeys) { | ||
task = self.loadHasManyForeignKeys(mapper, def, records, __opts) | ||
} else if (def.type === 'belongsTo') { | ||
task = self.loadBelongsTo(mapper, def, records, __opts) | ||
} | ||
if (task) { | ||
tasks.push(task) | ||
} | ||
if (task) { | ||
tasks.push(task) | ||
} | ||
}) | ||
return utils.Promise.all(tasks) | ||
}) | ||
return Promise.all(tasks) | ||
}).then(function () { | ||
let response = new Response(records, {}, 'findAll') | ||
response.found = records.length | ||
response = self.respond(response, opts) | ||
.then(() => { | ||
let response = new Response(records, {}, 'findAll') | ||
response.found = records.length | ||
response = this.respond(response, opts) | ||
// afterFindAll lifecycle hook | ||
op = opts.op = 'afterFindAll' | ||
return utils.resolve(self[op](mapper, query, opts, response)).then(function (_response) { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
// afterFindAll lifecycle hook | ||
op = opts.op = 'afterFindAll' | ||
return utils.resolve(this[op](mapper, query, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}) | ||
}, | ||
@@ -1054,3 +1020,3 @@ | ||
opts || (opts = {}) | ||
return utils.isUndefined(opts[opt]) ? utils.plainCopy(this[opt]) : utils.plainCopy(opts[opt]) | ||
return opts[opt] === undefined ? utils.plainCopy(this[opt]) : utils.plainCopy(opts[opt]) | ||
}, | ||
@@ -1068,3 +1034,2 @@ | ||
loadHasMany (mapper, def, records, __opts) { | ||
const self = this | ||
let singular = false | ||
@@ -1076,5 +1041,3 @@ | ||
} | ||
const IDs = records.map(function (record) { | ||
return self.makeHasManyForeignKey(mapper, def, record) | ||
}) | ||
const IDs = records.map((record) => this.makeHasManyForeignKey(mapper, def, record)) | ||
const query = { | ||
@@ -1088,8 +1051,6 @@ where: {} | ||
} else { | ||
criteria['in'] = IDs.filter(function (id) { | ||
return id | ||
}) | ||
criteria['in'] = IDs.filter((id) => id) | ||
} | ||
return self.findAll(def.getRelation(), query, __opts).then(function (relatedItems) { | ||
records.forEach(function (record) { | ||
return this.findAll(def.getRelation(), query, __opts).then((relatedItems) => { | ||
records.forEach((record) => { | ||
let attached = [] | ||
@@ -1100,3 +1061,3 @@ // avoid unneccesary iteration when we only have one record | ||
} else { | ||
relatedItems.forEach(function (relatedItem) { | ||
relatedItems.forEach((relatedItem) => { | ||
if (utils.get(relatedItem, def.foreignKey) === record[mapper.idAttribute]) { | ||
@@ -1113,3 +1074,2 @@ attached.push(relatedItem) | ||
loadHasManyLocalKeys (mapper, def, records, __opts) { | ||
const self = this | ||
let record | ||
@@ -1123,9 +1083,9 @@ const relatedMapper = def.getRelation() | ||
if (record) { | ||
return self.findAll(relatedMapper, { | ||
return this.findAll(relatedMapper, { | ||
where: { | ||
[relatedMapper.idAttribute]: { | ||
'in': self.makeHasManyLocalKeys(mapper, def, record) | ||
'in': this.makeHasManyLocalKeys(mapper, def, record) | ||
} | ||
} | ||
}, __opts).then(function (relatedItems) { | ||
}, __opts).then((relatedItems) => { | ||
def.setLocalField(record, relatedItems) | ||
@@ -1135,17 +1095,17 @@ }) | ||
let localKeys = [] | ||
records.forEach(function (record) { | ||
localKeys = localKeys.concat(self.self.makeHasManyLocalKeys(mapper, def, record)) | ||
records.forEach((record) => { | ||
localKeys = localKeys.concat(this.makeHasManyLocalKeys(mapper, def, record)) | ||
}) | ||
return self.findAll(relatedMapper, { | ||
return this.findAll(relatedMapper, { | ||
where: { | ||
[relatedMapper.idAttribute]: { | ||
'in': unique(localKeys).filter(function (x) { return x }) | ||
'in': unique(localKeys).filter((x) => x) | ||
} | ||
} | ||
}, __opts).then(function (relatedItems) { | ||
records.forEach(function (item) { | ||
}, __opts).then((relatedItems) => { | ||
records.forEach((item) => { | ||
let attached = [] | ||
let itemKeys = utils.get(item, def.localKeys) || [] | ||
itemKeys = utils.isArray(itemKeys) ? itemKeys : Object.keys(itemKeys) | ||
relatedItems.forEach(function (relatedItem) { | ||
relatedItems.forEach((relatedItem) => { | ||
if (itemKeys && itemKeys.indexOf(relatedItem[relatedMapper.idAttribute]) !== -1) { | ||
@@ -1163,3 +1123,2 @@ attached.push(relatedItem) | ||
loadHasManyForeignKeys (mapper, def, records, __opts) { | ||
const self = this | ||
const relatedMapper = def.getRelation() | ||
@@ -1174,26 +1133,24 @@ const idAttribute = mapper.idAttribute | ||
if (record) { | ||
return self.findAll(def.getRelation(), { | ||
return this.findAll(def.getRelation(), { | ||
where: { | ||
[def.foreignKeys]: { | ||
'contains': self.makeHasManyForeignKeys(mapper, def, record) | ||
'contains': this.makeHasManyForeignKeys(mapper, def, record) | ||
} | ||
} | ||
}, __opts).then(function (relatedItems) { | ||
}, __opts).then((relatedItems) => { | ||
def.setLocalField(record, relatedItems) | ||
}) | ||
} else { | ||
return self.findAll(relatedMapper, { | ||
return this.findAll(relatedMapper, { | ||
where: { | ||
[def.foreignKeys]: { | ||
'isectNotEmpty': records.map(function (record) { | ||
return self.makeHasManyForeignKeys(mapper, def, record) | ||
}) | ||
'isectNotEmpty': records.map((record) => this.makeHasManyForeignKeys(mapper, def, record)) | ||
} | ||
} | ||
}, __opts).then(function (relatedItems) { | ||
}, __opts).then((relatedItems) => { | ||
const foreignKeysField = def.foreignKeys | ||
records.forEach(function (record) { | ||
records.forEach((record) => { | ||
const _relatedItems = [] | ||
const id = utils.get(record, idAttribute) | ||
relatedItems.forEach(function (relatedItem) { | ||
relatedItems.forEach((relatedItem) => { | ||
const foreignKeys = utils.get(relatedItems, foreignKeysField) || [] | ||
@@ -1223,4 +1180,4 @@ if (foreignKeys.indexOf(id) !== -1) { | ||
} | ||
return this.loadHasMany(mapper, def, records, __opts).then(function () { | ||
records.forEach(function (record) { | ||
return this.loadHasMany(mapper, def, records, __opts).then(() => { | ||
records.forEach((record) => { | ||
const relatedData = def.getLocalField(record) | ||
@@ -1235,27 +1192,2 @@ if (utils.isArray(relatedData) && relatedData.length) { | ||
/** | ||
* Logging utility method. Override this method if you want to send log | ||
* messages to something other than the console. | ||
* | ||
* @name Adapter#log | ||
* @method | ||
* @param {string} level Log level. | ||
* @param {...*} values Values to log. | ||
*/ | ||
log (level, ...args) { | ||
if (level && !args.length) { | ||
args.push(level) | ||
level = 'debug' | ||
} | ||
if (level === 'debug' && !this.debug) { | ||
return | ||
} | ||
const prefix = `${level.toUpperCase()}: (Adapter)` | ||
if (console[level]) { | ||
console[level](prefix, ...args) | ||
} else { | ||
console.log(prefix, ...args) | ||
} | ||
}, | ||
/** | ||
* Return the foreignKey from the given record for the provided relationship. | ||
@@ -1290,3 +1222,3 @@ * | ||
localKeys = localKeys.concat(itemKeys) | ||
return unique(localKeys).filter(function (x) { return x }) | ||
return unique(localKeys).filter((x) => x) | ||
}, | ||
@@ -1341,3 +1273,2 @@ | ||
sum (mapper, field, query, opts) { | ||
const self = this | ||
let op | ||
@@ -1352,20 +1283,20 @@ if (!utils.isString(field)) { | ||
op = opts.op = 'beforeSum' | ||
return utils.resolve(self[op](mapper, field, query, opts)).then(function () { | ||
// Allow for re-assignment from lifecycle hook | ||
op = opts.op = 'sum' | ||
self.dbg(op, mapper, field, query, opts) | ||
return utils.resolve(self._sum(mapper, field, query, opts)) | ||
}).then(function (results) { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, op) | ||
response = self.respond(response, opts) | ||
// afterSum lifecycle hook | ||
op = opts.op = 'afterSum' | ||
return utils.resolve(self[op](mapper, field, query, opts, response)).then(function (_response) { | ||
return utils.resolve(this[op](mapper, field, query, opts)) | ||
.then(() => { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
op = opts.op = 'sum' | ||
this.dbg(op, mapper, field, query, opts) | ||
return utils.resolve(this._sum(mapper, field, query, opts)) | ||
}) | ||
}) | ||
.then((results) => { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, op) | ||
response = this.respond(response, opts) | ||
// afterSum lifecycle hook | ||
op = opts.op = 'afterSum' | ||
return utils.resolve(this[op](mapper, field, query, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}, | ||
@@ -1400,3 +1331,2 @@ | ||
update (mapper, id, props, opts) { | ||
const self = this | ||
props || (props = {}) | ||
@@ -1408,23 +1338,23 @@ opts || (opts = {}) | ||
op = opts.op = 'beforeUpdate' | ||
return utils.resolve(self[op](mapper, id, props, opts)).then(function (_props) { | ||
// Allow for re-assignment from lifecycle hook | ||
props = utils.isUndefined(_props) ? props : _props | ||
props = withoutRelations(mapper, props, opts) | ||
op = opts.op = 'update' | ||
self.dbg(op, mapper, id, props, opts) | ||
return utils.resolve(self._update(mapper, id, props, opts)) | ||
}).then(function (results) { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, 'update') | ||
response.updated = data ? 1 : 0 | ||
response = self.respond(response, opts) | ||
// afterUpdate lifecycle hook | ||
op = opts.op = 'afterUpdate' | ||
return utils.resolve(self[op](mapper, id, props, opts, response)).then(function (_response) { | ||
return utils.resolve(this[op](mapper, id, props, opts)) | ||
.then((_props) => { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
props = _props === undefined ? props : _props | ||
props = withoutRelations(mapper, props, opts) | ||
op = opts.op = 'update' | ||
this.dbg(op, mapper, id, props, opts) | ||
return utils.resolve(this._update(mapper, id, props, opts)) | ||
}) | ||
}) | ||
.then((results) => { | ||
let [data, result] = results | ||
result || (result = {}) | ||
let response = new Response(data, result, 'update') | ||
response.updated = data ? 1 : 0 | ||
response = this.respond(response, opts) | ||
// afterUpdate lifecycle hook | ||
op = opts.op = 'afterUpdate' | ||
return utils.resolve(this[op](mapper, id, props, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}, | ||
@@ -1453,3 +1383,2 @@ | ||
updateAll (mapper, props, query, opts) { | ||
const self = this | ||
props || (props = {}) | ||
@@ -1462,24 +1391,24 @@ query || (query = {}) | ||
op = opts.op = 'beforeUpdateAll' | ||
return utils.resolve(self[op](mapper, props, query, opts)).then(function (_props) { | ||
// Allow for re-assignment from lifecycle hook | ||
props = utils.isUndefined(_props) ? props : _props | ||
props = withoutRelations(mapper, props, opts) | ||
op = opts.op = 'updateAll' | ||
self.dbg(op, mapper, props, query, opts) | ||
return utils.resolve(self._updateAll(mapper, props, query, opts)) | ||
}).then(function (results) { | ||
let [data, result] = results | ||
data || (data = []) | ||
result || (result = {}) | ||
let response = new Response(data, result, 'updateAll') | ||
response.updated = data.length | ||
response = self.respond(response, opts) | ||
// afterUpdateAll lifecycle hook | ||
op = opts.op = 'afterUpdateAll' | ||
return utils.resolve(self[op](mapper, props, query, opts, response)).then(function (_response) { | ||
return utils.resolve(this[op](mapper, props, query, opts)) | ||
.then((_props) => { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
props = _props === undefined ? props : _props | ||
props = withoutRelations(mapper, props, opts) | ||
op = opts.op = 'updateAll' | ||
this.dbg(op, mapper, props, query, opts) | ||
return utils.resolve(this._updateAll(mapper, props, query, opts)) | ||
}) | ||
}) | ||
.then((results) => { | ||
let [data, result] = results | ||
data || (data = []) | ||
result || (result = {}) | ||
let response = new Response(data, result, 'updateAll') | ||
response.updated = data.length | ||
response = this.respond(response, opts) | ||
// afterUpdateAll lifecycle hook | ||
op = opts.op = 'afterUpdateAll' | ||
return utils.resolve(this[op](mapper, props, query, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}, | ||
@@ -1500,3 +1429,2 @@ | ||
updateMany (mapper, records, opts) { | ||
const self = this | ||
records || (records = []) | ||
@@ -1507,33 +1435,82 @@ opts || (opts = {}) | ||
records = records.filter(function (record) { | ||
return utils.get(record, idAttribute) | ||
}) | ||
records = records.filter((record) => utils.get(record, idAttribute)) | ||
// beforeUpdateMany lifecycle hook | ||
op = opts.op = 'beforeUpdateMany' | ||
return utils.resolve(self[op](mapper, records, opts)).then(function (_records) { | ||
// Allow for re-assignment from lifecycle hook | ||
records = utils.isUndefined(_records) ? records : _records | ||
records = records.map(function (record) { | ||
return withoutRelations(mapper, record, opts) | ||
return utils.resolve(this[op](mapper, records, opts)) | ||
.then((_records) => { | ||
// Allow for re-assignment from lifecycle hook | ||
records = _records === undefined ? records : _records | ||
records = records.map((record) => withoutRelations(mapper, record, opts)) | ||
op = opts.op = 'updateMany' | ||
this.dbg(op, mapper, records, opts) | ||
return utils.resolve(this._updateMany(mapper, records, opts)) | ||
}) | ||
op = opts.op = 'updateMany' | ||
self.dbg(op, mapper, records, opts) | ||
return utils.resolve(self._updateMany(mapper, records, opts)) | ||
}).then(function (results) { | ||
let [data, result] = results | ||
data || (data = []) | ||
result || (result = {}) | ||
let response = new Response(data, result, 'updateMany') | ||
response.updated = data.length | ||
response = self.respond(response, opts) | ||
.then((results) => { | ||
let [data, result] = results | ||
data || (data = []) | ||
result || (result = {}) | ||
let response = new Response(data, result, 'updateMany') | ||
response.updated = data.length | ||
response = this.respond(response, opts) | ||
// afterUpdateMany lifecycle hook | ||
op = opts.op = 'afterUpdateMany' | ||
return utils.resolve(self[op](mapper, records, opts, response)).then(function (_response) { | ||
// Allow for re-assignment from lifecycle hook | ||
return utils.isUndefined(_response) ? response : _response | ||
// afterUpdateMany lifecycle hook | ||
op = opts.op = 'afterUpdateMany' | ||
return utils.resolve(this[op](mapper, records, opts, response)) | ||
.then((_response) => _response === undefined ? response : _response) | ||
}) | ||
}) | ||
} | ||
}) | ||
/** | ||
* Create a subclass of this Adapter: | ||
* | ||
* @example <caption>Adapter.extend</caption> | ||
* // Normally you would do: import {Adapter} from 'js-data' | ||
* const JSData = require('js-data@3.0.0-beta.10') | ||
* const {Adapter} = JSData | ||
* console.log('Using JSData v' + JSData.version.full) | ||
* | ||
* // Extend the class using ES2015 class syntax. | ||
* class CustomAdapterClass extends Adapter { | ||
* foo () { return 'bar' } | ||
* static beep () { return 'boop' } | ||
* } | ||
* const customAdapter = new CustomAdapterClass() | ||
* console.log(customAdapter.foo()) | ||
* console.log(CustomAdapterClass.beep()) | ||
* | ||
* // Extend the class using alternate method. | ||
* const OtherAdapterClass = Adapter.extend({ | ||
* foo () { return 'bar' } | ||
* }, { | ||
* beep () { return 'boop' } | ||
* }) | ||
* const otherAdapter = new OtherAdapterClass() | ||
* console.log(otherAdapter.foo()) | ||
* console.log(OtherAdapterClass.beep()) | ||
* | ||
* // Extend the class, providing a custom constructor. | ||
* function AnotherAdapterClass () { | ||
* Adapter.call(this) | ||
* this.created_at = new Date().getTime() | ||
* } | ||
* Adapter.extend({ | ||
* constructor: AnotherAdapterClass, | ||
* foo () { return 'bar' } | ||
* }, { | ||
* beep () { return 'boop' } | ||
* }) | ||
* const anotherAdapter = new AnotherAdapterClass() | ||
* console.log(anotherAdapter.created_at) | ||
* console.log(anotherAdapter.foo()) | ||
* console.log(AnotherAdapterClass.beep()) | ||
* | ||
* @method Adapter.extend | ||
* @param {Object} [props={}] Properties to add to the prototype of the | ||
* subclass. | ||
* @param {Object} [props.constructor] Provide a custom constructor function | ||
* to be used as the subclass itself. | ||
* @param {Object} [classProps={}] Static properties to add to the subclass. | ||
* @returns {Constructor} Subclass of this Adapter class. | ||
*/ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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 README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
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
753667
9071
1
0