|
|
|
@ -23,23 +23,21 @@ export default async function ({
|
|
|
|
|
const childModel = await childCol?.getModel(); |
|
|
|
|
const parentCol = await relationColumnOption.getParentColumn(); |
|
|
|
|
const parentModel = await parentCol?.getModel(); |
|
|
|
|
const refTableAlias = `__nc_rollup`; |
|
|
|
|
|
|
|
|
|
switch (relationColumnOption.type) { |
|
|
|
|
case RelationTypes.HAS_MANY: |
|
|
|
|
// if (!rollup.table_name || !rollup.rtn) {
|
|
|
|
|
// rollup = { ...rollup, ...hasMany.find(hm => hm.table_name === rollup.rltn) };
|
|
|
|
|
// }
|
|
|
|
|
return { |
|
|
|
|
builder: knex(childModel?.table_name) |
|
|
|
|
builder: knex(`${childModel?.table_name} as ${refTableAlias}`) |
|
|
|
|
[columnOptions.rollup_function]?.( |
|
|
|
|
knex.ref(`${childModel?.table_name}.${rollupColumn.column_name}`) |
|
|
|
|
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`) |
|
|
|
|
) |
|
|
|
|
.where( |
|
|
|
|
knex.ref( |
|
|
|
|
`${alias || parentModel.table_name}.${parentCol.column_name}` |
|
|
|
|
), |
|
|
|
|
'=', |
|
|
|
|
knex.ref(`${childModel.table_name}.${childCol.column_name}`) |
|
|
|
|
knex.ref(`${refTableAlias}.${childCol.column_name}`) |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
case RelationTypes.MANY_TO_MANY: { |
|
|
|
@ -48,15 +46,15 @@ export default async function ({
|
|
|
|
|
const mmParentCol = await relationColumnOption.getMMParentColumn(); |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
builder: knex(parentModel.table_name) |
|
|
|
|
builder: knex(`${parentModel?.table_name} as ${refTableAlias}`) |
|
|
|
|
[columnOptions.rollup_function]?.( |
|
|
|
|
knex.ref(`${parentModel.table_name}.${rollupColumn.column_name}`) |
|
|
|
|
knex.ref(`${refTableAlias}.${rollupColumn.column_name}`) |
|
|
|
|
) |
|
|
|
|
.innerJoin( |
|
|
|
|
mmModel.table_name, |
|
|
|
|
knex.ref(`${mmModel.table_name}.${mmParentCol.column_name}`), |
|
|
|
|
'=', |
|
|
|
|
knex.ref(`${parentModel.table_name}.${parentCol.column_name}`) |
|
|
|
|
knex.ref(`${refTableAlias}.${parentCol.column_name}`) |
|
|
|
|
) |
|
|
|
|
.where( |
|
|
|
|
knex.ref(`${mmModel.table_name}.${mmChildCol.column_name}`), |
|
|
|
|