From af876ba43b223fdc5b54d51fec4585f1d4156061 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 25 May 2023 14:26:35 +0530 Subject: [PATCH] fix: read api - extract dependency fields, use it to generate select query and avoid duplicate use of nocoExecute Signed-off-by: Pranav C --- .run/Run NocoDB Sqlite.run.xml | 2 +- packages/nocodb/src/db/BaseModelSqlv2.ts | 27 ++++++++++++------- packages/nocodb/src/services/datas.service.ts | 15 ++++++----- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/.run/Run NocoDB Sqlite.run.xml b/.run/Run NocoDB Sqlite.run.xml index 19996f59ba..e7b120a2af 100644 --- a/.run/Run NocoDB Sqlite.run.xml +++ b/.run/Run NocoDB Sqlite.run.xml @@ -12,4 +12,4 @@ - \ No newline at end of file + diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index f7ddc9db94..f5d04ebffe 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -130,10 +130,24 @@ class BaseModelSqlv2 { autoBind(this); } - public async readByPk(id?: any, validateFormula = false): Promise { + public async readByPk( + id?: any, + validateFormula = false, + query: any = {}, + ): Promise { const qb = this.dbDriver(this.tnPath); - await this.selectObject({ qb, validateFormula }); + const { ast, dependencyFields } = await getAst({ + query, + model: this.model, + view: this.viewId && (await View.get(this.viewId)), + }); + + await this.selectObject({ + ...(dependencyFields ?? {}), + qb, + validateFormula, + }); qb.where(_wherePk(this.model.primaryKeys, id)); @@ -153,14 +167,7 @@ class BaseModelSqlv2 { data.__proto__ = proto; } - // retrieve virtual column data as well - const project = await Project.get(this.model.project_id); - const { model, view } = await getViewAndModelByAliasOrId({ - projectName: project.title, - tableName: this.model.title, - }); - const { ast } = await getAst({ model, view }); - return data ? await nocoExecute(ast, data, {}) : {}; + return data ? await nocoExecute(ast, data, {}, query) : {}; } public async exist(id?: any): Promise { diff --git a/packages/nocodb/src/services/datas.service.ts b/packages/nocodb/src/services/datas.service.ts index 1e1aa78d24..01c663cee8 100644 --- a/packages/nocodb/src/services/datas.service.ts +++ b/packages/nocodb/src/services/datas.service.ts @@ -221,15 +221,13 @@ export class DatasService { dbDriver: await NcConnectionMgrv2.get(base), }); - const row = await baseModel.readByPk(param.rowId); + const row = await baseModel.readByPk(param.rowId, false, param.query); if (!row) { NcError.notFound('Row not found'); } - const { ast } = await getAst({ model, query: param.query, view }); - - return await nocoExecute(ast, row, {}, param.query); + return row; } async dataExist(param: PathParams & { rowId: string; query: any }) { @@ -637,13 +635,16 @@ export class DatasService { dbDriver: await NcConnectionMgrv2.get(base), }); - const { ast } = await getAst({ model, query: param.query }); + const { ast, dependencyFields } = await getAst({ + model, + query: param.query, + }); return await nocoExecute( ast, - await baseModel.readByPk(param.rowId), - {}, + await baseModel.readByPk(param.rowId, false), {}, + dependencyFields, ); } catch (e) { console.log(e);