Package Exports
- drizzle-transactional
Readme
Drizzle Transactional
๐ 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.
๐ Documentation
Complete documentation is available in our Documentation Wiki
๐ Quick Links
- 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
๐ Key Topics
- Propagation Behaviors - Transaction propagation types
- Transaction Hooks - Lifecycle callbacks
- Examples - Real-world usage patterns
- Testing Guide - Testing strategies
- Troubleshooting - Common issues and solutions
๐ Features
- ๐ฏ 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
๐ฆ Installation
npm install drizzle-transactional drizzle-orm
Database dependencies:
# For PostgreSQL (recommended)
npm install pg @types/pg
# For development
npm install reflect-metadata
โก Quick Example
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.
๐ค Compatibility
- Node.js: 18+
- TypeScript: 5.0+
- Drizzle ORM: 0.36+
- PostgreSQL: 12+
๐ Learn More
- 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
๐งช Testing
# Run core functionality tests
npm run test:quick
# Run all tests
npm run test:all
๐ Acknowledgments
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.
๐ License
MIT License - see LICENSE file for details.
Built with โค๏ธ for the Drizzle ORM community, with assistance from Claude AI.
๐ Project Status
๐ 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! ๐