diff --git a/packages/strapi-connector-bookshelf/lib/queries.js b/packages/strapi-connector-bookshelf/lib/queries.js index 9411cdc834..94ccdfa5ce 100644 --- a/packages/strapi-connector-bookshelf/lib/queries.js +++ b/packages/strapi-connector-bookshelf/lib/queries.js @@ -46,12 +46,8 @@ module.exports = function createQueryBuilder({ model, modelKey, strapi }) { * Find one entry based on params */ async function findOne(params, populate, { transacting } = {}) { - const entry = await model.where(params).fetch({ - withRelated: populate, - transacting, - }); - - return entry ? entry.toJSON() : null; + const entries = await find({ ...params, _limit: 1 }, populate, { transacting }); + return entries[0] || null; } /** @@ -127,8 +123,8 @@ module.exports = function createQueryBuilder({ model, modelKey, strapi }) { return wrapTransaction(runUpdate, { transacting }); } - async function deleteOne(params, { transacting } = {}) { - const entry = await model.where(params).fetch({ transacting }); + async function deleteOne(id, { transacting } = {}) { + const entry = await model.where({ [model.primaryKey]: id }).fetch({ transacting }); if (!entry) { const err = new Error('entry.notFound'); @@ -155,7 +151,7 @@ module.exports = function createQueryBuilder({ model, modelKey, strapi }) { } }); - await model.updateRelations({ ...params, values }, { transacting }); + await model.updateRelations({ [model.primaryKey]: id, values }, { transacting }); const runDelete = async trx => { await deleteComponents(entry, { transacting: trx }); @@ -168,15 +164,15 @@ module.exports = function createQueryBuilder({ model, modelKey, strapi }) { async function deleteMany(params, { transacting } = {}) { if (params[model.primaryKey]) { - const entries = await find(params, null, { transacting }); + const entries = await find({ ...params, _limit: 1 }, null, { transacting }); if (entries.length > 0) { - return deleteOne({ id: entries[0][model.primaryKey] }, { transacting }); + return deleteOne(entries[0][model.primaryKey], { transacting }); } return new Promise(resolve => resolve); } const entries = await find(params, null, { transacting }); - return await Promise.all(entries.map(entry => deleteOne({ id: entry.id }, { transacting }))); + return Promise.all(entries.map(entry => deleteOne(entry.id, { transacting }))); } function search(params, populate) { diff --git a/packages/strapi-connector-mongoose/lib/queries.js b/packages/strapi-connector-mongoose/lib/queries.js index 17a3b05649..b278587812 100644 --- a/packages/strapi-connector-mongoose/lib/queries.js +++ b/packages/strapi-connector-mongoose/lib/queries.js @@ -314,7 +314,7 @@ module.exports = ({ model, modelKey, strapi }) => { .filter(el => el.ref) .map(el => el.ref._id); - // verify the provided ids are realted to this entity. + // verify the provided ids are related to this entity. idsToKeep.forEach(id => { if (allIds.findIndex(currentId => currentId.toString() === id) === -1) { const err = new Error( @@ -396,9 +396,8 @@ module.exports = ({ model, modelKey, strapi }) => { } async function findOne(params, populate) { - const entry = await model.findOne(params).populate(populate || defaultPopulate); - - return entry ? entry.toObject() : null; + const entries = await find({ ...params, _limit: 1 }, populate); + return entries[0] || null; } function count(params) { @@ -451,9 +450,9 @@ module.exports = ({ model, modelKey, strapi }) => { async function deleteMany(params) { if (params[model.primaryKey]) { - const entries = await find(params); + const entries = await find({ ...params, _limit: 1 }); if (entries.length > 0) { - return deleteOne({ id: entries[0][model.primaryKey] }); + return deleteOne(entries[0][model.primaryKey]); } return new Promise(resolve => resolve); }