Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_patch (id, raw, params = {}) {
// Do not allow to patch the id
const data = _.omit(raw, this.id);
// By default we will just query for the one id. For multi patch
// we create a list of the ids of all items that will be changed
// to re-query them after the update
return this._findOrGet(id, Object.assign({}, params, {
query: _.extend({}, params.query, { $select: [`${this.table}.${this.id}`] })
})).then(results => {
const idList = results.map(current => current[this.id]);
const query = {
[`${this.table}.${this.id}`]: { $in: idList }
};
const q = this.knexify(this.db(params), query);
const findParams = Object.assign({}, params, {
query: Object.assign({}, params.query, query)
});
return q.update(data).then(() => {
return this._findOrGet(null, findParams).then(items => {
if (id !== null) {
if (items.length === 1) {
return items[0];
} else {
async _findOrGet (id, params = {}) {
if (id === null) {
return this._find(_.extend({}, params, {
paginate: false
}));
}
return this._get(id, params);
}
async _create (data, params = {}) {
if (Array.isArray(data)) {
return Promise.all(data.map(current => this._create(current, params)));
}
const id = data[this.id] || this._uId++;
const current = _.extend({}, data, { [this.id]: id });
const result = (this.store[id] = current);
return _select(result, params, this.id);
}
constructor (options = {}) {
super(_.extend({
id: 'id',
matcher: sift,
sorter
}, options));
this._uId = options.startId || 0;
this.store = options.store || {};
}