reworked WithBuilder to allow recursive with operations

This commit is contained in:
dswbx
2025-01-16 15:25:30 +01:00
parent 37a65bcaf6
commit 26a5fd8b34
10 changed files with 305 additions and 102 deletions

View File

@@ -158,28 +158,12 @@ export class ManyToOneRelation extends EntityRelation<typeof ManyToOneRelation.s
buildWith(entity: Entity, reference: string) {
const { self, entityRef, otherRef, relationRef } = this.queryInfo(entity, reference);
const limit =
self.cardinality === 1
? 1
: (this.config.with_limit ?? ManyToOneRelation.DEFAULTS.with_limit);
//console.log("buildWith", entity.name, reference, { limit });
return (eb: ExpressionBuilder<any, any>) =>
eb
.selectFrom(`${self.entity.name} as ${relationRef}`)
.select(self.entity.getSelect(relationRef))
.whereRef(entityRef, "=", otherRef)
.limit(limit);
/*return qb.select((eb) =>
jsonFrom(
eb
.selectFrom(`${self.entity.name} as ${relationRef}`)
.select(self.entity.getSelect(relationRef))
.whereRef(entityRef, "=", otherRef)
.limit(limit)
).as(relationRef)
);*/
.$if(self.cardinality === 1, (qb) => qb.limit(1));
}
/**

View File

@@ -90,26 +90,13 @@ export class PolymorphicRelation extends EntityRelation<typeof PolymorphicRelati
buildWith(entity: Entity) {
const { other, whereLhs, reference, entityRef, otherRef } = this.queryInfo(entity);
const limit = other.cardinality === 1 ? 1 : 5;
return (eb: ExpressionBuilder<any, any>) =>
eb
.selectFrom(other.entity.name)
.select(other.entity.getSelect(other.entity.name))
.where(whereLhs, "=", reference)
.whereRef(entityRef, "=", otherRef)
.limit(limit);
/*return qb.select((eb) =>
jsonFrom(
eb
.selectFrom(other.entity.name)
.select(other.entity.getSelect(other.entity.name))
.where(whereLhs, "=", reference)
.whereRef(entityRef, "=", otherRef)
.limit(limit)
).as(other.reference)
);*/
.$if(other.cardinality === 1, (qb) => qb.limit(1));
}
override isListableFor(entity: Entity): boolean {