Browse Source

fix: read api - extract dependency fields, use it to generate select query and avoid duplicate use of nocoExecute

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5735/head
Pranav C 2 years ago
parent
commit
af876ba43b
  1. 27
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 15
      packages/nocodb/src/services/datas.service.ts

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

@ -130,10 +130,24 @@ class BaseModelSqlv2 {
autoBind(this); autoBind(this);
} }
public async readByPk(id?: any, validateFormula = false): Promise<any> { public async readByPk(
id?: any,
validateFormula = false,
query: any = {},
): Promise<any> {
const qb = this.dbDriver(this.tnPath); 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)); qb.where(_wherePk(this.model.primaryKeys, id));
@ -153,14 +167,7 @@ class BaseModelSqlv2 {
data.__proto__ = proto; data.__proto__ = proto;
} }
// retrieve virtual column data as well return data ? await nocoExecute(ast, data, {}, query) : {};
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, {}) : {};
} }
public async exist(id?: any): Promise<any> { public async exist(id?: any): Promise<any> {

15
packages/nocodb/src/services/datas.service.ts

@ -221,15 +221,13 @@ export class DatasService {
dbDriver: await NcConnectionMgrv2.get(base), dbDriver: await NcConnectionMgrv2.get(base),
}); });
const row = await baseModel.readByPk(param.rowId); const row = await baseModel.readByPk(param.rowId, false, param.query);
if (!row) { if (!row) {
NcError.notFound('Row not found'); NcError.notFound('Row not found');
} }
const { ast } = await getAst({ model, query: param.query, view }); return row;
return await nocoExecute(ast, row, {}, param.query);
} }
async dataExist(param: PathParams & { rowId: string; query: any }) { async dataExist(param: PathParams & { rowId: string; query: any }) {
@ -637,13 +635,16 @@ export class DatasService {
dbDriver: await NcConnectionMgrv2.get(base), 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( return await nocoExecute(
ast, ast,
await baseModel.readByPk(param.rowId), await baseModel.readByPk(param.rowId, false),
{},
{}, {},
dependencyFields,
); );
} catch (e) { } catch (e) {
console.log(e); console.log(e);

Loading…
Cancel
Save