dayo
dayo
DTDrizzle Team
Created by dayo on 1/12/2025 in #help
Need help inferring types of nested model
I went with the expanded version.. allowing column inclusion/exclusion
import type {
BuildQueryResult,
DBQueryConfig,
ExtractTablesWithRelations,
} from "drizzle-orm";

import * as schema from "@/lib/db/schema";

type Schema = typeof schema;
type TablesWithRelations = ExtractTablesWithRelations<Schema>;

export type IncludeRelation<TableName extends keyof TablesWithRelations> =
DBQueryConfig<
"one" | "many",
boolean,
TablesWithRelations,
TablesWithRelations[TableName]
>["with"];

export type IncludeColumns<TableName extends keyof TablesWithRelations> =
DBQueryConfig<
"one" | "many",
boolean,
TablesWithRelations,
TablesWithRelations[TableName]
>["columns"];

export type InferQueryModel<
TableName extends keyof TablesWithRelations,
Columns extends IncludeColumns<TableName> | undefined = undefined,
With extends IncludeRelation<TableName> | undefined = undefined,
> = BuildQueryResult<
TablesWithRelations,
TablesWithRelations[TableName],
{
columns: Columns;
with: With;
}
>;
import type {
BuildQueryResult,
DBQueryConfig,
ExtractTablesWithRelations,
} from "drizzle-orm";

import * as schema from "@/lib/db/schema";

type Schema = typeof schema;
type TablesWithRelations = ExtractTablesWithRelations<Schema>;

export type IncludeRelation<TableName extends keyof TablesWithRelations> =
DBQueryConfig<
"one" | "many",
boolean,
TablesWithRelations,
TablesWithRelations[TableName]
>["with"];

export type IncludeColumns<TableName extends keyof TablesWithRelations> =
DBQueryConfig<
"one" | "many",
boolean,
TablesWithRelations,
TablesWithRelations[TableName]
>["columns"];

export type InferQueryModel<
TableName extends keyof TablesWithRelations,
Columns extends IncludeColumns<TableName> | undefined = undefined,
With extends IncludeRelation<TableName> | undefined = undefined,
> = BuildQueryResult<
TablesWithRelations,
TablesWithRelations[TableName],
{
columns: Columns;
with: With;
}
>;
11 replies
DTDrizzle Team
Created by dayo on 1/12/2025 in #help
Need help inferring types of nested model
It works, thank you
11 replies
DTDrizzle Team
Created by dayo on 1/12/2025 in #help
Need help inferring types of nested model
Thanks for the link as well :boneZone:
11 replies
DTDrizzle Team
Created by dayo on 1/12/2025 in #help
Need help inferring types of nested model
Ohhh very interesting! I'll try this
11 replies
DTDrizzle Team
Created by dayo on 1/12/2025 in #help
Need help inferring types of nested model
No description
11 replies
DTDrizzle Team
Created by dayo on 1/12/2025 in #help
Need help inferring types of nested model
yes I have
11 replies