Registry / database / sequelize-transactional-decorator

sequelize-transactional-decorator

JSON →
library1.0.1jsnpmunverified

A @Transactional method decorator for Sequelize (v4-6) inspired by Spring's @Transactional annotation. Version 1.0.1 is the latest stable; released 2021. Provides declarative transaction management, various propagation behaviors (REQUIRED, REQUIRES_NEW, etc.), isolation levels, and easy NestJS integration via SequelizeTransactionalModule. Combines multiple service calls under one transaction using CLS (continuation-local-storage).

npm install sequelize-transactional-decorator
INSTALL
IMPORT
SIG · SEQUELIZE-TRANSACT
S
sequelize-transactional-decorator
databasejavascriptv1.0.1
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.

Transactional
✓ import { Transactional } from 'sequelize-transactional-decorator'
Default import does not exist; always named import.
initSequelizeCLS
✓ import { initSequelizeCLS } from 'sequelize-transactional-decorator'
initSequelizeTransactional
✓ import { initSequelizeTransactional } from 'sequelize-transactional-decorator'
Use initSequelizeTransactional for non-NestJS setups; pass your Sequelize instance.
SequelizeTransactionalModule
✓ import { SequelizeTransactionalModule } from 'sequelize-transactional-decorator'
Only available for NestJS; use .register() in module imports.

Initialize CLS, define a Sequelize model, and use @Transactional decorator on a method to wrap database operations in a transaction.

import { initSequelizeCLS, Transactional } from 'sequelize-transactional-decorator'; import { Sequelize, Model, DataTypes } from 'sequelize'; initSequelizeCLS(); const sequelize = new Sequelize('sqlite::memory:', { logging: false }); const User = sequelize.define('User', { name: DataTypes.STRING }); class UserService { @Transactional() async createUser(name: string) { await User.create({ name }); } } const service = new UserService(); await service.createUser('Alice'); console.log(await User.findAll()); // Output: [ { id: 1, name: 'Alice', ... } ]
Debug
Known issues
gotchaCLS must be initialized before any Sequelize connection is established. Failure to do so will cause transactions to not work properly.
fix
Call initSequelizeCLS() at the very beginning of your application, before creating any Sequelize instances.
affects: >=1.0.0
gotchaThe decorator relies on async_hooks via cls-hooked; it may not work in older Node.js versions (pre-8.2).
fix
Use Node.js >= 8.2 or polyfill async_hooks.
affects: >=1.0.0
deprecatedSequelize v6 is partially supported; some users report issues with transaction nesting.
fix
Consider using sequelize's built-in transaction support or test thoroughly with Sequelize v6.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: cls_hooked is not a constructor
Missing or incompatible cls-hooked module
fix
Install a compatible version: npm install cls-hooked@2.0.1
Error: CLS context not available. Make sure to call initSequelizeCLS() before creating any Sequelize instances.
initSequelizeCLS() was not called or called after connection
fix
Ensure initSequelizeCLS() is called at the top of your entry file, before any Sequelize operations.
SequelizeDatabaseError: SQLITE_ERROR: no such transaction
Transaction decorator used without proper CLS context (e.g., in a test environment)
fix
Mock the @Transactional decorator in tests or ensure CLS is initialized.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
sequelizerequiredPeer dependency; required to use the decorator
cls-hookedrequiredUsed internally for CLS context
Agent activity
9 hits · last 30 days
node
8
Resources
sequelize-transactional-decorator — npm install sequelize-transactional-decorator · libregistry