|
|
|
@ -134,7 +134,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
): Promise<any> { |
|
|
|
|
const qb = this.dbDriver(this.tnPath); |
|
|
|
|
|
|
|
|
|
const { ast, dependencyFields } = await getAst({ |
|
|
|
|
const { ast, dependencyFields, parsedQuery } = await getAst({ |
|
|
|
|
query, |
|
|
|
|
model: this.model, |
|
|
|
|
view: ignoreView ? null : this.viewId && (await View.get(this.viewId)), |
|
|
|
@ -165,7 +165,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
data.__proto__ = proto; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return data ? await nocoExecute(ast, data, {}, dependencyFields) : null; |
|
|
|
|
return data ? await nocoExecute(ast, data, {}, parsedQuery) : null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async exist(id?: any): Promise<any> { |
|
|
|
@ -1509,7 +1509,8 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
const proto: any = { __columnAliases: {} }; |
|
|
|
|
const columns = await this.model.getColumns(); |
|
|
|
|
for (const column of columns) { |
|
|
|
|
await Promise.all( |
|
|
|
|
columns.map(async (column) => { |
|
|
|
|
switch (column.uidt) { |
|
|
|
|
case UITypes.Lookup: |
|
|
|
|
{ |
|
|
|
@ -1537,7 +1538,6 @@ class BaseModelSqlv2 {
|
|
|
|
|
this._columns[column.title] = column; |
|
|
|
|
const colOptions = |
|
|
|
|
(await column.getColOptions()) as LinkToAnotherRecordColumn; |
|
|
|
|
// const parentColumn = await colOptions.getParentColumn();
|
|
|
|
|
|
|
|
|
|
if (colOptions?.type === 'hm') { |
|
|
|
|
const listLoader = new DataLoader(async (ids: string[]) => { |
|
|
|
@ -1574,14 +1574,6 @@ class BaseModelSqlv2 {
|
|
|
|
|
getCompositePk(self.model.primaryKeys, this), |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// defining HasMany count method within GQL Type class
|
|
|
|
|
// Object.defineProperty(type.prototype, column.alias, {
|
|
|
|
|
// async value(): Promise<any> {
|
|
|
|
|
// return listLoader.load(this[model.pk.alias]);
|
|
|
|
|
// },
|
|
|
|
|
// configurable: true
|
|
|
|
|
// });
|
|
|
|
|
} else if (colOptions.type === 'mm') { |
|
|
|
|
const listLoader = new DataLoader(async (ids: string[]) => { |
|
|
|
|
if (ids?.length > 1) { |
|
|
|
@ -1608,7 +1600,6 @@ class BaseModelSqlv2 {
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const self: BaseModelSqlv2 = this; |
|
|
|
|
// const childColumn = await colOptions.getChildColumn();
|
|
|
|
|
proto[ |
|
|
|
|
column.uidt === UITypes.Links |
|
|
|
|
? `_nc_lk_${column.title}` |
|
|
|
@ -1629,7 +1620,36 @@ class BaseModelSqlv2 {
|
|
|
|
|
const cCol = await Column.get({ |
|
|
|
|
colId: colOptions.fk_child_column_id, |
|
|
|
|
}); |
|
|
|
|
const readLoader = new DataLoader(async (ids: string[]) => { |
|
|
|
|
|
|
|
|
|
// use dataloader to get batches of parent data together rather than getting them individually
|
|
|
|
|
// it takes individual keys and callback is invoked with an array of values and we can get the
|
|
|
|
|
// result for all those together and return the value in the same order as in the array
|
|
|
|
|
// this way all parents data extracted together
|
|
|
|
|
const readLoader = new DataLoader(async (_ids: string[]) => { |
|
|
|
|
// handle binary(16) foreign keys
|
|
|
|
|
const ids = _ids.map((id) => { |
|
|
|
|
if (pCol.ct !== 'binary(16)') return id; |
|
|
|
|
|
|
|
|
|
// Cast the id to string.
|
|
|
|
|
const idAsString = id + ''; |
|
|
|
|
// Check if the id is a UUID and the column is binary(16)
|
|
|
|
|
const isUUIDBinary16 = |
|
|
|
|
idAsString.length === 36 || idAsString.length === 32; |
|
|
|
|
// If the id is a UUID and the column is binary(16), convert the id to a Buffer. Otherwise, return null to indicate that the id is not a UUID.
|
|
|
|
|
const idAsUUID = isUUIDBinary16 |
|
|
|
|
? idAsString.length === 32 |
|
|
|
|
? idAsString.replace( |
|
|
|
|
/(.{8})(.{4})(.{4})(.{4})(.{12})/, |
|
|
|
|
'$1-$2-$3-$4-$5', |
|
|
|
|
) |
|
|
|
|
: idAsString |
|
|
|
|
: null; |
|
|
|
|
|
|
|
|
|
return idAsUUID |
|
|
|
|
? Buffer.from(idAsUUID.replace(/-/g, ''), 'hex') |
|
|
|
|
: id; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const data = await ( |
|
|
|
|
await Model.getBaseModelSQL({ |
|
|
|
|
id: pCol.fk_model_id, |
|
|
|
@ -1637,17 +1657,25 @@ class BaseModelSqlv2 {
|
|
|
|
|
}) |
|
|
|
|
).list( |
|
|
|
|
{ |
|
|
|
|
// limit: ids.length,
|
|
|
|
|
where: `(${pCol.column_name},in,${ids.join(',')})`, |
|
|
|
|
fieldsSet: (readLoader as any).args?.fieldsSet, |
|
|
|
|
filterArr: [ |
|
|
|
|
new Filter({ |
|
|
|
|
id: null, |
|
|
|
|
fk_column_id: pCol.id, |
|
|
|
|
fk_model_id: pCol.fk_model_id, |
|
|
|
|
value: ids as any[], |
|
|
|
|
comparison_op: 'in', |
|
|
|
|
}), |
|
|
|
|
], |
|
|
|
|
}, |
|
|
|
|
true, |
|
|
|
|
); |
|
|
|
|
const gs = groupBy(data, pCol.title); |
|
|
|
|
return ids.map(async (id: string) => gs?.[id]?.[0]); |
|
|
|
|
|
|
|
|
|
const groupedList = groupBy(data, pCol.title); |
|
|
|
|
return _ids.map(async (id: string) => groupedList?.[id]?.[0]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// defining HasMany count method within GQL Type class
|
|
|
|
|
// defining BelongsTo read resolver method
|
|
|
|
|
proto[column.title] = async function (args?: any) { |
|
|
|
|
if ( |
|
|
|
|
this?.[cCol?.title] === null || |
|
|
|
@ -1664,7 +1692,8 @@ class BaseModelSqlv2 {
|
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}), |
|
|
|
|
); |
|
|
|
|
this._proto = proto; |
|
|
|
|
return proto; |
|
|
|
|
} |
|
|
|
|