Way to get relations via typeof someSchema.$inferSelect?

Is there a way to get a type of a database schema with the relations included? Currently, I get my Account type like this:
type Account = typeof accountSchema.$inferSelect
type Account = typeof accountSchema.$inferSelect
But that doesn't give me the relations I've built into the Account type (in my case I have a profile relation similar to the profile_info example in the docs. Do I just need to build this manually? Something like this?
type Account = typeof accountSchema.$inferSelect & {
profile: Profile
}
type Account = typeof accountSchema.$inferSelect & {
profile: Profile
}
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
3 Replies
Sillvva
Sillvva6mo ago
You can do it that way. You can also use this custom helper type.
import * as schema from "$server/db/schema";
import {
type BuildQueryResult,
type DBQueryConfig,
type ExtractTablesWithRelations
} from "drizzle-orm";

type TSchema = ExtractTablesWithRelations<typeof schema>;

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

export type InferQueryModel<
TableName extends keyof TSchema,
QBConfig extends QueryConfig<TableName> = {}
> = BuildQueryResult<
TSchema,
TSchema[TableName],
QBConfig
>;
import * as schema from "$server/db/schema";
import {
type BuildQueryResult,
type DBQueryConfig,
type ExtractTablesWithRelations
} from "drizzle-orm";

type TSchema = ExtractTablesWithRelations<typeof schema>;

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

export type InferQueryModel<
TableName extends keyof TSchema,
QBConfig extends QueryConfig<TableName> = {}
> = BuildQueryResult<
TSchema,
TSchema[TableName],
QBConfig
>;
Then you use it like this. First parameter is base table name, second is the same query object drizzle users for relational queries.
type Result = InferQueryModel<
"logs",
{
columns: { id: true },
with: {
character: {
columns: { id: true }
}
}
}
>;

// type Result = { id: string; character: { id: string; } }
type Result = InferQueryModel<
"logs",
{
columns: { id: true },
with: {
character: {
columns: { id: true }
}
}
}
>;

// type Result = { id: string; character: { id: string; } }
dp
dpOP6mo ago
Awesome, thank you!
Seifeddine  LARGAT
hey i found a more precise one on a github discussion (and i'm using it) :
import type { BuildQueryResult, DBQueryConfig, ExtractTablesWithRelations } from 'drizzle-orm';
import type { Exact } from 'type-fest';// <-- you must install type-fest
import * as schema from '../secure/db/schemas';
type TSchema = ExtractTablesWithRelations<typeof schema>;

export type QueryConfig<TableName extends keyof TSchema> = DBQueryConfig<
'one' | 'many',
boolean,
TSchema,
TSchema[TableName]
>;

export type InferQueryModel<
TableName extends keyof TSchema,
QBConfig extends Exact<QueryConfig<TableName>, QBConfig> = {} // <-- notice Exact here
> = BuildQueryResult<TSchema, TSchema[TableName], QBConfig>;
import type { BuildQueryResult, DBQueryConfig, ExtractTablesWithRelations } from 'drizzle-orm';
import type { Exact } from 'type-fest';// <-- you must install type-fest
import * as schema from '../secure/db/schemas';
type TSchema = ExtractTablesWithRelations<typeof schema>;

export type QueryConfig<TableName extends keyof TSchema> = DBQueryConfig<
'one' | 'many',
boolean,
TSchema,
TSchema[TableName]
>;

export type InferQueryModel<
TableName extends keyof TSchema,
QBConfig extends Exact<QueryConfig<TableName>, QBConfig> = {} // <-- notice Exact here
> = BuildQueryResult<TSchema, TSchema[TableName], QBConfig>;
Want results from more Discord servers?
Add your server