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

Loading…
Cancel
Save