Browse Source

refactor: cleanup

Signed-off-by: Pranav C <pranavxc@gmail.com>
test/query-opt-imp
Pranav C 2 years ago
parent
commit
6bef279c38
  1. 47
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

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

@ -206,14 +206,14 @@ class BaseModelSqlv2 {
const { where, fields, ...rest } = this._getListArgs(args as any); const { where, fields, ...rest } = this._getListArgs(args as any);
const innerQb = this.dbDriver(this.tnPath); const innerQb = this.dbDriver(this.tnPath);
innerQb.select('*') innerQb.select('*');
const wrapperQb = this.dbDriver.from(innerQb.as(INNER_QUERY_ALIAS)) const wrapperQb = this.dbDriver.from(innerQb.as(INNER_QUERY_ALIAS));
await this.selectObject({ await this.selectObject({
qb: wrapperQb, qb: wrapperQb,
fieldsSet: args.fieldsSet, fieldsSet: args.fieldsSet,
viewId: this.viewId, viewId: this.viewId,
alias: INNER_QUERY_ALIAS alias: INNER_QUERY_ALIAS,
}); });
if (+rest?.shuffle) { if (+rest?.shuffle) {
await this.shuffle({ qb: innerQb }); await this.shuffle({ qb: innerQb });
@ -288,10 +288,6 @@ class BaseModelSqlv2 {
const data = await this.execAndParse(wrapperQb); const data = await this.execAndParse(wrapperQb);
// todo: remove
console.log(wrapperQb.toQuery());
return data?.map((d) => { return data?.map((d) => {
d.__proto__ = proto; d.__proto__ = proto;
return d; return d;
@ -413,7 +409,10 @@ class BaseModelSqlv2 {
return await qb; return await qb;
} }
async multipleHmList({ colId, ids }, args: { limit?; offset?; fieldsSet?:Set<string> } = {}) { async multipleHmList(
{ colId, ids },
args: { limit?; offset?; fieldsSet?: Set<string> } = {}
) {
try { try {
const { where, sort, ...rest } = this._getListArgs(args as any); const { where, sort, ...rest } = this._getListArgs(args as any);
// todo: get only required fields // todo: get only required fields
@ -441,7 +440,11 @@ class BaseModelSqlv2 {
const parentTn = this.getTnPath(parentTable); const parentTn = this.getTnPath(parentTable);
const qb = this.dbDriver(childTn); const qb = this.dbDriver(childTn);
await childModel.selectObject({ qb, extractPkAndPv: true, fieldsSet: args.fieldsSet }); await childModel.selectObject({
qb,
extractPkAndPv: true,
fieldsSet: args.fieldsSet,
});
await this.applySortAndFilter({ table: childTable, where, qb, sort }); await this.applySortAndFilter({ table: childTable, where, qb, sort });
const childQb = this.dbDriver.queryBuilder().from( const childQb = this.dbDriver.queryBuilder().from(
@ -1254,7 +1257,10 @@ class BaseModelSqlv2 {
}); });
} }
private async getSelectQueryBuilderForFormula(column: Column<any>, tableAlias?:string ) { private async getSelectQueryBuilderForFormula(
column: Column<any>,
tableAlias?: string
) {
const formula = await column.getColOptions<FormulaColumn>(); const formula = await column.getColOptions<FormulaColumn>();
if (formula.error) throw new Error(`Formula error: ${formula.error}`); if (formula.error) throw new Error(`Formula error: ${formula.error}`);
const qb = await formulaQueryBuilderv2( const qb = await formulaQueryBuilderv2(
@ -1263,7 +1269,7 @@ class BaseModelSqlv2 {
this.dbDriver, this.dbDriver,
this.model, this.model,
column, column,
{ }, {},
tableAlias tableAlias
); );
return qb; return qb;
@ -1408,7 +1414,7 @@ class BaseModelSqlv2 {
{ {
// limit: ids.length, // limit: ids.length,
where: `(${pCol.column_name},in,${ids.join(',')})`, where: `(${pCol.column_name},in,${ids.join(',')})`,
fieldsSet: (readLoader as any).args?.fieldsSet fieldsSet: (readLoader as any).args?.fieldsSet,
}, },
true true
); );
@ -1421,7 +1427,7 @@ class BaseModelSqlv2 {
}); });
// defining HasMany count method within GQL Type class // defining HasMany count method within GQL Type class
proto[column.title] = async function (args?:any) { proto[column.title] = async function (args?: any) {
if ( if (
this?.[cCol?.title] === null || this?.[cCol?.title] === null ||
this?.[cCol?.title] === undefined this?.[cCol?.title] === undefined
@ -1482,7 +1488,7 @@ class BaseModelSqlv2 {
extractPkAndPv, extractPkAndPv,
viewId, viewId,
fieldsSet, fieldsSet,
alias alias,
}: { }: {
fieldsSet?: Set<string>; fieldsSet?: Set<string>;
qb: Knex.QueryBuilder; qb: Knex.QueryBuilder;
@ -1510,14 +1516,15 @@ class BaseModelSqlv2 {
// hide if column marked as hidden in view // hide if column marked as hidden in view
// of if column is system field and system field is hidden // of if column is system field and system field is hidden
if ( if (
fieldsSet ? !fieldsSet.has(column.title) : ( fieldsSet
!extractPkAndPv && ? !fieldsSet.has(column.title)
: !extractPkAndPv &&
!(viewOrTableColumn instanceof Column) && !(viewOrTableColumn instanceof Column) &&
(!(viewOrTableColumn as GridViewColumn)?.show || (!(viewOrTableColumn as GridViewColumn)?.show ||
(!view?.show_system_fields && (!view?.show_system_fields &&
column.uidt !== UITypes.ForeignKey && column.uidt !== UITypes.ForeignKey &&
!column.pk && !column.pk &&
isSystemColumn(column))) ) isSystemColumn(column)))
) )
continue; continue;
@ -1575,7 +1582,7 @@ class BaseModelSqlv2 {
case UITypes.Formula: case UITypes.Formula:
try { try {
const selectQb = await this.getSelectQueryBuilderForFormula( const selectQb = await this.getSelectQueryBuilderForFormula(
barcodeValueColumn, barcodeValueColumn
); );
qb.select({ qb.select({
[column.column_name]: selectQb.builder, [column.column_name]: selectQb.builder,
@ -1607,8 +1614,8 @@ class BaseModelSqlv2 {
sanitize(column.title), sanitize(column.title),
]) ])
); );
} catch(e) { } catch (e) {
console.log(e) console.log(e);
// return dummy select // return dummy select
qb.select( qb.select(
this.dbDriver.raw(`'ERR' as ??`, [sanitize(column.title)]) this.dbDriver.raw(`'ERR' as ??`, [sanitize(column.title)])

Loading…
Cancel
Save