Registry / database / drizzle-transaction-context

drizzle-transaction-context

JSON →
library0.2.7jsnpmunverified

Provides implicit and execution-scoped transactions/savepoints for drizzle-orm using Node.js async_hooks. Current stable version is 0.2.7. Release cadence is irregular; updates depend on community contributions. Key differentiator: avoids blocking transactions across functions/files/services, supports decorator-based AOP (@Transactional/@SavePoint), and includes a safe mode to catch common mistakes. Requires drizzle-orm ^0.44.7 and Node.js >=18. Works in Bun/Deno as well.

npm install drizzle-transaction-context
INSTALL
IMPORT
SIG · DRIZZLE-TRANSACTIO
D
drizzle-transaction-context
databasejavascriptv0.2.7
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createTransactionContext
import { createTransactionContext } from 'drizzle-transaction-context'
const createTransactionContext = require('drizzle-transaction-context')
ESM-only package; does not support CommonJS require.
Transactional
const { Transactional } = createTransactionContext(db); // Then use as decorator: @Transactional
import { Transactional } from 'drizzle-transaction-context'
Transactional is returned by createTransactionContext factory, not a direct named export from the package.
withTransaction
const { withTransaction } = createTransactionContext(db); await withTransaction(async () => { ... });
import { withTransaction } from 'drizzle-transaction-context'
withTransaction is part of the context object returned by createTransactionContext.

Demonstrates setup of drizzle-transaction-context with Bun SQLite, creating a transaction context, and using useTransaction inside withTransaction to ensure both inserts are atomic.

import { createTransactionContext } from 'drizzle-transaction-context'; import { Database } from 'bun:sqlite'; import { drizzle } from 'drizzle-orm/bun-sqlite'; import { customers } from './schema'; // assume schema defined const sqlite = new Database(':memory:'); const db = drizzle({ client: sqlite, schema }); const { useTransaction, withTransaction } = createTransactionContext(db); async function addCustomer(name: string) { const tx = useTransaction(); await tx.insert(customers).values({ name }); } await withTransaction(async () => { await addCustomer('Alice'); await addCustomer('Bob'); }); console.log('Transaction committed successfully.');
Debug
Known issues
gotchacreateTransactionContext requires passing a drizzle instance that already has the schema attached. If you omit schema, transactions will fail with 'Schema not found'.
fix
Ensure you pass a drizzle instance created with schema: drizzle({ client, schema }).
affects: >=0.1.0
gotchaThe package uses node:async_hooks. On Node.js <24, performance may be degraded. Bun and Deno have their own async_hooks implementations.
fix
Upgrade to Node.js 24+ or use Bun/Deno for better performance.
affects: >=0.2.0
gotchauseTransaction() must be called within an active withTransaction callback; otherwise it throws 'No active transaction'.
fix
Wrap calls to useTransaction() inside withTransaction() or a decorated method.
affects: >=0.1.0
deprecatedThe old pattern of passing db directly to createTransactionContext without schema is deprecated and will be removed in v1.0.
fix
Pass a drizzle instance that includes the schema property.
affects: >=0.2.0
Errors
Common errors & fixes
Error: No active transaction
useTransaction() called outside of any withTransaction scope.
fix
Ensure useTransaction is called inside a callback passed to withTransaction, or inside a method decorated with @Transactional.
TypeError: Cannot read properties of undefined (reading 'insert')
The drizzle instance passed to createTransactionContext does not have schema attached, so the table reference is undefined.
fix
Create the drizzle instance with schema: drizzle({ client, schema }) before passing to createTransactionContext.
Upgrade
Version history
0.2.7latest on npm
Audit
Dependencies
drizzle-ormrequiredpeer dependency; required to define database schema and perform operations within transaction context
Agent activity
5 hits · last 30 days
node
4
Resources
drizzle-transaction-context — npm install drizzle-transaction-context · libregistry