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 });
let id;
const res = await this.createQueryBuilder(uid).insert(dataToInsert).execute();
const id = res[0].id || res[0];
const trx = await strapi.db.transaction();
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 trx.commit();
@ -293,18 +289,14 @@ const createEntityManager = (db) => {
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();
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 trx.commit();
@ -372,10 +364,10 @@ const createEntityManager = (db) => {
const { id } = entity;
await this.createQueryBuilder(uid).where({ id }).delete().execute();
const trx = await strapi.db.transaction();
try {
await this.createQueryBuilder(uid).where({ id }).delete().transacting(trx).execute();
await this.deleteRelations(uid, id, { transaction: trx });
await trx.commit();

View File

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