mirror of
https://github.com/strapi/strapi.git
synced 2025-08-13 19:27:34 +00:00
small refacto of findOne + fix deleteMany of connector-mongoose
Signed-off-by: Pierre Noël <pierre.noel@strapi.io> Signed-off-by: Pierre Noël <pierre.noel@strapi.io>
This commit is contained in:
parent
b5ec9cb1c8
commit
b5f9795dbc
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user