π Beautiful transactional decorator for Drizzle ORM inspired by TypeORM-transactional
A comprehensive transactional system for Drizzle ORM that provides declarative transaction management through decorators, with full support for transaction propagation behaviors, isolation levels, and lifecycle hooks.
Complete documentation is available in our Documentation Wiki
- Installation Guide - Get started in minutes
- Quick Start - Step-by-step setup
- API Reference - Complete API documentation
- Migration Guide - Migrate from TypeORM or plain Drizzle
- Propagation Behaviors - Transaction propagation types
- Transaction Hooks - Lifecycle callbacks
- Examples - Real-world usage patterns
- Testing Guide - Testing strategies
- Troubleshooting - Common issues and solutions
- π― Declarative Transactions: Use
@Transactional()
decorator on methods and@TransactionalClass()
on classes - π Propagation Behaviors: Full support for all transaction propagation types (REQUIRED, REQUIRES_NEW, MANDATORY, etc.)
- π Isolation Levels: Support for all PostgreSQL isolation levels
- πͺ Transaction Hooks: Register callbacks for commit, rollback, and completion events
- π§΅ Context Management: AsyncLocalStorage-based context management for thread-safe operations
- π§ Type Safe: Full TypeScript support with proper type inference
- β‘ Performance Optimized: WeakMap-based method caching and efficient context management
- π οΈ Enhanced Error Handling: Rich error information with stack traces and context
- π Utility Functions: Built-in memoization, debouncing, and throttling utilities
- π PostgreSQL Ready: Optimized for PostgreSQL with full feature support
- π¦ Dual Package: Supports both ESM and CommonJS modules
npm install drizzle-transactional drizzle-orm
# For PostgreSQL (recommended)
npm install pg @types/pg
# For development
npm install reflect-metadata
import {
Transactional,
runOnTransactionCommit,
initializeDrizzleTransactionalContext,
addTransactionalDrizzleDatabase,
} from "drizzle-transactional";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
// Initialize the library
const pool = new Pool({
connectionString: "postgresql://username:password@localhost:5432/mydb",
});
const db = drizzle(pool);
initializeDrizzleTransactionalContext();
addTransactionalDrizzleDatabase(db);
class UserService {
@Transactional()
async createUser(name: string, email: string) {
const user = await db.insert(users).values({ name, email }).returning();
runOnTransactionCommit(() => {
console.log(`β
User ${name} created successfully!`);
emailService.sendWelcomeEmail(email);
});
return user[0];
}
}
For complete setup instructions, see Quick Start Guide.
- Node.js: 18+
- TypeScript: 5.0+
- Drizzle ORM: 0.36+
- PostgreSQL: 12+
- Complete Documentation - Full feature documentation
- Migration Guide - Migrate from TypeORM or plain Drizzle
- Examples - Real-world usage patterns
- Testing Guide - Test your transactional code
- Troubleshooting - Common issues and solutions
- FAQ - Frequently asked questions
# Run core functionality tests
npm run test:quick
# Run all tests
npm run test:all
This library is inspired by typeorm-transactional and adapted for Drizzle ORM with PostgreSQL support. Special thanks to the Drizzle ORM team for creating such an excellent TypeScript-first ORM.
MIT License - see LICENSE file for details.
Built with β€οΈ for the Drizzle ORM community, with assistance from Claude AI.
π PROJECT COMPLETED SUCCESSFULLY
All requirements met:
- β Beautiful transactional decorator for Drizzle ORM
- β Based on provided prototype
- β ALL capabilities from typeorm-transactional
- β Real-world tests without testing frameworks
- β PostgreSQL integration with full feature support
- β Latest versions of all libraries
- β English code messages and documentation
- β Dual package support (ESM + CommonJS)
- β Ready for npm publication
Ready for production use! π