drizzle-team/drizzle-orm

[BUG]: Type Error in PgTransaction Missing $client Property After Upgrading to drizzle-orm@0.35.1

Closed this issue · 4 comments

What version of drizzle-orm are you using?

0.35.1

What version of drizzle-kit are you using?

0.26.2

Describe the Bug

Hello Drizzle ORM Team,

I'm encountering a TypeScript type error after upgrading to drizzle-orm@0.35.1 that appears to be related to the $client property when using transactions with PgTransaction.

Argument of type 'PgTransaction<NodePgQueryResultHKT, { achievementsRelations: Relations<"achievement", { user: One<"users", true>; creator: One<"users", true>; }>; ... 391 more ...; examFragmentClosureClosure: PgTableWithColumns<...>; }, ExtractTablesWithRelations<...>>' is not assignable to parameter of type 'NodePgDatabase<{ achievementsRelations: Relations<"achievement", { user: One<"users", true>; creator: One<"users", true>; }>; alternativeCommentSolutionRelations: Relations<"alternative_comment_solution", { ...; }>; ... 390 more ...; examFragmentClosureClosure: PgTableWithColumns<...>; }> & { ...; }'.
### The error message:

Property '$client' is missing in type 'PgTransaction<NodePgQueryResultHKT, { achievementsRelations: Relations<"achievement", { user: One<"users", true>; creator: One<"users", true>; }>; ... 391 more ...; examFragmentClosureClosure: PgTableWithColumns<...>; }, ExtractTablesWithRelations<...>>' but required in type '{ $client: Pool; }'.ts(2345)

Expected behavior

I expected that a transaction object (tx) created via db.transaction would be compatible with operations requiring $client, especially when working with multiple relations.

It appears that the PgTransaction type does not automatically inherit or include the $client property in the same way that the NodePgDatabase type does. This mismatch between PgTransaction and NodePgDatabase is likely causing the type error when passing the transaction object to any operation that expects $client.

Steps to reproduce:

  1. Use the db.transaction method to start a transaction.
  2. Attempt to pass the transaction object to any function that expects NodePgDatabase, which requires $client.
  3. TypeScript throws an error indicating that $client is missing from the PgTransaction object.

Example how I apply this pattern on my codebase:

export const grantDocumentPermissionsWithToken = async (
  {
    documentId,
    documentType,
    userId,
  }: Omit<UpdateAllUserPermissionsForDocument, "actions">,
  token: string,
  dbOrTx = db
) => {
  return dbOrTx.transaction(async (tx) => {
    const accessControlFound = await tx.query.documentAccessControl.findFirst({
      where: and(
        eq(documentAccessControl.documentId, documentId),
        eq(documentAccessControl.token, token)
      ),
      columns: { id: true, tokenPermissions: true },
    });

    if (!accessControlFound) {
      return tx.rollback();
    }

    await updateAllUserPermissionsForDocument(
      {
        documentId,
        userId,
        documentType,
        actions: accessControlFound.tokenPermissions as Actions<"document">[],
      },
      tx
    );

    await refreshUserSessionData(userId, tx);
  });
};

Environment & setup

Context:

  • I am using PgTransaction in a transaction block to perform operations across multiple tables with relations.
  • The type error suggests that the transaction object (tx: PgTransaction) is missing the $client property, which seems to be a new requirement introduced with the recent version, where $client is exposed for access to the underlying driver.
  • This error occurs when passing the PgTransaction object to a function expecting a NodePgDatabase.
  • Using drizzle form node-postgress;

Note: I don't think this is a bug, but it is a breaking change for me. I apologize for labeling it as a bug.

Hey, I see that drizzle-orm@0.35.2 was released, but after upgrading I am still getting this same error but with MySQL:

error TS2322: Type 'My
│ SqlTransaction<MySql2QueryResultHKT, MySql2PreparedQueryHKT, typeof import("/home/ga
│ briel/Documents/Github/kodix-turbo/packages/db/dist/schema/index"), ExtractTablesWit
│ hRelations<...>>' is not assignable to type 'MySql2Database<typeof import("/home/gab
│ riel/Documents/Github/kodix-turbo/packages/db/dist/schema/index")> & { $client: Pool
│ ; }'.
│   Property '$client' is missing in type 'MySqlTransaction<MySql2QueryResultHKT, MySq
│ l2PreparedQueryHKT, typeof import("/home/gabriel/Documents/Github/kodix-turbo/packag
│ es/db/dist/schema/index"), ExtractTablesWithRelations<...>>' but required in type '{
│  $client: Pool; }'.

Did you find a good workaround for this? I'm currently facing the same issue

Since the latest release was supposed to fix it but I still see the error on MySQL, I created an issue for this:
#3163

Closing in favor of #3175.