prisma / extension-read-replicas

Prisma Client Extension to add read replica support to your Prisma Client
Apache License 2.0
101 stars 6 forks source link

Return this when context is transactional #16

Closed casey-chow closed 11 months ago

casey-chow commented 1 year ago

I have some functions where I need to make sure I call .$primary() in my database (specifically because I'm using INSERT ... RETURNING, so I need to manually overwrite $queryRaw to hit the primary), but if I call .$primary() it returns the main client rather than the expected transaction client. In the context of a transaction, it makes sense for txn.$primary() to return itself idempotently, so that write-facing code can be agnostic to whether the Prisma client is transactional or not.

That's to say:

prisma.$transaction(async (txn) => {
    return txn.$primary().$queryRaw`INSERT ... RETURNING *`;
});

Should run within the context of the transaction, rather than outside of it.

casey-chow commented 11 months ago

Fixed in #21.