Need help inferring types of nested model

I'm new to drizzle, so obvious skill issue here, but how can I infer types from models with specific relations selected? I know of InferSelectModel but I don't see a way to include relations. Ideally I'd want to infer the type i get from something like this:
async userById(id: string) {
const user = await this.db.query.users.findFirst({
where: eq(users.id, id),
with: {
teamMemberships: {
columns: {
role: true,
createdAt: true,
},
with: {
team: true,
},
},
},
});
return user;
}
async userById(id: string) {
const user = await this.db.query.users.findFirst({
where: eq(users.id, id),
with: {
teamMemberships: {
columns: {
role: true,
createdAt: true,
},
with: {
team: true,
},
},
},
});
return user;
}
6 Replies
Patrik
Patrik2w ago
Have you specified relations?
dayo
dayoOP2w ago
yes I have
dayo
dayoOP2w ago
No description
TOSL
TOSL2w ago
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 UserWithPosts = InferResultType<'users', { posts: true }>
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 UserWithPosts = InferResultType<'users', { posts: true }>
Is this that you mean?
TOSL
TOSL2w ago
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...
dayo
dayoOP2w ago
Ohhh very interesting! I'll try this Thanks for the link as well :boneZone: It works, thank you 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;
}
>;

Did you find this page helpful?