Skip to content

Commit

Permalink
refactor: revert subquery implementation since it won't improve the p…
Browse files Browse the repository at this point in the history
…erformance

Signed-off-by: Pranav C <[email protected]>
  • Loading branch information
pranavxc committed Mar 30, 2023
1 parent 12c5f6f commit a4a6e92
Showing 1 changed file with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ const GROUP_COL = '__nc_group_id';
const nanoidv2 = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 14);
const { v4: uuidv4 } = require('uuid');

const INNER_QUERY_ALIAS = '__nc_inner';

// const WRAPPER_QUERY_ALIAS = '__nc_wrapper';

async function populatePk(model: Model, insertObj: any) {
await model.getColumns();
for (const pkCol of model.primaryKeys) {
Expand Down Expand Up @@ -207,19 +203,16 @@ class BaseModelSqlv2 {
): Promise<any> {
const { where, fields, ...rest } = this._getListArgs(args as any);

const innerQb = this.dbDriver(this.tnPath);
innerQb.select('*');
const qb = this.dbDriver(this.tnPath);

const wrapperQb = this.dbDriver.from(innerQb.as(INNER_QUERY_ALIAS));
await this.selectObject({
qb: wrapperQb,
qb,
fieldsSet: args.fieldsSet,
viewId: this.viewId,
alias: INNER_QUERY_ALIAS,
validateFormula,
});
if (+rest?.shuffle) {
await this.shuffle({ qb: innerQb });
await this.shuffle({ qb });
}

const aliasColObjMap = await this.model.getAliasColObjMap();
Expand All @@ -245,7 +238,7 @@ class BaseModelSqlv2 {
logical_op: 'and',
}),
],
innerQb,
qb,
this.dbDriver
);

Expand All @@ -254,7 +247,7 @@ class BaseModelSqlv2 {
? args.sortArr
: await Sort.list({ viewId: this.viewId });

await sortV2(sorts, innerQb, this.dbDriver);
await sortV2(sorts, qb, this.dbDriver);
} else {
await conditionV2(
[
Expand All @@ -269,30 +262,30 @@ class BaseModelSqlv2 {
logical_op: 'and',
}),
],
innerQb,
qb,
this.dbDriver
);

if (!sorts) sorts = args.sortArr;

await sortV2(sorts, innerQb, this.dbDriver);
await sortV2(sorts, qb, this.dbDriver);
}

// sort by primary key if not autogenerated string
// if autogenerated string sort by created_at column if present
if (this.model.primaryKey && this.model.primaryKey.ai) {
innerQb.orderBy(this.model.primaryKey.column_name);
qb.orderBy(this.model.primaryKey.column_name);
} else if (this.model.columns.find((c) => c.column_name === 'created_at')) {
innerQb.orderBy('created_at');
qb.orderBy('created_at');
}

if (!ignoreViewFilterAndSort) applyPaginate(innerQb, rest);
if (!ignoreViewFilterAndSort) applyPaginate(qb, rest);
const proto = await this.getProto();

let data;

try {
data = await this.execAndParse(wrapperQb);
data = await this.execAndParse(qb);
} catch (e) {
if (!validateFormula) {
return this.list(args, ignoreViewFilterAndSort, true);
Expand Down

0 comments on commit a4a6e92

Please sign in to comment.