From 11fe984a67c3cca5b2f1ee2e55234521e2caa846 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sun, 24 Mar 2024 11:52:49 +0530 Subject: [PATCH] Nc fix/insert issue (#7947) * fix: use optimized query reader for post insert read * fix: use optimised query for read before delete * fix: add async --- packages/nocodb/src/db/BaseModelSqlv2.ts | 62 +++++++++++++++++------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index ef874ea280..41fd24021f 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -2813,13 +2813,15 @@ class BaseModelSqlv2 { async delByPk(id, _trx?, cookie?) { let trx: Knex.Transaction = _trx; try { + const source = await Source.get(this.model.source_id); // retrieve data for handling params in hook - const data = await this.readByPk( - id, - false, - {}, - { ignoreView: true, getHiddenColumn: true }, - ); + const data = await this.readRecord({ + idOrRecord: id, + validateFormula: false, + ignoreView: true, + getHiddenColumn: true, + source, + }); await this.beforeDelete(id, trx, cookie); const execQueries: ((trx: Knex.Transaction) => Promise)[] = []; @@ -3056,9 +3058,31 @@ class BaseModelSqlv2 { return this.dbDriver.clientType(); } + public async readRecord(params: { + idOrRecord: string | Record; + fieldsSet?: Set; + ignoreView?: boolean; + getHiddenColumn?: boolean; + validateFormula?: boolean; + source: Source; + disableOptimization?: boolean; + view?: View; + }): Promise { + return this.readByPk( + params.idOrRecord, + false, + {}, + { + ignoreView: params.ignoreView, + getHiddenColumn: params.getHiddenColumn, + }, + ); + } + async nestedInsert(data, _trx = null, cookie?) { // const driver = trx ? trx : await this.dbDriver.transaction(); try { + const source = await Source.get(this.model.source_id); await populatePk(this.model, data); const insertObj = await this.model.mapAliasToColumn( data, @@ -3102,12 +3126,13 @@ class BaseModelSqlv2 { // handle if autogenerated primary key is used if (ag) { if (!response) await this.execAndParse(query); - response = await this.readByPk( - insertObj[ag.column_name], - false, - {}, - { ignoreView: true, getHiddenColumn: true }, - ); + response = await this.readRecord({ + idOrRecord: insertObj[ag.column_name], + ignoreView: true, + getHiddenColumn: true, + validateFormula: false, + source, + }); } else if ( !response || (typeof response?.[0] !== 'object' && response?.[0] !== null) @@ -3166,12 +3191,13 @@ class BaseModelSqlv2 { await Promise.all(postInsertOps.map((f) => f(rowId))); if (rowId !== null && rowId !== undefined) { - response = await this.readByPk( - rowId, - false, - {}, - { ignoreView: true, getHiddenColumn: true }, - ); + response = await this.readRecord({ + idOrRecord: rowId, + validateFormula: false, + ignoreView: true, + getHiddenColumn: true, + source, + }); } await this.afterInsert(response, this.dbDriver, cookie);