Browse Source

fix: corrections in getAst method usage

Signed-off-by: Pranav C <pranavxc@gmail.com>
test/query-opt-imp
Pranav C 2 years ago
parent
commit
9f1718304c
  1. 16
      packages/nocodb/src/lib/controllers/dbData/oldData.ctl.ts
  2. 4
      packages/nocodb/src/lib/controllers/publicControllers/publicDataExport.ctl.ts
  3. 11
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts
  4. 8
      packages/nocodb/src/lib/meta/api/dataApis/dataAliasApis.ts
  5. 13
      packages/nocodb/src/lib/services/dbData/helpers.ts
  6. 23
      packages/nocodb/src/lib/services/dbData/index.ts
  7. 23
      packages/nocodb/src/lib/services/public/publicData.svc.ts
  8. 4
      packages/nocodb/src/lib/services/public/publicDataExport.svc.ts

16
packages/nocodb/src/lib/controllers/dbData/oldData.ctl.ts

@ -21,7 +21,7 @@ export async function dataList(req: Request, res: Response) {
dbDriver: await NcConnectionMgrv2.get(base),
});
const requestObj = await getAst({
const { ast } = await getAst({
query: req.query,
model,
view,
@ -36,7 +36,7 @@ export async function dataList(req: Request, res: Response) {
} catch (e) {}
const data = await nocoExecute(
requestObj,
ast,
await baseModel.list(listArgs),
{},
listArgs
@ -132,13 +132,15 @@ async function dataRead(req: Request, res: Response) {
dbDriver: await NcConnectionMgrv2.get(base),
});
const {ast} = await getAst({
query: req.query,
model,
view,
})
res.json(
await nocoExecute(
await getAst({
query: req.query,
model,
view,
}),
ast ,
await baseModel.readByPk(req.params.rowId),
{},
{}

4
packages/nocodb/src/lib/controllers/publicControllers/publicDataExport.ctl.ts

@ -136,7 +136,7 @@ async function getDbRows(model, view: View, req: Request) {
dbDriver: await NcConnectionMgrv2.get(base),
});
const requestObj = await getAst({
const { ast } = await getAst({
query: req.query,
model,
view,
@ -159,7 +159,7 @@ async function getDbRows(model, view: View, req: Request) {
elapsed = temp[0] * 1000 + temp[1] / 1000000
) {
const rows = await nocoExecute(
requestObj,
ast,
await baseModel.list({ ...listArgs, offset, limit }),
{},
listArgs

11
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

@ -195,13 +195,14 @@ class BaseModelSqlv2 {
filterArr?: Filter[];
sortArr?: Sort[];
sort?: string | string[];
fieldsSet?:Set<string>
} = {},
ignoreViewFilterAndSort = false
): Promise<any> {
const { where, fields, ...rest } = this._getListArgs(args as any);
const qb = this.dbDriver(this.tnPath);
await this.selectObject({ qb, fields, viewId: this.viewId });
await this.selectObject({ qb, fieldsSet: args.fieldsSet, viewId: this.viewId });
if (+rest?.shuffle) {
await this.shuffle({ qb });
}
@ -272,6 +273,9 @@ class BaseModelSqlv2 {
if (!ignoreViewFilterAndSort) applyPaginate(qb, rest);
const proto = await this.getProto();
console.log('list query', qb.toQuery())
const data = await this.execAndParse(qb);
// console.log(qb.toQuery());
@ -1456,7 +1460,9 @@ class BaseModelSqlv2 {
fields: _fields,
extractPkAndPv,
viewId,
fieldsSet
}: {
fieldsSet?: Set<string>;
qb: Knex.QueryBuilder;
columns?: Column[];
fields?: string[] | string;
@ -1480,6 +1486,7 @@ class BaseModelSqlv2 {
// hide if column marked as hidden in view
// of if column is system field and system field is hidden
if (
(!fieldsSet || !fieldsSet.has(column.title)) &&
!extractPkAndPv &&
!(viewOrTableColumn instanceof Column) &&
(!(viewOrTableColumn as GridViewColumn)?.show ||
@ -2716,7 +2723,7 @@ class BaseModelSqlv2 {
qb.limit(+rest?.limit || 25);
qb.offset(+rest?.offset || 0);
await this.selectObject({ qb });
await this.selectObject({ qb, extractPkAndPv:true });
// todo: refactor and move to a method (applyFilterAndSort)
const aliasColObjMap = await this.model.getAliasColObjMap();

8
packages/nocodb/src/lib/meta/api/dataApis/dataAliasApis.ts

@ -157,9 +157,11 @@ async function getFindOne(model, view: View, req) {
} catch (e) {}
const data = await baseModel.findOne(args);
const { ast } = await getAst({ model, query: args, view })
return data
? await nocoExecute(
await getAst({ model, query: args, view }),
ast,
data,
{},
{}
@ -197,9 +199,11 @@ async function dataRead(req: Request, res: Response) {
dbDriver: NcConnectionMgrv2.get(base),
});
const { ast } = await getAst({ model, query: req.query, view })
res.json(
await nocoExecute(
await getAst({ model, query: req.query, view }),
ast,
await baseModel.readByPk(req.params.rowId),
{},
{}

13
packages/nocodb/src/lib/services/dbData/helpers.ts

@ -242,13 +242,14 @@ export async function getDbRows(param: {
temp = process.hrtime(startTime),
elapsed = temp[0] * 1000 + temp[1] / 1000000
) {
const {ast} = await getAst({
query: query,
includePkByDefault: false,
model: view.model,
view,
});
const rows = await nocoExecute(
await getAst({
query: query,
includePkByDefault: false,
model: view.model,
view,
}),
ast,
await baseModel.list({ ...listArgs, offset, limit }),
{},
query

23
packages/nocodb/src/lib/services/dbData/index.ts

@ -114,9 +114,10 @@ export async function getDataList(param: {
dbDriver: await NcConnectionMgrv2.get(base),
});
const requestObj = await getAst({ model, query, view });
const { ast, dependencyFields } = await getAst({ model, query, view });
const listArgs: any = { ...query };
const listArgs: any = { ...query, fieldsSet: dependencyFields?.fields };
try {
listArgs.filterArr = JSON.parse(listArgs.filterArrJson);
} catch (e) {}
@ -128,7 +129,7 @@ export async function getDataList(param: {
let count = 0;
try {
data = await nocoExecute(
requestObj,
ast,
await baseModel.list(listArgs),
{},
listArgs
@ -170,10 +171,12 @@ export async function getFindOne(param: {
args.sortArr = JSON.parse(args.sortArrJson);
} catch (e) {}
const {ast} = await getAst({ model, query: args, view })
const data = await baseModel.findOne(args);
return data
? await nocoExecute(
await getAst({ model, query: args, view }),
ast,
data,
{},
{}
@ -225,8 +228,10 @@ export async function dataRead(
NcError.notFound('Row not found');
}
const { ast } = await getAst({ model, query: param.query, view })
return await nocoExecute(
await getAst({ model, query: param.query, view }),
ast,
row,
{},
param.query
@ -279,7 +284,7 @@ export async function getGroupedDataList(param: {
dbDriver: await NcConnectionMgrv2.get(base),
});
const requestObj = await getAst({ model, query, view });
const { ast } = await getAst({ model, query, view });
const listArgs: any = { ...query };
try {
@ -299,7 +304,7 @@ export async function getGroupedDataList(param: {
groupColumnId: param.columnId,
});
data = await nocoExecute(
{ key: 1, value: requestObj },
{ key: 1, value: ast },
groupedData,
{},
listArgs
@ -650,8 +655,10 @@ export async function dataReadByViewId(param: {
dbDriver: await NcConnectionMgrv2.get(base),
});
const { ast } = await getAst({ model, query: param.query })
return await nocoExecute(
await getAst({ model, query: param.query }),
ast,
await baseModel.readByPk(param.rowId),
{},
{}

23
packages/nocodb/src/lib/services/public/publicData.svc.ts

@ -59,20 +59,25 @@ export async function dataList(param: {
let count = 0;
try {
const { ast } = await getAst({
query: param.query,
model,
view,
})
data = await nocoExecute(
await getAst({
query: param.query,
model,
view,
}),
ast,
await baseModel.list(listArgs),
{},
listArgs
);
count = await baseModel.count(listArgs);
} catch (e) {
console.log(e)
// show empty result instead of throwing error here
// e.g. search some text in a numeric field
NcError.internalServerError('Please try after some time')
}
return new PagedResponseImpl(data, { ...param.query, count });
@ -128,7 +133,7 @@ async function getGroupedDataList(param: {
dbDriver: await NcConnectionMgrv2.get(base),
});
const requestObj = await getAst({ model, query: param.query, view });
const { ast } = await getAst({ model, query: param.query, view });
const listArgs: any = { ...query };
try {
@ -149,7 +154,7 @@ async function getGroupedDataList(param: {
groupColumnId,
});
data = await nocoExecute(
{ key: 1, value: requestObj },
{ key: 1, value: ast },
groupedData,
{},
listArgs
@ -304,7 +309,7 @@ export async function relDataList(param: {
dbDriver: await NcConnectionMgrv2.get(base),
});
const requestObj = await getAst({
const { ast } = await getAst({
query: param.query,
model,
extractOnlyPrimaries: true,
@ -314,7 +319,7 @@ export async function relDataList(param: {
let count = 0;
try {
data = data = await nocoExecute(
requestObj,
ast,
await baseModel.list(param.query),
{},
param.query

4
packages/nocodb/src/lib/services/public/publicDataExport.svc.ts

@ -46,7 +46,7 @@ export async function getDbRows(param: {
dbDriver: await NcConnectionMgrv2.get(base),
});
const requestObj = await getAst({
const { ast } = await getAst({
query: param.query,
model: param.model,
view: param.view,
@ -69,7 +69,7 @@ export async function getDbRows(param: {
elapsed = temp[0] * 1000 + temp[1] / 1000000
) {
const rows = await nocoExecute(
requestObj,
ast,
await baseModel.list({ ...listArgs, offset, limit }),
{},
listArgs

Loading…
Cancel
Save