Skip to content

Commit

Permalink
fix: validate model names when joining
Browse files Browse the repository at this point in the history
  • Loading branch information
retro committed Jul 31, 2024
1 parent 9509a31 commit 4d196f8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class Repository<
return this.filterFragmentBuilderRegistry;
}

join<N1 extends string, N2 extends string>(
join<N1 extends N, N2 extends N & Exclude<N, N1>>(
type: AnyJoin["type"],
modelName1: N1,
modelName2: N2,
Expand All @@ -122,6 +122,10 @@ export class Repository<

invariant(model1, `Model ${model1} not found in repository`);
invariant(model2, `Model ${model2} not found in repository`);
invariant(
model1.name !== model2.name,
`Model ${model1.name} cannot be joined to itself`,
);

const joinSqlDef = (context: C) => {
const models = {
Expand Down Expand Up @@ -161,31 +165,31 @@ export class Repository<
return this;
}

joinOneToOne<N1 extends string, N2 extends string>(
joinOneToOne<N1 extends N, N2 extends N & Exclude<N, N1>>(
model1: N1,
model2: N2,
joinSqlDefFn: JoinFn<C, string & keyof D, N1, N2>,
) {
return this.join("oneToOne", model1, model2, joinSqlDefFn);
}

joinOneToMany<N1 extends string, N2 extends string>(
joinOneToMany<N1 extends N, N2 extends N & Exclude<N, N1>>(
model1: N1,
model2: N2,
joinSqlDefFn: JoinFn<C, string & keyof D, N1, N2>,
) {
return this.join("oneToMany", model1, model2, joinSqlDefFn);
}

joinManyToOne<N1 extends string, N2 extends string>(
joinManyToOne<N1 extends N, N2 extends N & Exclude<N, N1>>(
model1: N1,
model2: N2,
joinSqlDefFn: JoinFn<C, string & keyof D, N1, N2>,
) {
return this.join("manyToOne", model1, model2, joinSqlDefFn);
}

joinManyToMany<N1 extends string, N2 extends string>(
joinManyToMany<N1 extends N, N2 extends N & Exclude<N, N1>>(
model1: N1,
model2: N2,
joinSqlDefFn: JoinFn<C, string & keyof D, N1, N2>,
Expand Down

0 comments on commit 4d196f8

Please sign in to comment.