Replies: 4 comments 6 replies
-
|
I found that whether export type DatabaseType = typeof database;
export type TransactionType = Parameters<Parameters<DatabaseType['transaction']>[0]>[0]; |
Beta Was this translation helpful? Give feedback.
-
|
Hello! I recently realized that drizzle actually already provides typed Are you defining a union import type { drizzle as postgres } from "drizzle-orm/node-postgres";
import type { drizzle as pglite } from "drizzle-orm/pglite";
import type * as schema from "../schema/index";
export type Schema = typeof schema;
export type Postgres = ReturnType<typeof postgres<Schema>>;
export type PGlite = ReturnType<typeof pglite<Schema>>;
export type Database = Postgres | PGlite;
const any = (db: Database) => {
// (parameter) tx: any
db.transaction(tx => {})
}
const correct = (db: Postgres) => {
// (parameter) tx: PgTransaction<NodePgQueryResultHKT, typeof schema, ExtractTablesWithRelations<typeof schema>>
db.transaction(tx => {})
} |
Beta Was this translation helpful? Give feedback.
-
|
import type { ExtractTablesWithRelations,} from 'drizzle-orm';
import type { NodePgQueryResultHKT } from 'drizzle-orm/node-postgres';
import { PgTransaction } from 'drizzle-orm/pg-core';
import type * as schema from '@/database/schema.js';
export type Transaction = PgTransaction<NodePgQueryResultHKT, typeof schema, ExtractTablesWithRelations<typeof schema>>;You can now simply import and use |
Beta Was this translation helpful? Give feedback.
-
|
For anyone else discovering this discussion, this is how you infer the type as of the latest beta (11): import type { PgAsyncTransaction } from "drizzle-orm/pg-core";
import type { PostgresJsQueryResultHKT } from "drizzle-orm/postgres-js";
import { relations } from "./relations.ts";
export type DbTransactionType = PgAsyncTransaction<PostgresJsQueryResultHKT, Record<string, never>, typeof relations>; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I’m using Drizzle ORM and need to pass the transaction object (
tx) to another function. Currently, I have definedtxasany, but I’d like to replace it with the correct, type-safe version.Here’s an example of my function:
Beta Was this translation helpful? Give feedback.
All reactions