Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_normalizeId (id, data) {
if (this.id === '_id') {
// Default Mongo IDs cannot be updated. The Mongo library handles
// this automatically.
return _.omit(data, this.id);
} else if (id !== null) {
// If not using the default Mongo _id field set the ID to its
// previous value. This prevents orphaned documents.
return Object.assign({}, data, { [this.id]: id });
} else {
return data;
}
}
}
const { query, filters } = this.filterQuery(params);
const options = Object.assign({
new: true,
overwrite: this.overwrite,
runValidators: true,
context: 'query',
setDefaultsOnInsert: true
}, params.mongoose);
query.$and = (query.$and || []).concat({ [this.id]: id });
if (this.id === '_id') {
// We can not update default mongo ids
data = _.omit(data, this.id);
} else {
// If not using the default Mongo _id field set the id to its
// previous value. This prevents orphaned documents.
data = Object.assign({}, data, { [this.id]: id });
}
const discriminator = query[this.discriminatorKey] || this.discriminatorKey;
const model = this.discriminators[discriminator] || this.Model;
let modelQuery = model.findOneAndUpdate(query, data, options);
if (filters.$populate) {
modelQuery = modelQuery.populate(filters.$populate);
}
return modelQuery.lean(this.lean).exec()
.then(result => {
_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 ids.then(idList => {
// Create a new query that re-queries all ids that
// were originally changed
const findParams = Object.assign({}, params, Object.assign({}, {
query: Object.assign({ [this.id]: { $in: idList } }, params.query)
}));
return Model.update(_.omit(data, this.id), options)
.then(() => {
if (params.$returning !== false) {
return this._getOrFind(id, findParams);
} else {
return Promise.resolve([]);
}
});
}).then(select(params, this.id)).catch(utils.errorHandler);
}
const patchEntry = entry => {
const currentId = entry[this.id];
this.store[currentId] = _.extend(this.store[currentId], _.omit(data, this.id));
return _select(this.store[currentId], params, this.id);
};
emit (event, data, ctx) {
const disabled = ctx && ctx[SYNC] === false;
if (!service._serviceEvents.includes(event) || disabled) {
debug(`Passing through non-service event '${path} ${event}'`);
return this._emit(event, data, ctx);
}
const context = hooks.isHookObject(ctx)
? _.omit(ctx, 'app', 'service') : ctx;
debug(`Sending sync-out event '${path} ${event}'`);
return app.emit('sync-out', app.sync.serialize({
event, path, data, context
}));
}
});
_update (id, data, params = {}) {
const { query, options } = this.multiOptions(id, params);
const entry = _.omit(data, '_id');
if (this.id !== '_id' || (params.nedb && params.nedb.upsert)) {
entry[this.id] = id;
}
return nfcall(this.getModel(params), 'update', query, entry, options)
.then(() => this._findOrGet(id, params))
.then(select(params, this.id));
}