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);
}
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));
}
return this._get(id, { sequelize: seqOptions, query: where }).then(instance => {
const copy = Object.keys(instance.toJSON()).reduce((result, key) => {
result[key] = typeof data[key] === 'undefined' ? null : data[key];
return result;
}, {});
return instance.update(copy, seqOptions)
.then(() => this._get(id, {
sequelize: Object.assign({}, seqOptions, {
raw: this.raw
})
}));
})
.then(select(params, this.id))
.catch(utils.errorHandler);
}