|
|
@ -1509,194 +1509,196 @@ class BaseModelSqlv2 { |
|
|
|
|
|
|
|
|
|
|
|
const proto: any = { __columnAliases: {} }; |
|
|
|
const proto: any = { __columnAliases: {} }; |
|
|
|
const columns = await this.model.getColumns(); |
|
|
|
const columns = await this.model.getColumns(); |
|
|
|
for (const column of columns) { |
|
|
|
await Promise.all( |
|
|
|
switch (column.uidt) { |
|
|
|
columns.map(async (column) => { |
|
|
|
case UITypes.Lookup: |
|
|
|
switch (column.uidt) { |
|
|
|
{ |
|
|
|
case UITypes.Lookup: |
|
|
|
// @ts-ignore
|
|
|
|
{ |
|
|
|
const colOptions: LookupColumn = await column.getColOptions(); |
|
|
|
// @ts-ignore
|
|
|
|
const relCol = await Column.get({ |
|
|
|
const colOptions: LookupColumn = await column.getColOptions(); |
|
|
|
colId: colOptions.fk_relation_column_id, |
|
|
|
const relCol = await Column.get({ |
|
|
|
}); |
|
|
|
colId: colOptions.fk_relation_column_id, |
|
|
|
const relColTitle = |
|
|
|
}); |
|
|
|
relCol.uidt === UITypes.Links |
|
|
|
const relColTitle = |
|
|
|
? `_nc_lk_${relCol.title}` |
|
|
|
relCol.uidt === UITypes.Links |
|
|
|
: relCol.title; |
|
|
|
? `_nc_lk_${relCol.title}` |
|
|
|
proto.__columnAliases[column.title] = { |
|
|
|
: relCol.title; |
|
|
|
path: [ |
|
|
|
proto.__columnAliases[column.title] = { |
|
|
|
relColTitle, |
|
|
|
path: [ |
|
|
|
(await Column.get({ colId: colOptions.fk_lookup_column_id })) |
|
|
|
relColTitle, |
|
|
|
?.title, |
|
|
|
(await Column.get({ colId: colOptions.fk_lookup_column_id })) |
|
|
|
], |
|
|
|
?.title, |
|
|
|
}; |
|
|
|
], |
|
|
|
} |
|
|
|
}; |
|
|
|
break; |
|
|
|
} |
|
|
|
case UITypes.Links: |
|
|
|
break; |
|
|
|
case UITypes.LinkToAnotherRecord: |
|
|
|
case UITypes.Links: |
|
|
|
{ |
|
|
|
case UITypes.LinkToAnotherRecord: |
|
|
|
this._columns[column.title] = column; |
|
|
|
{ |
|
|
|
const colOptions = |
|
|
|
this._columns[column.title] = column; |
|
|
|
(await column.getColOptions()) as LinkToAnotherRecordColumn; |
|
|
|
const colOptions = |
|
|
|
// const parentColumn = await colOptions.getParentColumn();
|
|
|
|
(await column.getColOptions()) as LinkToAnotherRecordColumn; |
|
|
|
|
|
|
|
// const parentColumn = await colOptions.getParentColumn();
|
|
|
|
if (colOptions?.type === 'hm') { |
|
|
|
|
|
|
|
const listLoader = new DataLoader(async (ids: string[]) => { |
|
|
|
if (colOptions?.type === 'hm') { |
|
|
|
if (ids.length > 1) { |
|
|
|
const listLoader = new DataLoader(async (ids: string[]) => { |
|
|
|
const data = await this.multipleHmList( |
|
|
|
if (ids.length > 1) { |
|
|
|
{ |
|
|
|
const data = await this.multipleHmList( |
|
|
|
colId: column.id, |
|
|
|
|
|
|
|
ids, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
(listLoader as any).args, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
return ids.map((id: string) => (data[id] ? data[id] : [])); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return [ |
|
|
|
|
|
|
|
await this.hmList( |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
colId: column.id, |
|
|
|
colId: column.id, |
|
|
|
id: ids[0], |
|
|
|
ids, |
|
|
|
}, |
|
|
|
}, |
|
|
|
(listLoader as any).args, |
|
|
|
(listLoader as any).args, |
|
|
|
), |
|
|
|
); |
|
|
|
]; |
|
|
|
return ids.map((id: string) => (data[id] ? data[id] : [])); |
|
|
|
} |
|
|
|
} else { |
|
|
|
}); |
|
|
|
return [ |
|
|
|
const self: BaseModelSqlv2 = this; |
|
|
|
await this.hmList( |
|
|
|
|
|
|
|
{ |
|
|
|
proto[ |
|
|
|
colId: column.id, |
|
|
|
column.uidt === UITypes.Links |
|
|
|
id: ids[0], |
|
|
|
? `_nc_lk_${column.title}` |
|
|
|
}, |
|
|
|
: column.title |
|
|
|
(listLoader as any).args, |
|
|
|
] = async function (args): Promise<any> { |
|
|
|
), |
|
|
|
(listLoader as any).args = args; |
|
|
|
]; |
|
|
|
return listLoader.load( |
|
|
|
} |
|
|
|
getCompositePk(self.model.primaryKeys, this), |
|
|
|
}); |
|
|
|
); |
|
|
|
const self: BaseModelSqlv2 = this; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
proto[ |
|
|
|
// defining HasMany count method within GQL Type class
|
|
|
|
column.uidt === UITypes.Links |
|
|
|
// Object.defineProperty(type.prototype, column.alias, {
|
|
|
|
? `_nc_lk_${column.title}` |
|
|
|
// async value(): Promise<any> {
|
|
|
|
: column.title |
|
|
|
// return listLoader.load(this[model.pk.alias]);
|
|
|
|
] = async function (args): Promise<any> { |
|
|
|
// },
|
|
|
|
(listLoader as any).args = args; |
|
|
|
// configurable: true
|
|
|
|
return listLoader.load( |
|
|
|
// });
|
|
|
|
getCompositePk(self.model.primaryKeys, this), |
|
|
|
} else if (colOptions.type === 'mm') { |
|
|
|
|
|
|
|
const listLoader = new DataLoader(async (ids: string[]) => { |
|
|
|
|
|
|
|
if (ids?.length > 1) { |
|
|
|
|
|
|
|
const data = await this.multipleMmList( |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
parentIds: ids, |
|
|
|
|
|
|
|
colId: column.id, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
(listLoader as any).args, |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
}; |
|
|
|
return data; |
|
|
|
|
|
|
|
} else { |
|
|
|
// defining HasMany count method within GQL Type class
|
|
|
|
return [ |
|
|
|
// Object.defineProperty(type.prototype, column.alias, {
|
|
|
|
await this.mmList( |
|
|
|
// 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) { |
|
|
|
|
|
|
|
const data = await this.multipleMmList( |
|
|
|
{ |
|
|
|
{ |
|
|
|
parentId: ids[0], |
|
|
|
parentIds: ids, |
|
|
|
colId: column.id, |
|
|
|
colId: column.id, |
|
|
|
}, |
|
|
|
}, |
|
|
|
(listLoader as any).args, |
|
|
|
(listLoader as any).args, |
|
|
|
), |
|
|
|
); |
|
|
|
]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const self: BaseModelSqlv2 = this; |
|
|
|
return data; |
|
|
|
// const childColumn = await colOptions.getChildColumn();
|
|
|
|
} else { |
|
|
|
proto[ |
|
|
|
return [ |
|
|
|
column.uidt === UITypes.Links |
|
|
|
await this.mmList( |
|
|
|
? `_nc_lk_${column.title}` |
|
|
|
{ |
|
|
|
: column.title |
|
|
|
parentId: ids[0], |
|
|
|
] = async function (args): Promise<any> { |
|
|
|
colId: column.id, |
|
|
|
(listLoader as any).args = args; |
|
|
|
}, |
|
|
|
return await listLoader.load( |
|
|
|
(listLoader as any).args, |
|
|
|
getCompositePk(self.model.primaryKeys, this), |
|
|
|
), |
|
|
|
); |
|
|
|
]; |
|
|
|
}; |
|
|
|
} |
|
|
|
} else if (colOptions.type === 'bt') { |
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
|
|
const colOptions = |
|
|
|
|
|
|
|
(await column.getColOptions()) as LinkToAnotherRecordColumn; |
|
|
|
|
|
|
|
const pCol = await Column.get({ |
|
|
|
|
|
|
|
colId: colOptions.fk_parent_column_id, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
const cCol = await Column.get({ |
|
|
|
|
|
|
|
colId: colOptions.fk_child_column_id, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
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 ( |
|
|
|
const self: BaseModelSqlv2 = this; |
|
|
|
await Model.getBaseModelSQL({ |
|
|
|
// const childColumn = await colOptions.getChildColumn();
|
|
|
|
id: pCol.fk_model_id, |
|
|
|
proto[ |
|
|
|
dbDriver: this.dbDriver, |
|
|
|
column.uidt === UITypes.Links |
|
|
|
}) |
|
|
|
? `_nc_lk_${column.title}` |
|
|
|
).list( |
|
|
|
: column.title |
|
|
|
{ |
|
|
|
] = async function (args): Promise<any> { |
|
|
|
fieldsSet: (readLoader as any).args?.fieldsSet, |
|
|
|
(listLoader as any).args = args; |
|
|
|
filterArr: [ |
|
|
|
return await listLoader.load( |
|
|
|
new Filter({ |
|
|
|
getCompositePk(self.model.primaryKeys, this), |
|
|
|
id: null, |
|
|
|
); |
|
|
|
fk_column_id: pCol.id, |
|
|
|
}; |
|
|
|
fk_model_id: pCol.fk_model_id, |
|
|
|
} else if (colOptions.type === 'bt') { |
|
|
|
value: ids as any[], |
|
|
|
// @ts-ignore
|
|
|
|
comparison_op: 'in', |
|
|
|
const colOptions = |
|
|
|
}), |
|
|
|
(await column.getColOptions()) as LinkToAnotherRecordColumn; |
|
|
|
], |
|
|
|
const pCol = await Column.get({ |
|
|
|
}, |
|
|
|
colId: colOptions.fk_parent_column_id, |
|
|
|
true, |
|
|
|
}); |
|
|
|
); |
|
|
|
const cCol = await Column.get({ |
|
|
|
|
|
|
|
colId: colOptions.fk_child_column_id, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
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, |
|
|
|
|
|
|
|
dbDriver: this.dbDriver, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
).list( |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
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); |
|
|
|
const gs = groupBy(data, pCol.title); |
|
|
|
return _ids.map(async (id: string) => gs?.[id]?.[0]); |
|
|
|
return _ids.map(async (id: string) => gs?.[id]?.[0]); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 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 |
|
|
|
) |
|
|
|
) |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
|
|
|
|
|
|
|
|
(readLoader as any).args = args; |
|
|
|
(readLoader as any).args = args; |
|
|
|
|
|
|
|
|
|
|
|
return await readLoader.load(this?.[cCol?.title]); |
|
|
|
return await readLoader.load(this?.[cCol?.title]); |
|
|
|
}; |
|
|
|
}; |
|
|
|
// todo : handle mm
|
|
|
|
// todo : handle mm
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
}), |
|
|
|
} |
|
|
|
); |
|
|
|
this._proto = proto; |
|
|
|
this._proto = proto; |
|
|
|
return proto; |
|
|
|
return proto; |
|
|
|
} |
|
|
|
} |
|
|
|