diff --git a/package.json b/package.json index 3820be3..0075fa1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wms-core", - "version": "0.22.0-rc.8", + "version": "0.22.0-rc.9", "description": "Node web application framework and toolbelt.", "repository": "https://gitlab.com/ArisuOngaku/wms-core", "author": "Alice Gaudon ", diff --git a/src/db/ModelQuery.ts b/src/db/ModelQuery.ts index 6e9dcd2..2503b37 100644 --- a/src/db/ModelQuery.ts +++ b/src/db/ModelQuery.ts @@ -2,7 +2,7 @@ import {query, QueryResult} from "./MysqlConnectionManager"; import {Connection} from "mysql"; import Model from "./Model"; import Pagination from "../Pagination"; -import ModelRelation, {RelationDatabaseProperties} from "./ModelRelation"; +import ModelRelation, {RecursiveRelationDatabaseProperties} from "./ModelRelation"; import ModelFactory from "./ModelFactory"; @@ -44,7 +44,7 @@ export default class ModelQuery implements WhereFieldConsumer, fields?: SelectFields) { this.type = type; @@ -135,7 +135,7 @@ export default class ModelQuery implements WhereFieldConsumer implements WhereFieldConsumer extends } export class RecursiveModelRelation extends ManyModelRelation { - public constructor(model: M, foreignModelType: ModelType, dbProperties: RelationDatabaseProperties) { + protected readonly dbProperties: RecursiveRelationDatabaseProperties; + + public constructor(model: M, foreignModelType: ModelType, dbProperties: RecursiveRelationDatabaseProperties) { super(model, foreignModelType, dbProperties); + this.dbProperties = dbProperties; this.constraint(query => query.recursive(this.dbProperties)); } @@ -240,3 +243,7 @@ export type PivotRelationDatabaseProperties = RelationDatabaseProperties & { localPivotKey: string; foreignPivotKey: string; }; + +export type RecursiveRelationDatabaseProperties = RelationDatabaseProperties & { + idKey: string; +}; diff --git a/test/ModelQuery.test.ts b/test/ModelQuery.test.ts index 5c97257..6787fe8 100644 --- a/test/ModelQuery.test.ts +++ b/test/ModelQuery.test.ts @@ -59,10 +59,10 @@ describe('Test ModelQuery', () => { const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory, '*'); query.where('f1', 'v1'); query.leftJoin('test').on('model.j1', 'test.j2'); - query.recursive({localKey: 'local', foreignKey: 'foreign'}); + query.recursive({localKey: 'local', foreignKey: 'foreign', idKey: 'foreign'}); query.sortBy('f2', 'ASC').limit(8); - expect(query.toString(true)).toBe("WITH RECURSIVE cte AS (SELECT `model`.* FROM `model` WHERE `f1`=? UNION SELECT o.* FROM `model` AS o, cte AS c WHERE o.`foreign`=c.`local`) SELECT * FROM cte LEFT JOIN `test` ON `model`.`j1`=`test`.`j2` ORDER BY `f2` ASC LIMIT 8"); + expect(query.toString(true)).toBe("WITH RECURSIVE cte AS (SELECT `model`.*,1 AS __depth, ARRAY[`foreign`] AS __path FROM `model` WHERE `f1`=? UNION SELECT o.*, c.__depth + 1,c.__path || o.`foreign` FROM `model` AS o, cte AS c WHERE o.`foreign`=c.`local`) SELECT * FROM cte LEFT JOIN `test` ON `model`.`j1`=`test`.`j2` ORDER BY `f2` ASC LIMIT 8"); expect(query.variables).toStrictEqual(['v1']); });