reduce transaction size

This commit is contained in:
Pierre Noël 2022-12-02 16:11:08 +01:00
parent e295276048
commit 3a2f6490e6
2 changed files with 49 additions and 62 deletions

View File

@ -211,17 +211,13 @@ const createEntityManager = (db) => {
} }
const dataToInsert = processData(metadata, data, { withDefaults: true }); const dataToInsert = processData(metadata, data, { withDefaults: true });
let id;
const res = await this.createQueryBuilder(uid).insert(dataToInsert).execute();
const id = res[0].id || res[0];
const trx = await strapi.db.transaction(); const trx = await strapi.db.transaction();
try { try {
const res = await this.createQueryBuilder(uid)
.insert(dataToInsert)
.transacting(trx)
.execute();
id = res[0].id || res[0];
await this.attachRelations(uid, id, data, { transaction: trx }); await this.attachRelations(uid, id, data, { transaction: trx });
await trx.commit(); await trx.commit();
@ -293,18 +289,14 @@ const createEntityManager = (db) => {
const { id } = entity; const { id } = entity;
const dataToUpdate = processData(metadata, data);
if (!isEmpty(dataToUpdate)) {
await this.createQueryBuilder(uid).where({ id }).update(dataToUpdate).execute();
}
const trx = await strapi.db.transaction(); const trx = await strapi.db.transaction();
try { try {
const dataToUpdate = processData(metadata, data);
if (!isEmpty(dataToUpdate)) {
await this.createQueryBuilder(uid)
.where({ id })
.update(dataToUpdate)
.transacting(trx)
.execute();
}
await this.updateRelations(uid, id, data, { transaction: trx }); await this.updateRelations(uid, id, data, { transaction: trx });
await trx.commit(); await trx.commit();
@ -372,10 +364,10 @@ const createEntityManager = (db) => {
const { id } = entity; const { id } = entity;
await this.createQueryBuilder(uid).where({ id }).delete().execute();
const trx = await strapi.db.transaction(); const trx = await strapi.db.transaction();
try { try {
await this.createQueryBuilder(uid).where({ id }).delete().transacting(trx).execute();
await this.deleteRelations(uid, id, { transaction: trx }); await this.deleteRelations(uid, id, { transaction: trx });
await trx.commit(); await trx.commit();

View File

@ -43,12 +43,9 @@ const createComponents = async (uid, data) => {
throw new Error('Expected an array to create repeatable component'); throw new Error('Expected an array to create repeatable component');
} }
// No Promise.all to avoid deadlocks const components = await Promise.all(
const components = []; componentValue.map((value) => createComponent(componentUID, value))
for (const value of componentValue) { );
const compo = await createComponent(componentUID, value);
components.push(compo);
}
componentBody[attributeName] = components.map(({ id }) => { componentBody[attributeName] = components.map(({ id }) => {
return { return {
@ -80,18 +77,18 @@ const createComponents = async (uid, data) => {
throw new Error('Expected an array to create repeatable component'); throw new Error('Expected an array to create repeatable component');
} }
// No Promise.all to avoid deadlocks componentBody[attributeName] = await Promise.all(
componentBody[attributeName] = []; dynamiczoneValues.map(async (value) => {
for (const value of dynamiczoneValues) { const { id } = await createComponent(value.__component, value);
const { id } = await createComponent(value.__component, value); return {
componentBody[attributeName].push({ id,
id, __component: value.__component,
__component: value.__component, __pivot: {
__pivot: { field: attributeName,
field: attributeName, },
}, };
}); })
} );
continue; continue;
} }
@ -140,12 +137,9 @@ const updateComponents = async (uid, entityToUpdate, data) => {
throw new Error('Expected an array to create repeatable component'); throw new Error('Expected an array to create repeatable component');
} }
// No Promise.all to avoid deadlocks const components = await Promise.all(
const components = []; componentValue.map((value) => updateOrCreateComponent(componentUID, value))
for (const value of componentValue) { );
const compo = await updateOrCreateComponent(componentUID, value);
components.push(compo);
}
componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => { componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => {
return { return {
@ -179,18 +173,19 @@ const updateComponents = async (uid, entityToUpdate, data) => {
throw new Error('Expected an array to create repeatable component'); throw new Error('Expected an array to create repeatable component');
} }
// No Promise.all to avoid deadlocks componentBody[attributeName] = await Promise.all(
componentBody[attributeName] = []; dynamiczoneValues.map(async (value) => {
for (const value of dynamiczoneValues) { const { id } = await updateOrCreateComponent(value.__component, value);
const { id } = await updateOrCreateComponent(value.__component, value);
componentBody[attributeName].push({ return {
id, id,
__component: value.__component, __component: value.__component,
__pivot: { __pivot: {
field: attributeName, field: attributeName,
}, },
}); };
} })
);
continue; continue;
} }
@ -292,14 +287,14 @@ const deleteComponents = async (uid, entityToDelete, { loadComponents = true } =
if (attribute.type === 'component') { if (attribute.type === 'component') {
const { component: componentUID } = attribute; const { component: componentUID } = attribute;
for (const subValue of _.castArray(value)) { await Promise.all(
await deleteComponent(componentUID, subValue); _.castArray(value).map((subValue) => deleteComponent(componentUID, subValue))
} );
} else { } else {
// delete dynamic zone components // delete dynamic zone components
for (const subValue of _.castArray(value)) { await Promise.all(
await deleteComponent(subValue.__component, subValue); _.castArray(value).map((subValue) => deleteComponent(subValue.__component, subValue))
} );
} }
continue; continue;