Browse Source

fix: lint

pull/2515/head
Wing-Kam Wong 2 years ago
parent
commit
79496fc622
  1. 345
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

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

@ -24,7 +24,7 @@ import {
RelationTypes, RelationTypes,
SortType, SortType,
UITypes, UITypes,
ViewTypes ViewTypes,
} from 'nocodb-sdk'; } from 'nocodb-sdk';
import formSubmissionEmailTemplate from '../../../../utils/common/formSubmissionEmailTemplate'; import formSubmissionEmailTemplate from '../../../../utils/common/formSubmissionEmailTemplate';
import ejs from 'ejs'; import ejs from 'ejs';
@ -34,7 +34,7 @@ import Hook from '../../../../models/Hook';
import NcPluginMgrv2 from '../../../../meta/helpers/NcPluginMgrv2'; import NcPluginMgrv2 from '../../../../meta/helpers/NcPluginMgrv2';
import { import {
_transformSubmittedFormDataForEmail, _transformSubmittedFormDataForEmail,
invokeWebhook invokeWebhook,
} from '../../../../meta/helpers/webhookHelpers'; } from '../../../../meta/helpers/webhookHelpers';
import Validator from 'validator'; import Validator from 'validator';
import { customValidators } from './customValidators'; import { customValidators } from './customValidators';
@ -73,13 +73,13 @@ class BaseModelSqlv2 {
private config: any = { private config: any = {
limitDefault: Math.max(+process.env.DB_QUERY_LIMIT_DEFAULT || 25, 1), limitDefault: Math.max(+process.env.DB_QUERY_LIMIT_DEFAULT || 25, 1),
limitMin: Math.max(+process.env.DB_QUERY_LIMIT_MIN || 1, 1), limitMin: Math.max(+process.env.DB_QUERY_LIMIT_MIN || 1, 1),
limitMax: Math.max(+process.env.DB_QUERY_LIMIT_MAX || 1000, 1) limitMax: Math.max(+process.env.DB_QUERY_LIMIT_MAX || 1000, 1),
}; };
constructor({ constructor({
dbDriver, dbDriver,
model, model,
viewId viewId,
}: { }: {
[key: string]: any; [key: string]: any;
model: Model; model: Model;
@ -135,14 +135,14 @@ class BaseModelSqlv2 {
new Filter({ new Filter({
children: args.filterArr || [], children: args.filterArr || [],
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
new Filter({ new Filter({
children: filterObj, children: filterObj,
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
...(args.filterArr || []) ...(args.filterArr || []),
], ],
qb, qb,
this.dbDriver this.dbDriver
@ -189,19 +189,19 @@ class BaseModelSqlv2 {
new Filter({ new Filter({
children: children:
(await Filter.rootFilterList({ viewId: this.viewId })) || [], (await Filter.rootFilterList({ viewId: this.viewId })) || [],
is_group: true is_group: true,
}), }),
new Filter({ new Filter({
children: args.filterArr || [], children: args.filterArr || [],
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
new Filter({ new Filter({
children: filterObj, children: filterObj,
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
...(args.filterArr || []) ...(args.filterArr || []),
], ],
qb, qb,
this.dbDriver this.dbDriver
@ -219,14 +219,14 @@ class BaseModelSqlv2 {
new Filter({ new Filter({
children: args.filterArr || [], children: args.filterArr || [],
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
new Filter({ new Filter({
children: filterObj, children: filterObj,
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
...(args.filterArr || []) ...(args.filterArr || []),
], ],
qb, qb,
this.dbDriver this.dbDriver
@ -241,7 +241,7 @@ class BaseModelSqlv2 {
// if autogenerated string sort by created_at column if present // if autogenerated string sort by created_at column if present
if (this.model.primaryKey && this.model.primaryKey.ai) { if (this.model.primaryKey && this.model.primaryKey.ai) {
qb.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')) { } else if (this.model.columns.find((c) => c.column_name === 'created_at')) {
qb.orderBy('created_at'); qb.orderBy('created_at');
} }
@ -249,7 +249,7 @@ class BaseModelSqlv2 {
const proto = await this.getProto(); const proto = await this.getProto();
const data = await this.extractRawQueryAndExec(qb); const data = await this.extractRawQueryAndExec(qb);
return data?.map(d => { return data?.map((d) => {
d.__proto__ = proto; d.__proto__ = proto;
return d; return d;
}); });
@ -274,19 +274,19 @@ class BaseModelSqlv2 {
new Filter({ new Filter({
children: children:
(await Filter.rootFilterList({ viewId: this.viewId })) || [], (await Filter.rootFilterList({ viewId: this.viewId })) || [],
is_group: true is_group: true,
}), }),
new Filter({ new Filter({
children: args.filterArr || [], children: args.filterArr || [],
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
new Filter({ new Filter({
children: filterObj, children: filterObj,
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
...(args.filterArr || []) ...(args.filterArr || []),
], ],
qb, qb,
this.dbDriver this.dbDriver
@ -297,14 +297,14 @@ class BaseModelSqlv2 {
new Filter({ new Filter({
children: args.filterArr || [], children: args.filterArr || [],
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
new Filter({ new Filter({
children: filterObj, children: filterObj,
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
...(args.filterArr || []) ...(args.filterArr || []),
], ],
qb, qb,
this.dbDriver this.dbDriver
@ -312,7 +312,7 @@ class BaseModelSqlv2 {
} }
qb.count(sanitize(this.model.primaryKey?.column_name) || '*', { qb.count(sanitize(this.model.primaryKey?.column_name) || '*', {
as: 'count' as: 'count',
}).first(); }).first();
const res = (await this.dbDriver.raw(unsanitize(qb.toQuery()))) as any; const res = (await this.dbDriver.raw(unsanitize(qb.toQuery()))) as any;
return (this.isPg ? res.rows[0] : res[0][0] ?? res[0]).count; return (this.isPg ? res.rows[0] : res[0][0] ?? res[0]).count;
@ -326,7 +326,7 @@ class BaseModelSqlv2 {
offset?; offset?;
sort?: string | string[]; sort?: string | string[];
} = { } = {
column_name: '' column_name: '',
} }
) { ) {
const { where, ...rest } = this._getListArgs(args as any); const { where, ...rest } = this._getListArgs(args as any);
@ -345,8 +345,8 @@ class BaseModelSqlv2 {
new Filter({ new Filter({
children: filterObj, children: filterObj,
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}) }),
], ],
qb, qb,
this.dbDriver this.dbDriver
@ -364,16 +364,20 @@ class BaseModelSqlv2 {
// const { cn } = this.hasManyRelations.find(({ tn }) => tn === child) || {}; // const { cn } = this.hasManyRelations.find(({ tn }) => tn === child) || {};
const relColumn = (await this.model.getColumns()).find( const relColumn = (await this.model.getColumns()).find(
c => c.id === colId (c) => c.id === colId
); );
const chilCol = await ((await relColumn.getColOptions()) as LinkToAnotherRecordColumn).getChildColumn(); const chilCol = await (
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn
).getChildColumn();
const childTable = await chilCol.getModel(); const childTable = await chilCol.getModel();
const parentCol = await ((await relColumn.getColOptions()) as LinkToAnotherRecordColumn).getParentColumn(); const parentCol = await (
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn
).getParentColumn();
const parentTable = await parentCol.getModel(); const parentTable = await parentCol.getModel();
const childModel = await Model.getBaseModelSQL({ const childModel = await Model.getBaseModelSQL({
model: childTable, model: childTable,
dbDriver: this.dbDriver dbDriver: this.dbDriver,
}); });
await parentTable.getColumns(); await parentTable.getColumns();
@ -383,7 +387,7 @@ class BaseModelSqlv2 {
const childQb = this.dbDriver.queryBuilder().from( const childQb = this.dbDriver.queryBuilder().from(
this.dbDriver this.dbDriver
.unionAll( .unionAll(
ids.map(p => { ids.map((p) => {
const query = qb const query = qb
.clone() .clone()
.select(this.dbDriver.raw('? as ??', [p, GROUP_COL])) .select(this.dbDriver.raw('? as ??', [p, GROUP_COL]))
@ -409,12 +413,12 @@ class BaseModelSqlv2 {
const proto = await ( const proto = await (
await Model.getBaseModelSQL({ await Model.getBaseModelSQL({
id: childTable.id, id: childTable.id,
dbDriver: this.dbDriver dbDriver: this.dbDriver,
}) })
).getProto(); ).getProto();
return _.groupBy( return _.groupBy(
children.map(c => { children.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}), }),
@ -430,16 +434,20 @@ class BaseModelSqlv2 {
try { try {
// const { cn } = this.hasManyRelations.find(({ tn }) => tn === child) || {}; // const { cn } = this.hasManyRelations.find(({ tn }) => tn === child) || {};
const relColumn = (await this.model.getColumns()).find( const relColumn = (await this.model.getColumns()).find(
c => c.id === colId (c) => c.id === colId
); );
const chilCol = await ((await relColumn.getColOptions()) as LinkToAnotherRecordColumn).getChildColumn(); const chilCol = await (
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn
).getChildColumn();
const childTable = await chilCol.getModel(); const childTable = await chilCol.getModel();
const parentCol = await ((await relColumn.getColOptions()) as LinkToAnotherRecordColumn).getParentColumn(); const parentCol = await (
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn
).getParentColumn();
const parentTable = await parentCol.getModel(); const parentTable = await parentCol.getModel();
await parentTable.getColumns(); await parentTable.getColumns();
const children = await this.dbDriver.unionAll( const children = await this.dbDriver.unionAll(
ids.map(p => { ids.map((p) => {
const query = this.dbDriver(childTable.table_name) const query = this.dbDriver(childTable.table_name)
.count(`${chilCol?.column_name} as count`) .count(`${chilCol?.column_name} as count`)
.whereIn( .whereIn(
@ -468,16 +476,20 @@ class BaseModelSqlv2 {
// todo: get only required fields // todo: get only required fields
const relColumn = (await this.model.getColumns()).find( const relColumn = (await this.model.getColumns()).find(
c => c.id === colId (c) => c.id === colId
); );
const chilCol = await ((await relColumn.getColOptions()) as LinkToAnotherRecordColumn).getChildColumn(); const chilCol = await (
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn
).getChildColumn();
const childTable = await chilCol.getModel(); const childTable = await chilCol.getModel();
const parentCol = await ((await relColumn.getColOptions()) as LinkToAnotherRecordColumn).getParentColumn(); const parentCol = await (
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn
).getParentColumn();
const parentTable = await parentCol.getModel(); const parentTable = await parentCol.getModel();
const childModel = await Model.getBaseModelSQL({ const childModel = await Model.getBaseModelSQL({
model: childTable, model: childTable,
dbDriver: this.dbDriver dbDriver: this.dbDriver,
}); });
await parentTable.getColumns(); await parentTable.getColumns();
@ -501,11 +513,11 @@ class BaseModelSqlv2 {
const proto = await ( const proto = await (
await Model.getBaseModelSQL({ await Model.getBaseModelSQL({
id: childTable.id, id: childTable.id,
dbDriver: this.dbDriver dbDriver: this.dbDriver,
}) })
).getProto(); ).getProto();
return children.map(c => { return children.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}); });
@ -519,11 +531,15 @@ class BaseModelSqlv2 {
try { try {
// const { cn } = this.hasManyRelations.find(({ tn }) => tn === child) || {}; // const { cn } = this.hasManyRelations.find(({ tn }) => tn === child) || {};
const relColumn = (await this.model.getColumns()).find( const relColumn = (await this.model.getColumns()).find(
c => c.id === colId (c) => c.id === colId
); );
const chilCol = await ((await relColumn.getColOptions()) as LinkToAnotherRecordColumn).getChildColumn(); const chilCol = await (
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn
).getChildColumn();
const childTable = await chilCol.getModel(); const childTable = await chilCol.getModel();
const parentCol = await ((await relColumn.getColOptions()) as LinkToAnotherRecordColumn).getParentColumn(); const parentCol = await (
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn
).getParentColumn();
const parentTable = await parentCol.getModel(); const parentTable = await parentCol.getModel();
await parentTable.getColumns(); await parentTable.getColumns();
@ -546,8 +562,11 @@ class BaseModelSqlv2 {
} }
public async multipleMmList({ colId, parentIds }, args?: { limit; offset }) { public async multipleMmList({ colId, parentIds }, args?: { limit; offset }) {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
// const tn = this.model.tn; // const tn = this.model.tn;
// const cn = (await relColOptions.getChildColumn()).title; // const cn = (await relColOptions.getChildColumn()).title;
@ -561,7 +580,7 @@ class BaseModelSqlv2 {
await parentTable.getColumns(); await parentTable.getColumns();
const childModel = await Model.getBaseModelSQL({ const childModel = await Model.getBaseModelSQL({
dbDriver: this.dbDriver, dbDriver: this.dbDriver,
model: childTable model: childTable,
}); });
const rtn = childTable.table_name; const rtn = childTable.table_name;
const rtnId = childTable.id; const rtnId = childTable.id;
@ -570,7 +589,7 @@ class BaseModelSqlv2 {
await childModel.selectObject({ qb }); await childModel.selectObject({ qb });
const finalQb = this.dbDriver.unionAll( const finalQb = this.dbDriver.unionAll(
parentIds.map(id => { parentIds.map((id) => {
const query = qb const query = qb
.clone() .clone()
.whereIn( .whereIn(
@ -595,22 +614,25 @@ class BaseModelSqlv2 {
const proto = await ( const proto = await (
await Model.getBaseModelSQL({ await Model.getBaseModelSQL({
id: rtnId, id: rtnId,
dbDriver: this.dbDriver dbDriver: this.dbDriver,
}) })
).getProto(); ).getProto();
const gs = _.groupBy( const gs = _.groupBy(
children.map(c => { children.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}), }),
GROUP_COL GROUP_COL
); );
return parentIds.map(id => gs[id] || []); return parentIds.map((id) => gs[id] || []);
} }
public async mmList({ colId, parentId }, args?: { limit; offset }) { public async mmList({ colId, parentId }, args?: { limit; offset }) {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
// const tn = this.model.tn; // const tn = this.model.tn;
// const cn = (await relColOptions.getChildColumn()).title; // const cn = (await relColOptions.getChildColumn()).title;
@ -624,7 +646,7 @@ class BaseModelSqlv2 {
await parentTable.getColumns(); await parentTable.getColumns();
const childModel = await Model.getBaseModelSQL({ const childModel = await Model.getBaseModelSQL({
dbDriver: this.dbDriver, dbDriver: this.dbDriver,
model: childTable model: childTable,
}); });
const rtn = childTable.table_name; const rtn = childTable.table_name;
const rtnId = childTable.id; const rtnId = childTable.id;
@ -649,15 +671,18 @@ class BaseModelSqlv2 {
await Model.getBaseModelSQL({ id: rtnId, dbDriver: this.dbDriver }) await Model.getBaseModelSQL({ id: rtnId, dbDriver: this.dbDriver })
).getProto(); ).getProto();
return children.map(c => { return children.map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}); });
} }
public async multipleMmListCount({ colId, parentIds }) { public async multipleMmListCount({ colId, parentIds }) {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
const vtn = (await relColOptions.getMMModel()).table_name; const vtn = (await relColOptions.getMMModel()).table_name;
const vcn = (await relColOptions.getMMChildColumn()).column_name; const vcn = (await relColOptions.getMMChildColumn()).column_name;
@ -678,7 +703,7 @@ class BaseModelSqlv2 {
// await childModel.selectObject({ qb }); // await childModel.selectObject({ qb });
const children = await this.dbDriver.unionAll( const children = await this.dbDriver.unionAll(
parentIds.map(id => { parentIds.map((id) => {
const query = qb const query = qb
.clone() .clone()
.whereIn( .whereIn(
@ -696,12 +721,15 @@ class BaseModelSqlv2 {
); );
const gs = _.groupBy(children, GROUP_COL); const gs = _.groupBy(children, GROUP_COL);
return parentIds.map(id => gs?.[id]?.[0] || []); return parentIds.map((id) => gs?.[id]?.[0] || []);
} }
public async mmListCount({ colId, parentId }) { public async mmListCount({ colId, parentId }) {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
const vtn = (await relColOptions.getMMModel()).table_name; const vtn = (await relColOptions.getMMModel()).table_name;
const vcn = (await relColOptions.getMMChildColumn()).column_name; const vcn = (await relColOptions.getMMChildColumn()).column_name;
@ -738,8 +766,11 @@ class BaseModelSqlv2 {
{ colId, pid = null }, { colId, pid = null },
args args
): Promise<any> { ): Promise<any> {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
const vtn = (await relColOptions.getMMModel()).table_name; const vtn = (await relColOptions.getMMModel()).table_name;
const vcn = (await relColOptions.getMMChildColumn()).column_name; const vcn = (await relColOptions.getMMChildColumn()).column_name;
@ -752,7 +783,7 @@ class BaseModelSqlv2 {
const rtn = childTable.table_name; const rtn = childTable.table_name;
const qb = this.dbDriver(rtn) const qb = this.dbDriver(rtn)
.count(`*`, { as: 'count' }) .count(`*`, { as: 'count' })
.where(qb => { .where((qb) => {
qb.whereNotIn( qb.whereNotIn(
rcn, rcn,
this.dbDriver(rtn) this.dbDriver(rtn)
@ -780,8 +811,11 @@ class BaseModelSqlv2 {
{ colId, pid = null }, { colId, pid = null },
args args
): Promise<any> { ): Promise<any> {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
const vtn = (await relColOptions.getMMModel()).table_name; const vtn = (await relColOptions.getMMModel()).table_name;
const vcn = (await relColOptions.getMMChildColumn()).column_name; const vcn = (await relColOptions.getMMChildColumn()).column_name;
@ -791,13 +825,13 @@ class BaseModelSqlv2 {
const childTable = await (await relColOptions.getParentColumn()).getModel(); const childTable = await (await relColOptions.getParentColumn()).getModel();
const childModel = await Model.getBaseModelSQL({ const childModel = await Model.getBaseModelSQL({
dbDriver: this.dbDriver, dbDriver: this.dbDriver,
model: childTable model: childTable,
}); });
const parentTable = await (await relColOptions.getChildColumn()).getModel(); const parentTable = await (await relColOptions.getChildColumn()).getModel();
await parentTable.getColumns(); await parentTable.getColumns();
const rtn = childTable.table_name; const rtn = childTable.table_name;
const qb = this.dbDriver(rtn).where(qb => const qb = this.dbDriver(rtn).where((qb) =>
qb qb
.whereNotIn( .whereNotIn(
rcn, rcn,
@ -825,7 +859,7 @@ class BaseModelSqlv2 {
const proto = await childModel.getProto(); const proto = await childModel.getProto();
return (await qb).map(c => { return (await qb).map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}); });
@ -836,8 +870,11 @@ class BaseModelSqlv2 {
{ colId, pid = null }, { colId, pid = null },
args args
): Promise<any> { ): Promise<any> {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
const cn = (await relColOptions.getChildColumn()).column_name; const cn = (await relColOptions.getChildColumn()).column_name;
const rcn = (await relColOptions.getParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name;
@ -851,7 +888,7 @@ class BaseModelSqlv2 {
const qb = this.dbDriver(tn) const qb = this.dbDriver(tn)
.count(`*`, { as: 'count' }) .count(`*`, { as: 'count' })
.where(qb => { .where((qb) => {
qb.whereNotIn( qb.whereNotIn(
cn, cn,
this.dbDriver(rtn) this.dbDriver(rtn)
@ -874,8 +911,11 @@ class BaseModelSqlv2 {
{ colId, pid = null }, { colId, pid = null },
args args
): Promise<any> { ): Promise<any> {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
const cn = (await relColOptions.getChildColumn()).column_name; const cn = (await relColOptions.getChildColumn()).column_name;
const rcn = (await relColOptions.getParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name;
@ -885,14 +925,14 @@ class BaseModelSqlv2 {
).getModel(); ).getModel();
const childModel = await Model.getBaseModelSQL({ const childModel = await Model.getBaseModelSQL({
dbDriver: this.dbDriver, dbDriver: this.dbDriver,
model: childTable model: childTable,
}); });
await parentTable.getColumns(); await parentTable.getColumns();
const tn = childTable.table_name; const tn = childTable.table_name;
const rtn = parentTable.table_name; const rtn = parentTable.table_name;
const qb = this.dbDriver(tn).where(qb => { const qb = this.dbDriver(tn).where((qb) => {
qb.whereNotIn( qb.whereNotIn(
cn, cn,
this.dbDriver(rtn) this.dbDriver(rtn)
@ -912,7 +952,7 @@ class BaseModelSqlv2 {
const proto = await childModel.getProto(); const proto = await childModel.getProto();
return (await this.extractRawQueryAndExec(qb)).map(c => { return (await this.extractRawQueryAndExec(qb)).map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}); });
@ -923,8 +963,11 @@ class BaseModelSqlv2 {
{ colId, cid = null }, { colId, cid = null },
args args
): Promise<any> { ): Promise<any> {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
const rcn = (await relColOptions.getParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name;
const parentTable = await ( const parentTable = await (
@ -938,7 +981,7 @@ class BaseModelSqlv2 {
await childTable.getColumns(); await childTable.getColumns();
const qb = this.dbDriver(rtn) const qb = this.dbDriver(rtn)
.where(qb => { .where((qb) => {
qb.whereNotIn( qb.whereNotIn(
rcn, rcn,
this.dbDriver(tn) this.dbDriver(tn)
@ -961,8 +1004,11 @@ class BaseModelSqlv2 {
{ colId, cid = null }, { colId, cid = null },
args args
): Promise<any> { ): Promise<any> {
const relColumn = (await this.model.getColumns()).find(c => c.id === colId); const relColumn = (await this.model.getColumns()).find(
const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; (c) => c.id === colId
);
const relColOptions =
(await relColumn.getColOptions()) as LinkToAnotherRecordColumn;
const rcn = (await relColOptions.getParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name;
const parentTable = await ( const parentTable = await (
@ -972,13 +1018,13 @@ class BaseModelSqlv2 {
const childTable = await (await relColOptions.getChildColumn()).getModel(); const childTable = await (await relColOptions.getChildColumn()).getModel();
const parentModel = await Model.getBaseModelSQL({ const parentModel = await Model.getBaseModelSQL({
dbDriver: this.dbDriver, dbDriver: this.dbDriver,
model: parentTable model: parentTable,
}); });
const rtn = parentTable.table_name; const rtn = parentTable.table_name;
const tn = childTable.table_name; const tn = childTable.table_name;
await childTable.getColumns(); await childTable.getColumns();
const qb = this.dbDriver(rtn).where(qb => { const qb = this.dbDriver(rtn).where((qb) => {
qb.whereNotIn( qb.whereNotIn(
rcn, rcn,
this.dbDriver(tn) this.dbDriver(tn)
@ -998,7 +1044,7 @@ class BaseModelSqlv2 {
applyPaginate(qb, args); applyPaginate(qb, args);
const proto = await parentModel.getProto(); const proto = await parentModel.getProto();
return (await this.extractRawQueryAndExec(qb)).map(c => { return (await this.extractRawQueryAndExec(qb)).map((c) => {
c.__proto__ = proto; c.__proto__ = proto;
return c; return c;
}); });
@ -1028,15 +1074,16 @@ class BaseModelSqlv2 {
(await Column.get({ colId: colOptions.fk_relation_column_id })) (await Column.get({ colId: colOptions.fk_relation_column_id }))
?.title, ?.title,
(await Column.get({ colId: colOptions.fk_lookup_column_id })) (await Column.get({ colId: colOptions.fk_lookup_column_id }))
?.title ?.title,
] ],
}; };
} }
break; break;
case UITypes.LinkToAnotherRecord: case UITypes.LinkToAnotherRecord:
{ {
this._columns[column.title] = column; this._columns[column.title] = column;
const colOptions = (await column.getColOptions()) as LinkToAnotherRecordColumn; const colOptions =
(await column.getColOptions()) as LinkToAnotherRecordColumn;
// const parentColumn = await colOptions.getParentColumn(); // const parentColumn = await colOptions.getParentColumn();
if (colOptions?.type === 'hm') { if (colOptions?.type === 'hm') {
@ -1046,7 +1093,7 @@ class BaseModelSqlv2 {
const data = await this.multipleHmList( const data = await this.multipleHmList(
{ {
colId: column.id, colId: column.id,
ids ids,
}, },
(listLoader as any).args (listLoader as any).args
); );
@ -1056,10 +1103,10 @@ class BaseModelSqlv2 {
await this.hmList( await this.hmList(
{ {
colId: column.id, colId: column.id,
id: ids[0] id: ids[0],
}, },
(listLoader as any).args (listLoader as any).args
) ),
]; ];
} }
} catch (e) { } catch (e) {
@ -1069,7 +1116,7 @@ class BaseModelSqlv2 {
}); });
const self: BaseModelSqlv2 = this; const self: BaseModelSqlv2 = this;
proto[column.title] = async function(args): Promise<any> { proto[column.title] = async function (args): Promise<any> {
(listLoader as any).args = args; (listLoader as any).args = args;
return listLoader.load( return listLoader.load(
getCompositePk(self.model.primaryKeys, this) getCompositePk(self.model.primaryKeys, this)
@ -1090,7 +1137,7 @@ class BaseModelSqlv2 {
const data = await this.multipleMmList( const data = await this.multipleMmList(
{ {
parentIds: ids, parentIds: ids,
colId: column.id colId: column.id,
}, },
(listLoader as any).args (listLoader as any).args
); );
@ -1101,10 +1148,10 @@ class BaseModelSqlv2 {
await this.mmList( await this.mmList(
{ {
parentId: ids[0], parentId: ids[0],
colId: column.id colId: column.id,
}, },
(listLoader as any).args (listLoader as any).args
) ),
]; ];
} }
} catch (e) { } catch (e) {
@ -1115,7 +1162,7 @@ class BaseModelSqlv2 {
const self: BaseModelSqlv2 = this; const self: BaseModelSqlv2 = this;
// const childColumn = await colOptions.getChildColumn(); // const childColumn = await colOptions.getChildColumn();
proto[column.title] = async function(args): Promise<any> { proto[column.title] = async function (args): Promise<any> {
(listLoader as any).args = args; (listLoader as any).args = args;
return await listLoader.load( return await listLoader.load(
getCompositePk(self.model.primaryKeys, this) getCompositePk(self.model.primaryKeys, this)
@ -1123,24 +1170,25 @@ class BaseModelSqlv2 {
}; };
} else if (colOptions.type === 'bt') { } else if (colOptions.type === 'bt') {
// @ts-ignore // @ts-ignore
const colOptions = (await column.getColOptions()) as LinkToAnotherRecordColumn; const colOptions =
(await column.getColOptions()) as LinkToAnotherRecordColumn;
const pCol = await Column.get({ const pCol = await Column.get({
colId: colOptions.fk_parent_column_id colId: colOptions.fk_parent_column_id,
}); });
const cCol = await Column.get({ const cCol = await Column.get({
colId: colOptions.fk_child_column_id colId: colOptions.fk_child_column_id,
}); });
const readLoader = new DataLoader(async (ids: string[]) => { const readLoader = new DataLoader(async (ids: string[]) => {
try { try {
const data = await ( const data = await (
await Model.getBaseModelSQL({ await Model.getBaseModelSQL({
id: pCol.fk_model_id, id: pCol.fk_model_id,
dbDriver: this.dbDriver dbDriver: this.dbDriver,
}) })
).list( ).list(
{ {
// limit: ids.length, // limit: ids.length,
where: `(${pCol.column_name},in,${ids.join(',')})` where: `(${pCol.column_name},in,${ids.join(',')})`,
}, },
true true
); );
@ -1153,7 +1201,7 @@ class BaseModelSqlv2 {
}); });
// defining HasMany count method within GQL Type class // defining HasMany count method within GQL Type class
proto[column.title] = async function() { proto[column.title] = async function () {
if ( if (
this?.[cCol?.title] === null || this?.[cCol?.title] === null ||
this?.[cCol?.title] === undefined this?.[cCol?.title] === undefined
@ -1214,7 +1262,7 @@ class BaseModelSqlv2 {
qb.select( qb.select(
this.dbDriver.raw(`?? as ??`, [ this.dbDriver.raw(`?? as ??`, [
selectQb.builder, selectQb.builder,
sanitize(column.title) sanitize(column.title),
]) ])
); );
} }
@ -1226,7 +1274,7 @@ class BaseModelSqlv2 {
// tn: this.title, // tn: this.title,
knex: this.dbDriver, knex: this.dbDriver,
// column, // column,
columnOptions: (await column.getColOptions()) as RollupColumn columnOptions: (await column.getColOptions()) as RollupColumn,
}) })
).builder.as(sanitize(column.title)) ).builder.as(sanitize(column.title))
); );
@ -1269,7 +1317,7 @@ class BaseModelSqlv2 {
response = await this.extractRawQueryAndExec(query); response = await this.extractRawQueryAndExec(query);
} }
const ai = this.model.columns.find(c => c.ai); const ai = this.model.columns.find((c) => c.ai);
if ( if (
!response || !response ||
(typeof response?.[0] !== 'object' && response?.[0] !== null) (typeof response?.[0] !== 'object' && response?.[0] !== null)
@ -1397,14 +1445,13 @@ class BaseModelSqlv2 {
const postInsertOps = []; const postInsertOps = [];
const nestedCols = (await this.model.getColumns()).filter( const nestedCols = (await this.model.getColumns()).filter(
c => c.uidt === UITypes.LinkToAnotherRecord (c) => c.uidt === UITypes.LinkToAnotherRecord
); );
for (const col of nestedCols) { for (const col of nestedCols) {
if (col.title in data) { if (col.title in data) {
const colOptions = await col.getColOptions< const colOptions =
LinkToAnotherRecordColumn await col.getColOptions<LinkToAnotherRecordColumn>();
>();
// parse data if it's JSON string // parse data if it's JSON string
const nestedData = const nestedData =
@ -1429,11 +1476,11 @@ class BaseModelSqlv2 {
postInsertOps.push(async () => { postInsertOps.push(async () => {
await this.dbDriver(childModel.table_name) await this.dbDriver(childModel.table_name)
.update({ .update({
[childCol.column_name]: rowId [childCol.column_name]: rowId,
}) })
.whereIn( .whereIn(
childModel.primaryKey.column_name, childModel.primaryKey.column_name,
nestedData?.map(r => r[childModel.primaryKey.title]) nestedData?.map((r) => r[childModel.primaryKey.title])
); );
}); });
} }
@ -1442,15 +1489,15 @@ class BaseModelSqlv2 {
postInsertOps.push(async () => { postInsertOps.push(async () => {
const parentModel = await colOptions const parentModel = await colOptions
.getParentColumn() .getParentColumn()
.then(c => c.getModel()); .then((c) => c.getModel());
await parentModel.getColumns(); await parentModel.getColumns();
const parentMMCol = await colOptions.getMMParentColumn(); const parentMMCol = await colOptions.getMMParentColumn();
const childMMCol = await colOptions.getMMChildColumn(); const childMMCol = await colOptions.getMMChildColumn();
const mmModel = await colOptions.getMMModel(); const mmModel = await colOptions.getMMModel();
const rows = nestedData.map(r => ({ const rows = nestedData.map((r) => ({
[parentMMCol.column_name]: r[parentModel.primaryKey.title], [parentMMCol.column_name]: r[parentModel.primaryKey.title],
[childMMCol.column_name]: rowId [childMMCol.column_name]: rowId,
})); }));
await this.dbDriver(mmModel.table_name).insert(rows); await this.dbDriver(mmModel.table_name).insert(rows);
}); });
@ -1473,7 +1520,7 @@ class BaseModelSqlv2 {
response = await query; response = await query;
} }
const ai = this.model.columns.find(c => c.ai); const ai = this.model.columns.find((c) => c.ai);
if ( if (
!response || !response ||
(typeof response?.[0] !== 'object' && response?.[0] !== null) (typeof response?.[0] !== 'object' && response?.[0] !== null)
@ -1503,7 +1550,7 @@ class BaseModelSqlv2 {
rowId = rowId =
response[this.model.primaryKey.title] || response[this.model.primaryKey.title] ||
response[this.model.primaryKey.column_name]; response[this.model.primaryKey.column_name];
await Promise.all(postInsertOps.map(f => f())); await Promise.all(postInsertOps.map((f) => f()));
// if (!trx) { // if (!trx) {
// await driver.commit(); // await driver.commit();
@ -1525,14 +1572,14 @@ class BaseModelSqlv2 {
async bulkInsert( async bulkInsert(
datas: any[], datas: any[],
{ {
chunkSize: _chunkSize = 100 chunkSize: _chunkSize = 100,
}: { }: {
chunkSize?: number; chunkSize?: number;
} = {} } = {}
) { ) {
try { try {
const insertDatas = await Promise.all( const insertDatas = await Promise.all(
datas.map(async d => { datas.map(async (d) => {
await populatePk(this.model, d); await populatePk(this.model, d);
return this.model.mapAliasToColumn(d); return this.model.mapAliasToColumn(d);
}) })
@ -1571,7 +1618,7 @@ class BaseModelSqlv2 {
let transaction; let transaction;
try { try {
const updateDatas = await Promise.all( const updateDatas = await Promise.all(
datas.map(d => this.model.mapAliasToColumn(d)) datas.map((d) => this.model.mapAliasToColumn(d))
); );
transaction = await this.dbDriver.transaction(); transaction = await this.dbDriver.transaction();
@ -1627,14 +1674,14 @@ class BaseModelSqlv2 {
new Filter({ new Filter({
children: args.filterArr || [], children: args.filterArr || [],
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
new Filter({ new Filter({
children: filterObj, children: filterObj,
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
...(args.filterArr || []) ...(args.filterArr || []),
], ],
qb, qb,
this.dbDriver this.dbDriver
@ -1689,14 +1736,14 @@ class BaseModelSqlv2 {
new Filter({ new Filter({
children: args.filterArr || [], children: args.filterArr || [],
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
new Filter({ new Filter({
children: filterObj, children: filterObj,
is_group: true, is_group: true,
logical_op: 'and' logical_op: 'and',
}), }),
...(args.filterArr || []) ...(args.filterArr || []),
], ],
qb, qb,
this.dbDriver this.dbDriver
@ -1730,7 +1777,7 @@ class BaseModelSqlv2 {
), ),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,
user: req?.user?.email user: req?.user?.email,
}); });
// } // }
} }
@ -1774,7 +1821,7 @@ class BaseModelSqlv2 {
description: DOMPurify.sanitize(`${id} deleted from ${this.model.title}`), description: DOMPurify.sanitize(`${id} deleted from ${this.model.title}`),
// details: JSON.stringify(data), // details: JSON.stringify(data),
ip: req?.clientIp, ip: req?.clientIp,
user: req?.user?.email user: req?.user?.email,
}); });
// } // }
await this.handleHooks('After.delete', data, req); await this.handleHooks('After.delete', data, req);
@ -1790,8 +1837,8 @@ class BaseModelSqlv2 {
try { try {
const formView = await view.getView<FormView>(); const formView = await view.getView<FormView>();
const emails = Object.entries(JSON.parse(formView?.email) || {}) const emails = Object.entries(JSON.parse(formView?.email) || {})
.filter(a => a[1]) .filter((a) => a[1])
.map(a => a[0]); .map((a) => a[0]);
if (emails?.length) { if (emails?.length) {
const transformedData = _transformSubmittedFormDataForEmail( const transformedData = _transformSubmittedFormDataForEmail(
data, data,
@ -1805,8 +1852,8 @@ class BaseModelSqlv2 {
html: ejs.render(formSubmissionEmailTemplate, { html: ejs.render(formSubmissionEmailTemplate, {
data: transformedData, data: transformedData,
tn: this.model.table_name, tn: this.model.table_name,
_tn: this.model.title _tn: this.model.title,
}) }),
}); });
} }
} catch (e) { } catch (e) {
@ -1819,7 +1866,7 @@ class BaseModelSqlv2 {
const hooks = await Hook.list({ const hooks = await Hook.list({
fk_model_id: this.model.id, fk_model_id: this.model.id,
event, event,
operation operation,
}); });
for (const hook of hooks) { for (const hook of hooks) {
invokeWebhook(hook, this.model, data, req?.user); invokeWebhook(hook, this.model, data, req?.user);
@ -1889,14 +1936,14 @@ class BaseModelSqlv2 {
async addChild({ async addChild({
colId, colId,
rowId, rowId,
childId childId,
}: { }: {
colId: string; colId: string;
rowId: string; rowId: string;
childId: string; childId: string;
}) { }) {
const columns = await this.model.getColumns(); const columns = await this.model.getColumns();
const column = columns.find(c => c.id === colId); const column = columns.find((c) => c.id === colId);
if (!column || column.uidt !== UITypes.LinkToAnotherRecord) if (!column || column.uidt !== UITypes.LinkToAnotherRecord)
NcError.notFound('Column not found'); NcError.notFound('Column not found');
@ -1925,7 +1972,7 @@ class BaseModelSqlv2 {
[vChildCol.column_name]: this.dbDriver(childTable.table_name) [vChildCol.column_name]: this.dbDriver(childTable.table_name)
.select(childColumn.column_name) .select(childColumn.column_name)
.where(_wherePk(childTable.primaryKeys, rowId)) .where(_wherePk(childTable.primaryKeys, rowId))
.first() .first(),
}); });
} }
break; break;
@ -1939,7 +1986,7 @@ class BaseModelSqlv2 {
.where(_wherePk(parentTable.primaryKeys, rowId)) .where(_wherePk(parentTable.primaryKeys, rowId))
.first() .first()
.as('___cn_alias') .as('___cn_alias')
) ),
}) })
.where(_wherePk(childTable.primaryKeys, childId)); .where(_wherePk(childTable.primaryKeys, childId));
} }
@ -1954,7 +2001,7 @@ class BaseModelSqlv2 {
.where(_wherePk(parentTable.primaryKeys, childId)) .where(_wherePk(parentTable.primaryKeys, childId))
.first() .first()
.as('___cn_alias') .as('___cn_alias')
) ),
}) })
.where(_wherePk(childTable.primaryKeys, rowId)); .where(_wherePk(childTable.primaryKeys, rowId));
} }
@ -1965,14 +2012,14 @@ class BaseModelSqlv2 {
async removeChild({ async removeChild({
colId, colId,
rowId, rowId,
childId childId,
}: { }: {
colId: string; colId: string;
rowId: string; rowId: string;
childId: string; childId: string;
}) { }) {
const columns = await this.model.getColumns(); const columns = await this.model.getColumns();
const column = columns.find(c => c.id === colId); const column = columns.find((c) => c.id === colId);
if (!column || column.uidt !== UITypes.LinkToAnotherRecord) if (!column || column.uidt !== UITypes.LinkToAnotherRecord)
NcError.notFound('Column not found'); NcError.notFound('Column not found');
@ -2002,7 +2049,7 @@ class BaseModelSqlv2 {
[vChildCol.column_name]: this.dbDriver(childTable.table_name) [vChildCol.column_name]: this.dbDriver(childTable.table_name)
.select(childColumn.column_name) .select(childColumn.column_name)
.where(_wherePk(childTable.primaryKeys, rowId)) .where(_wherePk(childTable.primaryKeys, rowId))
.first() .first(),
}) })
.delete(); .delete();
} }
@ -2063,7 +2110,7 @@ function extractSortsObject(
if (!Array.isArray(sorts)) sorts = sorts.split(','); if (!Array.isArray(sorts)) sorts = sorts.split(',');
return sorts.map(s => { return sorts.map((s) => {
const sort: SortType = { direction: 'asc' }; const sort: SortType = { direction: 'asc' };
if (s.startsWith('-')) { if (s.startsWith('-')) {
sort.direction = 'desc'; sort.direction = 'desc';
@ -2133,7 +2180,7 @@ function extractFilterFromXwhere(
children: extractFilterFromXwhere( children: extractFilterFromXwhere(
str.substring(openIndex + 1, closingIndex + 1), str.substring(openIndex + 1, closingIndex + 1),
aliasColObjMap aliasColObjMap
) ),
}), }),
// RHS of nested query(recursion) // RHS of nested query(recursion)
...extractFilterFromXwhere(str.substring(closingIndex + 2), aliasColObjMap) ...extractFilterFromXwhere(str.substring(closingIndex + 2), aliasColObjMap)
@ -2142,7 +2189,7 @@ function extractFilterFromXwhere(
} }
function extractCondition(nestedArrayConditions, aliasColObjMap) { function extractCondition(nestedArrayConditions, aliasColObjMap) {
return nestedArrayConditions?.map(str => { return nestedArrayConditions?.map((str) => {
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const
let [logicOp, alias, op, value] = let [logicOp, alias, op, value] =
str.match(/(?:~(and|or|not))?\((.*?),(\w+),(.*)\)/)?.slice(1) || []; str.match(/(?:~(and|or|not))?\((.*?),(\w+),(.*)\)/)?.slice(1) || [];
@ -2152,7 +2199,7 @@ function extractCondition(nestedArrayConditions, aliasColObjMap) {
comparison_op: op, comparison_op: op,
fk_column_id: aliasColObjMap[alias]?.id, fk_column_id: aliasColObjMap[alias]?.id,
logical_op: logicOp, logical_op: logicOp,
value value,
}); });
}); });
} }
@ -2162,7 +2209,7 @@ function applyPaginate(
{ {
limit = 20, limit = 20,
offset = 0, offset = 0,
ignoreLimit = false ignoreLimit = false,
}: XcFilter & { ignoreLimit?: boolean } }: XcFilter & { ignoreLimit?: boolean }
) { ) {
query.offset(offset); query.offset(offset);
@ -2181,7 +2228,7 @@ function _wherePk(primaryKeys: Column[], id) {
} }
function getCompositePk(primaryKeys: Column[], row) { function getCompositePk(primaryKeys: Column[], row) {
return primaryKeys.map(c => row[c.title]).join('___'); return primaryKeys.map((c) => row[c.title]).join('___');
} }
export { BaseModelSqlv2 }; export { BaseModelSqlv2 };

Loading…
Cancel
Save