Browse Source

feat: lookup support - mm/hm lookup - WIP

pull/6987/head
Pranav C 1 year ago
parent
commit
11d5c930c0
  1. 28
      packages/nocodb/src/db/generateMMLookupSelectQuery.ts

28
packages/nocodb/src/db/generateMMLookupSelectQuery.ts

@ -11,18 +11,23 @@ import type {
import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2'; import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2';
import genRollupSelectv2 from '~/db/genRollupSelectv2'; import genRollupSelectv2 from '~/db/genRollupSelectv2';
import { NcError } from '~/helpers/catchError'; import { NcError } from '~/helpers/catchError';
import {getAliasGenerator} from "~/utils";
export default async function generateBTLookupSelectQuery({ export default async function generateBTLookupSelectQuery({
column, column,
baseModelSqlv2, baseModelSqlv2,
alias, alias,
model, model,
getAlias = getAliasGenerator('__lk_slt_')
}: { }: {
column: Column; column: Column;
baseModelSqlv2: BaseModelSqlv2; baseModelSqlv2: BaseModelSqlv2;
alias: string; alias: string;
model: Model; model: Model;
getAlias: ReturnType<typeof getAliasGenerator>
}): Promise<any> { }): Promise<any> {
const knex = baseModelSqlv2.dbDriver; const knex = baseModelSqlv2.dbDriver;
const rootAlias = alias; const rootAlias = alias;
@ -30,7 +35,7 @@ export default async function generateBTLookupSelectQuery({
{ {
let aliasCount = 0, let aliasCount = 0,
selectQb; selectQb;
const alias = `__nc_lk_${aliasCount++}`; const alias = getAlias();
const lookup = await column.getColOptions<LookupColumn>(); const lookup = await column.getColOptions<LookupColumn>();
{ {
const relationCol = await lookup.getRelationColumn(); const relationCol = await lookup.getRelationColumn();
@ -62,7 +67,7 @@ export default async function generateBTLookupSelectQuery({
let lookupColumn = await lookup.getLookupColumn(); let lookupColumn = await lookup.getLookupColumn();
let prevAlias = alias; let prevAlias = alias;
while (lookupColumn.uidt === UITypes.Lookup) { while (lookupColumn.uidt === UITypes.Lookup) {
const nestedAlias = `__nc_lk_nested_${aliasCount++}`; const nestedAlias = getAlias();
const nestedLookup = await lookupColumn.getColOptions<LookupColumn>(); const nestedLookup = await lookupColumn.getColOptions<LookupColumn>();
const relationCol = await nestedLookup.getRelationColumn(); const relationCol = await nestedLookup.getRelationColumn();
const relation = const relation =
@ -70,9 +75,7 @@ export default async function generateBTLookupSelectQuery({
// if any of the relation in nested lookup is // if any of the relation in nested lookup is
// not belongs to then throw error as we don't support // not belongs to then throw error as we don't support
if (relation.type !== RelationTypes.BELONGS_TO) { if (relation.type === RelationTypes.BELONGS_TO) {
// NcError.badRequest('HasMany/ManyToMany lookup is not supported');
}
const childColumn = await relation.getChildColumn(); const childColumn = await relation.getChildColumn();
const parentColumn = await relation.getParentColumn(); const parentColumn = await relation.getParentColumn();
@ -87,13 +90,14 @@ export default async function generateBTLookupSelectQuery({
`${prevAlias}.${childColumn.column_name}`, `${prevAlias}.${childColumn.column_name}`,
); );
} else if (relation.type === RelationTypes.HAS_MANY) {
}else if (relation.type === RelationTypes.MANY_TO_MANY) {
const mmTableAlias = getAlias()
const mmModel = await relation.getMMModel();
const mmChildCol = await relation.getMMChildColumn();
const mmModel = await relationColumnOption.getMMModel(); const mmParentCol = await relation.getMMParentColumn();
const mmChildCol = await relationColumnOption.getMMChildColumn();
const mmParentCol = await relationColumnOption.getMMParentColumn();
knex( knex(
`${baseModelSqlv2.getTnPath( `${baseModelSqlv2.getTnPath(
@ -126,6 +130,10 @@ export default async function generateBTLookupSelectQuery({
}`, }`,
), ),
) )
}
lookupColumn = await nestedLookup.getLookupColumn(); lookupColumn = await nestedLookup.getLookupColumn();
prevAlias = nestedAlias; prevAlias = nestedAlias;

Loading…
Cancel
Save