Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
where: Object.assign({
[this.Op.and]: { [this.id]: id }
}, where)
}, params.sequelize);
const Model = this.applyScope(params);
// findById calls findAll under the hood. We use findAll so that
// eager loading can be used without a separate code path.
return Model.findAll(q).then(result => {
if (result.length === 0) {
throw new errors.NotFound(`No record found for id '${id}'`);
}
return result[0];
}).then(select(params, this.id)).catch(utils.errorHandler);
}
function makeSorter (query, options) {
// The sort function (if $sort is set)
const sorter = query.$sort ? createSorter(query.$sort) : createSorter({
[options.idField]: 1
});
return function (result) {
const isPaginated = !!result[options.dataField];
let data = isPaginated ? result[options.dataField] : result;
if (sorter) {
data = data.sort(sorter);
}
const limit = typeof result.limit === 'number' ? result.limit : parseInt(query.$limit, 10);
if (limit && !isNaN(limit) && limit !== -1) {
data = data.slice(0, limit);
}
let runQuery = total => nfcall(q, 'exec').then(data => {
return {
total,
limit: filters.$limit,
skip: filters.$skip || 0,
data: data.map(select(params, this.id))
};
});
where[this.Op.and] = { [this.id]: id };
}
const options = Object.assign({}, { where }, params.sequelize);
const Model = this.applyScope(params);
if (params.$returning !== false) {
return this._getOrFind(id, opts).then(data => {
return Model.destroy(options).then(() => data);
})
.then(select(params, this.id))
.catch(utils.errorHandler);
} else {
return Model.destroy(options).then(() => Promise.resolve([]))
.then(select(params, this.id))
.catch(utils.errorHandler);
}
}
}
const isMulti = Array.isArray(_data);
const data = isMulti ? _data : [_data];
return model.create(data, params.mongoose).then(results => {
if ($populate) {
return Promise.all(results.map(result => this.Model.populate(result, $populate)));
}
return results;
}).then(results => {
if (this.lean) {
results = results.map(item => (item.toObject ? item.toObject() : item));
}
return isMulti ? results : results[0];
}).then(select(params, this.id)).catch(errorHandler);
}
_remove (id, params = {}) {
const { query, options } = this.multiOptions(id, params);
return this._findOrGet(id, params).then(items =>
nfcall(this.getModel(params), 'remove', query, options)
.then(() => items)
).then(select(params, this.id));
}
}
_get (id, params = {}) {
const { query } = this.filterQuery(params);
query.$and = (query.$and || []).concat({ [this.id]: this._objectifyId(id) });
return this.Model.findOne(query).then(data => {
if (!data) {
throw new errors.NotFound(`No record found for id '${id}'`);
}
return data;
}).then(select(params, this.id)).catch(errorHandler);
}
async _get (id, params = {}) {
const { query } = this.filterQuery(params);
const findOptions = Object.assign({ $and: [{ [this.id]: id }, query] });
return nfcall(this.getModel(params), 'findOne', findOptions)
.then(doc => {
if (!doc) {
throw new errors.NotFound(`No record found for id '${id}'`);
}
return doc;
})
.then(select(params, this.id));
}
.then((result) => {
if (result === null) {
throw new errors.NotFound(`No record found for id '${id}'`, query);
}
return this.Model.deleteOne(query, params.mongoose).lean(this.lean)
.exec()
.then(() => result)
.then(select(params, this.id));
})
.catch(errorHandler);
_create (raw, params = {}) {
const addId = item => {
if (this.id !== '_id' && item[this.id] === undefined) {
return Object.assign({
[this.id]: crypto.randomBytes(8).toString('hex')
}, item);
}
return item;
};
const data = Array.isArray(raw) ? raw.map(addId) : addId(raw);
return nfcall(this.getModel(params), 'insert', data)
.then(select(params, this.id));
}