how to infer the type of the db for the postgres driver

hey hey, i am just starting to use drizzle. i really like it! i want to pass the db to another function to avoid the need to recreate it, for that i need to know the type of the db const. can anyone tell me, how to infer it?
export const myFunction = async (
db: ???
): Promise<{ success: boolean}> => {
// do something with db
}
export const myFunction = async (
db: ???
): Promise<{ success: boolean}> => {
// do something with db
}
my question is, how i can infer the type of db for the following code
import {
ScraperRunFailReason,
ScraperRunStatus,
pgPool,
schema,
} from '@mylib'

const db: ??? = drizzle(pgPool, { schema })
await myFunction(db)
import {
ScraperRunFailReason,
ScraperRunStatus,
pgPool,
schema,
} from '@mylib'

const db: ??? = drizzle(pgPool, { schema })
await myFunction(db)
2 Replies
divramod
divramodOP4mo ago
i tried the code snippet from https://github.com/drizzle-team/drizzle-orm/issues/695#issuecomment-2126704308 but i dont know how to use it. when using it like this:
export const myFunction = async (
db: InferQueryModel
): Promise<{ success: boolean}> => {
// do something with db
}
export const myFunction = async (
db: InferQueryModel
): Promise<{ success: boolean}> => {
// do something with db
}
i get the message
typescript: Generic type 'InferQueryModel' requires between 1 and 2 type arguments.
typescript: Generic type 'InferQueryModel' requires between 1 and 2 type arguments.
but i dont know what and how i should pass to InferQueryModel besides that the code snippet from the github issue gives me the following messages
eslint: Don't use `{}` as a type. `{}` actually means "any non-nullish value".
- If you want a type meaning "any object", you probably want `object` instead.
- If you want a type meaning "any value", you probably want `unknown` instead.
- If you want a type meaning "empty object", you probably want `Record<string, never>` instead.
- If you really want a type meaning "any non-nullish value", you probably want `NonNullable<unknown>` instead. [@typescript-eslint/ban-types]
eslint: Don't use `{}` as a type. `{}` actually means "any non-nullish value".
- If you want a type meaning "any object", you probably want `object` instead.
- If you want a type meaning "any value", you probably want `unknown` instead.
- If you want a type meaning "empty object", you probably want `Record<string, never>` instead.
- If you really want a type meaning "any non-nullish value", you probably want `NonNullable<unknown>` instead. [@typescript-eslint/ban-types]
and
typescript: Type 'QBConfig' does not satisfy the constraint 'true | Record<string, unknown>'.
typescript: Type 'QBConfig' does not satisfy the constraint 'true | Record<string, unknown>'.
import type * as schema from './schema'
import type {
BuildQueryResult,
DBQueryConfig,
ExtractTablesWithRelations,
} from 'drizzle-orm'
import type { Exact } from 'type-fest'

type TSchema = ExtractTablesWithRelations<typeof schema>

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 * as schema from './schema'
import type {
BuildQueryResult,
DBQueryConfig,
ExtractTablesWithRelations,
} from 'drizzle-orm'
import type { Exact } from 'type-fest'

type TSchema = ExtractTablesWithRelations<typeof schema>

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>
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...
Hisbaan
Hisbaan4mo ago
Try this:
const db = drizzle(pgPool, { schema });
export type DrizzleClient = typeof db;

// in the other file
import { DrizzleClient } from "the-first-file";
export const myFunction = async (db: DrizzleClient) => {
// ...
}
const db = drizzle(pgPool, { schema });
export type DrizzleClient = typeof db;

// in the other file
import { DrizzleClient } from "the-first-file";
export const myFunction = async (db: DrizzleClient) => {
// ...
}
Want results from more Discord servers?
Add your server