|
|
|
@ -57,6 +57,9 @@ 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) { |
|
|
|
@ -202,14 +205,18 @@ class BaseModelSqlv2 {
|
|
|
|
|
): Promise<any> { |
|
|
|
|
const { where, fields, ...rest } = this._getListArgs(args as any); |
|
|
|
|
|
|
|
|
|
const qb = this.dbDriver(this.tnPath); |
|
|
|
|
const innerQb = this.dbDriver(this.tnPath); |
|
|
|
|
innerQb.select('*') |
|
|
|
|
|
|
|
|
|
const wrapperQb = this.dbDriver.from(innerQb.as(INNER_QUERY_ALIAS)) |
|
|
|
|
await this.selectObject({ |
|
|
|
|
qb, |
|
|
|
|
qb: wrapperQb, |
|
|
|
|
fieldsSet: args.fieldsSet, |
|
|
|
|
viewId: this.viewId, |
|
|
|
|
alias: INNER_QUERY_ALIAS |
|
|
|
|
}); |
|
|
|
|
if (+rest?.shuffle) { |
|
|
|
|
await this.shuffle({ qb }); |
|
|
|
|
await this.shuffle({ qb: innerQb }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const aliasColObjMap = await this.model.getAliasColObjMap(); |
|
|
|
@ -235,7 +242,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
logical_op: 'and', |
|
|
|
|
}), |
|
|
|
|
], |
|
|
|
|
qb, |
|
|
|
|
innerQb, |
|
|
|
|
this.dbDriver |
|
|
|
|
); |
|
|
|
|
|
|
|
|
@ -244,7 +251,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
? args.sortArr |
|
|
|
|
: await Sort.list({ viewId: this.viewId }); |
|
|
|
|
|
|
|
|
|
await sortV2(sorts, qb, this.dbDriver); |
|
|
|
|
await sortV2(sorts, innerQb, this.dbDriver); |
|
|
|
|
} else { |
|
|
|
|
await conditionV2( |
|
|
|
|
[ |
|
|
|
@ -259,31 +266,31 @@ class BaseModelSqlv2 {
|
|
|
|
|
logical_op: 'and', |
|
|
|
|
}), |
|
|
|
|
], |
|
|
|
|
qb, |
|
|
|
|
innerQb, |
|
|
|
|
this.dbDriver |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
if (!sorts) sorts = args.sortArr; |
|
|
|
|
|
|
|
|
|
await sortV2(sorts, qb, this.dbDriver); |
|
|
|
|
await sortV2(sorts, innerQb, 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) { |
|
|
|
|
qb.orderBy(this.model.primaryKey.column_name); |
|
|
|
|
innerQb.orderBy(this.model.primaryKey.column_name); |
|
|
|
|
} else if (this.model.columns.find((c) => c.column_name === 'created_at')) { |
|
|
|
|
qb.orderBy('created_at'); |
|
|
|
|
innerQb.orderBy('created_at'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!ignoreViewFilterAndSort) applyPaginate(qb, rest); |
|
|
|
|
if (!ignoreViewFilterAndSort) applyPaginate(innerQb, rest); |
|
|
|
|
const proto = await this.getProto(); |
|
|
|
|
|
|
|
|
|
console.log('list query', qb.toQuery()); |
|
|
|
|
const data = await this.execAndParse(wrapperQb); |
|
|
|
|
|
|
|
|
|
const data = await this.execAndParse(qb); |
|
|
|
|
// todo: remove
|
|
|
|
|
console.log(wrapperQb.toQuery()); |
|
|
|
|
|
|
|
|
|
// console.log(qb.toQuery());
|
|
|
|
|
|
|
|
|
|
return data?.map((d) => { |
|
|
|
|
d.__proto__ = proto; |
|
|
|
@ -1247,7 +1254,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async getSelectQueryBuilderForFormula(column: Column<any>) { |
|
|
|
|
private async getSelectQueryBuilderForFormula(column: Column<any>, tableAlias?:string ) { |
|
|
|
|
const formula = await column.getColOptions<FormulaColumn>(); |
|
|
|
|
if (formula.error) throw new Error(`Formula error: ${formula.error}`); |
|
|
|
|
const qb = await formulaQueryBuilderv2( |
|
|
|
@ -1255,7 +1262,9 @@ class BaseModelSqlv2 {
|
|
|
|
|
null, |
|
|
|
|
this.dbDriver, |
|
|
|
|
this.model, |
|
|
|
|
column |
|
|
|
|
column, |
|
|
|
|
{ }, |
|
|
|
|
tableAlias |
|
|
|
|
); |
|
|
|
|
return qb; |
|
|
|
|
} |
|
|
|
@ -1473,6 +1482,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
extractPkAndPv, |
|
|
|
|
viewId, |
|
|
|
|
fieldsSet, |
|
|
|
|
alias |
|
|
|
|
}: { |
|
|
|
|
fieldsSet?: Set<string>; |
|
|
|
|
qb: Knex.QueryBuilder; |
|
|
|
@ -1480,6 +1490,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
fields?: string[] | string; |
|
|
|
|
extractPkAndPv?: boolean; |
|
|
|
|
viewId?: string; |
|
|
|
|
alias?: string; |
|
|
|
|
}): Promise<void> { |
|
|
|
|
const view = await View.get(viewId); |
|
|
|
|
const viewColumns = viewId && (await View.getColumns(viewId)); |
|
|
|
@ -1531,7 +1542,8 @@ class BaseModelSqlv2 {
|
|
|
|
|
case UITypes.Formula: |
|
|
|
|
try { |
|
|
|
|
const selectQb = await this.getSelectQueryBuilderForFormula( |
|
|
|
|
qrValueColumn |
|
|
|
|
qrValueColumn, |
|
|
|
|
alias |
|
|
|
|
); |
|
|
|
|
qb.select({ |
|
|
|
|
[column.column_name]: selectQb.builder, |
|
|
|
@ -1563,7 +1575,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
case UITypes.Formula: |
|
|
|
|
try { |
|
|
|
|
const selectQb = await this.getSelectQueryBuilderForFormula( |
|
|
|
|
barcodeValueColumn |
|
|
|
|
barcodeValueColumn, |
|
|
|
|
); |
|
|
|
|
qb.select({ |
|
|
|
|
[column.column_name]: selectQb.builder, |
|
|
|
@ -1586,7 +1598,8 @@ class BaseModelSqlv2 {
|
|
|
|
|
{ |
|
|
|
|
try { |
|
|
|
|
const selectQb = await this.getSelectQueryBuilderForFormula( |
|
|
|
|
column |
|
|
|
|
column, |
|
|
|
|
alias |
|
|
|
|
); |
|
|
|
|
qb.select( |
|
|
|
|
this.dbDriver.raw(`?? as ??`, [ |
|
|
|
@ -1594,7 +1607,8 @@ class BaseModelSqlv2 {
|
|
|
|
|
sanitize(column.title), |
|
|
|
|
]) |
|
|
|
|
); |
|
|
|
|
} catch { |
|
|
|
|
} catch(e) { |
|
|
|
|
console.log(e) |
|
|
|
|
// return dummy select
|
|
|
|
|
qb.select( |
|
|
|
|
this.dbDriver.raw(`'ERR' as ??`, [sanitize(column.title)]) |
|
|
|
@ -1609,6 +1623,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
// tn: this.title,
|
|
|
|
|
knex: this.dbDriver, |
|
|
|
|
// column,
|
|
|
|
|
alias, |
|
|
|
|
columnOptions: (await column.getColOptions()) as RollupColumn, |
|
|
|
|
}) |
|
|
|
|
).builder.as(sanitize(column.title)) |
|
|
|
@ -1616,7 +1631,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
res[sanitize(column.title || column.column_name)] = sanitize( |
|
|
|
|
`${this.model.table_name}.${column.column_name}` |
|
|
|
|
`${alias || this.model.table_name}.${column.column_name}` |
|
|
|
|
); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|