mirror of
https://github.com/strapi/strapi.git
synced 2025-08-14 11:48:43 +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
|
* Find one entry based on params
|
||||||
*/
|
*/
|
||||||
async function findOne(params, populate, { transacting } = {}) {
|
async function findOne(params, populate, { transacting } = {}) {
|
||||||
const entry = await model.where(params).fetch({
|
const entries = await find({ ...params, _limit: 1 }, populate, { transacting });
|
||||||
withRelated: populate,
|
return entries[0] || null;
|
||||||
transacting,
|
|
||||||
});
|
|
||||||
|
|
||||||
return entry ? entry.toJSON() : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -127,8 +123,8 @@ module.exports = function createQueryBuilder({ model, modelKey, strapi }) {
|
|||||||
return wrapTransaction(runUpdate, { transacting });
|
return wrapTransaction(runUpdate, { transacting });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteOne(params, { transacting } = {}) {
|
async function deleteOne(id, { transacting } = {}) {
|
||||||
const entry = await model.where(params).fetch({ transacting });
|
const entry = await model.where({ [model.primaryKey]: id }).fetch({ transacting });
|
||||||
|
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
const err = new Error('entry.notFound');
|
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 => {
|
const runDelete = async trx => {
|
||||||
await deleteComponents(entry, { transacting: trx });
|
await deleteComponents(entry, { transacting: trx });
|
||||||
@ -168,15 +164,15 @@ module.exports = function createQueryBuilder({ model, modelKey, strapi }) {
|
|||||||
|
|
||||||
async function deleteMany(params, { transacting } = {}) {
|
async function deleteMany(params, { transacting } = {}) {
|
||||||
if (params[model.primaryKey]) {
|
if (params[model.primaryKey]) {
|
||||||
const entries = await find(params, null, { transacting });
|
const entries = await find({ ...params, _limit: 1 }, null, { transacting });
|
||||||
if (entries.length > 0) {
|
if (entries.length > 0) {
|
||||||
return deleteOne({ id: entries[0][model.primaryKey] }, { transacting });
|
return deleteOne(entries[0][model.primaryKey], { transacting });
|
||||||
}
|
}
|
||||||
return new Promise(resolve => resolve);
|
return new Promise(resolve => resolve);
|
||||||
}
|
}
|
||||||
|
|
||||||
const entries = await find(params, null, { transacting });
|
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) {
|
function search(params, populate) {
|
||||||
|
@ -314,7 +314,7 @@ module.exports = ({ model, modelKey, strapi }) => {
|
|||||||
.filter(el => el.ref)
|
.filter(el => el.ref)
|
||||||
.map(el => el.ref._id);
|
.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 => {
|
idsToKeep.forEach(id => {
|
||||||
if (allIds.findIndex(currentId => currentId.toString() === id) === -1) {
|
if (allIds.findIndex(currentId => currentId.toString() === id) === -1) {
|
||||||
const err = new Error(
|
const err = new Error(
|
||||||
@ -396,9 +396,8 @@ module.exports = ({ model, modelKey, strapi }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function findOne(params, populate) {
|
async function findOne(params, populate) {
|
||||||
const entry = await model.findOne(params).populate(populate || defaultPopulate);
|
const entries = await find({ ...params, _limit: 1 }, populate);
|
||||||
|
return entries[0] || null;
|
||||||
return entry ? entry.toObject() : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function count(params) {
|
function count(params) {
|
||||||
@ -451,9 +450,9 @@ module.exports = ({ model, modelKey, strapi }) => {
|
|||||||
|
|
||||||
async function deleteMany(params) {
|
async function deleteMany(params) {
|
||||||
if (params[model.primaryKey]) {
|
if (params[model.primaryKey]) {
|
||||||
const entries = await find(params);
|
const entries = await find({ ...params, _limit: 1 });
|
||||||
if (entries.length > 0) {
|
if (entries.length > 0) {
|
||||||
return deleteOne({ id: entries[0][model.primaryKey] });
|
return deleteOne(entries[0][model.primaryKey]);
|
||||||
}
|
}
|
||||||
return new Promise(resolve => resolve);
|
return new Promise(resolve => resolve);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user