Browse Source

fix: use runOps for running pre-post ops

pull/8371/head
mertmit 7 months ago
parent
commit
ff66af696f
  1. 72
      packages/nocodb/src/db/BaseModelSqlv2.ts

72
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -3124,7 +3124,7 @@ class BaseModelSqlv2 {
await this.prepareNocoData(insertObj, true, cookie); await this.prepareNocoData(insertObj, true, cookie);
await Promise.all(preInsertOps.map((f) => f(this.dbDriver))); await this.runOps(preInsertOps.map((f) => f()));
let response; let response;
const query = this.dbDriver(this.tnPath).insert(insertObj); const query = this.dbDriver(this.tnPath).insert(insertObj);
@ -3206,7 +3206,7 @@ class BaseModelSqlv2 {
} }
rowId = this.extractCompositePK({ ai, ag, rowId, insertObj }); rowId = this.extractCompositePK({ ai, ag, rowId, insertObj });
await Promise.all(postInsertOps.map((f) => f(rowId))); await this.runOps(postInsertOps.map((f) => f(rowId)));
if (rowId !== null && rowId !== undefined) { if (rowId !== null && rowId !== undefined) {
response = await this.readRecord({ response = await this.readRecord({
@ -3267,8 +3267,8 @@ class BaseModelSqlv2 {
data: Record<string, any>; data: Record<string, any>;
insertObj: Record<string, any>; insertObj: Record<string, any>;
}) { }) {
const postInsertOps: ((rowId: any, trx?: any) => Promise<void>)[] = []; const postInsertOps: ((rowId: any) => Promise<string>)[] = [];
const preInsertOps: ((trx?: any) => Promise<void>)[] = []; const preInsertOps: (() => Promise<string>)[] = [];
for (const col of nestedCols) { for (const col of nestedCols) {
if (col.title in data) { if (col.title in data) {
const colOptions = await col.getColOptions<LinkToAnotherRecordColumn>(); const colOptions = await col.getColOptions<LinkToAnotherRecordColumn>();
@ -3302,15 +3302,16 @@ class BaseModelSqlv2 {
if (isBt) { if (isBt) {
// todo: unlink the ref record // todo: unlink the ref record
preInsertOps.push(async (trx: any = this.dbDriver) => { preInsertOps.push(async () => {
await trx(this.getTnPath(childModel.table_name)) return this.dbDriver(this.getTnPath(childModel.table_name))
.update({ .update({
[childCol.column_name]: null, [childCol.column_name]: null,
}) })
.where( .where(
childCol.column_name, childCol.column_name,
nestedData[childModel.primaryKey.title], nestedData[childModel.primaryKey.title],
); )
.toQuery();
}); });
if (typeof nestedData !== 'object') continue; if (typeof nestedData !== 'object') continue;
@ -3318,15 +3319,16 @@ class BaseModelSqlv2 {
const parentCol = await colOptions.getParentColumn(); const parentCol = await colOptions.getParentColumn();
insertObj[childCol.column_name] = nestedData?.[parentCol.title]; insertObj[childCol.column_name] = nestedData?.[parentCol.title];
} else { } else {
postInsertOps.push(async (rowId, trx: any = this.dbDriver) => { postInsertOps.push(async (rowId) => {
await trx(this.getTnPath(childModel.table_name)) return this.dbDriver(this.getTnPath(childModel.table_name))
.update({ .update({
[childCol.column_name]: rowId, [childCol.column_name]: rowId,
}) })
.where( .where(
childModel.primaryKey.column_name, childModel.primaryKey.column_name,
nestedData[childModel.primaryKey.title], nestedData[childModel.primaryKey.title],
); )
.toQuery();
}); });
} }
} }
@ -3338,32 +3340,22 @@ class BaseModelSqlv2 {
const childModel = await childCol.getModel(); const childModel = await childCol.getModel();
await childModel.getColumns(); await childModel.getColumns();
postInsertOps.push( postInsertOps.push(async (rowId) => {
async ( return this.dbDriver(this.getTnPath(childModel.table_name))
rowId,
// todo: use transaction type
trx: any = this.dbDriver,
) => {
await trx(this.getTnPath(childModel.table_name))
.update({ .update({
[childCol.column_name]: rowId, [childCol.column_name]: rowId,
}) })
.whereIn( .whereIn(
childModel.primaryKey.column_name, childModel.primaryKey.column_name,
nestedData?.map((r) => r[childModel.primaryKey.title]), nestedData?.map((r) => r[childModel.primaryKey.title]),
); )
}, .toQuery();
); });
} }
break; break;
case RelationTypes.MANY_TO_MANY: { case RelationTypes.MANY_TO_MANY: {
if (!Array.isArray(nestedData)) continue; if (!Array.isArray(nestedData)) continue;
postInsertOps.push( postInsertOps.push(async (rowId) => {
async (
rowId,
// todo: use transaction type
trx: any = this.dbDriver,
) => {
const parentModel = await colOptions const parentModel = await colOptions
.getParentColumn() .getParentColumn()
.then((c) => c.getModel()); .then((c) => c.getModel());
@ -3376,9 +3368,10 @@ class BaseModelSqlv2 {
[parentMMCol.column_name]: r[parentModel.primaryKey.title], [parentMMCol.column_name]: r[parentModel.primaryKey.title],
[childMMCol.column_name]: rowId, [childMMCol.column_name]: rowId,
})); }));
await trx(this.getTnPath(mmModel.table_name)).insert(rows); return this.dbDriver(this.getTnPath(mmModel.table_name))
}, .insert(rows)
); .toQuery();
});
} }
} }
} }
@ -3410,8 +3403,8 @@ class BaseModelSqlv2 {
try { try {
// TODO: ag column handling for raw bulk insert // TODO: ag column handling for raw bulk insert
const insertDatas = raw ? datas : []; const insertDatas = raw ? datas : [];
let postInsertOps: ((rowId: any, trx?: any) => Promise<void>)[] = []; let postInsertOps: ((rowId: any) => Promise<string>)[] = [];
let preInsertOps: ((trx?: any) => Promise<void>)[] = []; let preInsertOps: (() => Promise<string>)[] = [];
let aiPkCol: Column; let aiPkCol: Column;
let agPkCol: Column; let agPkCol: Column;
@ -3596,7 +3589,10 @@ class BaseModelSqlv2 {
} }
} }
await Promise.all(preInsertOps.map((f) => f(trx))); await this.runOps(
preInsertOps.map((f) => f()),
trx,
);
let responses; let responses;
@ -3661,7 +3657,10 @@ class BaseModelSqlv2 {
}); });
} }
await Promise.all(postInsertOps.map((f) => f(rowId, trx))); await this.runOps(
postInsertOps.map((f) => f(rowId)),
trx,
);
} }
await trx.commit(); await trx.commit();
@ -5248,6 +5247,13 @@ class BaseModelSqlv2 {
return data; return data;
} }
async runOps(ops: Promise<string>[], trx = this.dbDriver) {
const queries = await Promise.all(ops);
for (const query of queries) {
await trx.raw(query);
}
}
protected async substituteColumnIdsWithColumnTitles( protected async substituteColumnIdsWithColumnTitles(
data: Record<string, any>[], data: Record<string, any>[],
dependencyColumns?: Column[], dependencyColumns?: Column[],

Loading…
Cancel
Save