Rework & type relations

This commit is contained in:
Zoe Roux
2025-03-09 16:26:28 +01:00
parent f9ff6c00d7
commit aab38f6a89
4 changed files with 96 additions and 76 deletions
+1
View File
@@ -9,3 +9,4 @@ export * from "./sort";
export * from "./keyset-paginate";
export * from "./db-metadata";
export * from "./original";
export * from "./relations";
+26
View File
@@ -0,0 +1,26 @@
import { type SQL, type Subquery, sql } from "drizzle-orm";
import type { SelectResultField } from "drizzle-orm/query-builders/select.types";
export const buildRelations = <
R extends string,
P extends object,
Rel extends Record<R, (languages: P) => Subquery>,
>(
enabled: R[],
relations: Rel,
params: P,
) => {
// we wrap that in a sql`` instead of using the builder because of this issue
// https://github.com/drizzle-team/drizzle-orm/pull/1674
return Object.fromEntries(
enabled.map((x) => [x, sql`${relations[x](params)}`]),
) as {
[P in R]?: SQL<
ReturnType<Rel[P]>["_"]["selectedFields"] extends {
[key: string]: infer TValue;
}
? SelectResultField<TValue>
: never
>;
};
};