Infer typescript type from table with relations

I have a pg table with a one to many relationship. I would like to get the typescript type of this table including the one to many relation. I've attached images of my schema files. Currently, I have come up with a solution which seems to work but seems a 'bit scuffed' as I can see it being prone to needing ammendment if I change relations in the future. This is the type I have currently come up with.
type pgGameSummary = InferSelectModel<typeof games> & {
moves: InferSelectModel<typeof moves>[];
};
type pgGameSummary = InferSelectModel<typeof games> & {
moves: InferSelectModel<typeof moves>[];
};
Is there some other 'infer......` type that I can use that will achieve what I need?
No description
No description
3 Replies
Sillvva
Sillvva8mo ago
From Github: https://github.com/drizzle-team/drizzle-orm/issues/695#issuecomment-1881454650
import * as schema from "$server/db/schema";
import type { BuildQueryResult, DBQueryConfig, ExtractTablesWithRelations } from "drizzle-orm";

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

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

export type InferResultType<
TableName extends keyof TSchema,
With extends IncludeRelation<TableName> | undefined = undefined
> = BuildQueryResult<
TSchema,
TSchema[TableName],
{
with: With;
}
>;

type X = InferResultType<"logs", { character: { with: { user: true } } }>
import * as schema from "$server/db/schema";
import type { BuildQueryResult, DBQueryConfig, ExtractTablesWithRelations } from "drizzle-orm";

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

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

export type InferResultType<
TableName extends keyof TSchema,
With extends IncludeRelation<TableName> | undefined = undefined
> = BuildQueryResult<
TSchema,
TSchema[TableName],
{
with: With;
}
>;

type X = InferResultType<"logs", { character: { with: { user: true } } }>
GitHub
Implement infering table model with relations · Issue #695 · drizzl...
Prisma API: import { Prisma } from '@prisma/client' // 1: Define a type that includes the relation to Post const userWithPosts = Prisma.validator<Prisma.UserDefaultArgs>()({ include...
Sillvva
Sillvva8mo ago
This also allows you to specify columns on the parent table
import * as schema from "$server/db/schema";
import type { BuildQueryResult, DBQueryConfig, ExtractTablesWithRelations } from "drizzle-orm";

type TSchema = ExtractTablesWithRelations<typeof schema>;
type QueryConfig<TableName extends keyof TSchema> = DBQueryConfig<"one" | "many", boolean, TSchema, TSchema[TableName]>;

export type InferResultType<
TableName extends keyof TSchema,
QBConfig extends QueryConfig<TableName> | {} = {}
> = BuildQueryResult<TSchema, TSchema[TableName], QBConfig>;

type Result = InferResultType<
"logs",
{
columns: { id: true },
with: {
character: {
columns: { id: true }
}
}
}
>
import * as schema from "$server/db/schema";
import type { BuildQueryResult, DBQueryConfig, ExtractTablesWithRelations } from "drizzle-orm";

type TSchema = ExtractTablesWithRelations<typeof schema>;
type QueryConfig<TableName extends keyof TSchema> = DBQueryConfig<"one" | "many", boolean, TSchema, TSchema[TableName]>;

export type InferResultType<
TableName extends keyof TSchema,
QBConfig extends QueryConfig<TableName> | {} = {}
> = BuildQueryResult<TSchema, TSchema[TableName], QBConfig>;

type Result = InferResultType<
"logs",
{
columns: { id: true },
with: {
character: {
columns: { id: true }
}
}
}
>
kal
kalOP7mo ago
Thank you buddy! This seems to be exactly what I was looking for. Appreciate it 👍
Want results from more Discord servers?
Add your server