Skip to content

Commit

Permalink
small refacto of findOne + fix deleteMany of connector-mongoose
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre No毛l <pierre.noel@strapi.io>

Signed-off-by: Pierre No毛l <pierre.noel@strapi.io>
  • Loading branch information
Pierre No毛l committed Mar 9, 2020
1 parent b5ec9cb commit b5f9795
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
20 changes: 8 additions & 12 deletions packages/strapi-connector-bookshelf/lib/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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');
Expand All @@ -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 });
Expand All @@ -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) {
Expand Down
11 changes: 5 additions & 6 deletions packages/strapi-connector-mongoose/lib/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit b5f9795

Please sign in to comment.