Gathering detailed insights and metrics for nestjs-typeorm-transactions-test
Gathering detailed insights and metrics for nestjs-typeorm-transactions-test
Gathering detailed insights and metrics for nestjs-typeorm-transactions-test
Gathering detailed insights and metrics for nestjs-typeorm-transactions-test
A NestJS module to make transaction management easier across different services
npm install nestjs-typeorm-transactions-test
Typescript
Module System
Node Version
NPM Version
TypeScript (96.84%)
JavaScript (3.16%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
4 Stars
28 Commits
1 Forks
1 Watchers
1 Branches
2 Contributors
Updated on May 23, 2025
Latest Version
0.0.34
Package Id
nestjs-typeorm-transactions-test@0.0.34
Unpacked Size
202.81 kB
Size
63.11 kB
File Count
45
NPM Version
9.5.1
Node Version
18.16.0
Published on
Nov 12, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
3
22
A NestJS module to make TypeORM transaction management easier across different services.
This package can be used to reduce the boilerplate code needed to manage TypeORM transactions. It acts as a wrapper around the actual TypeORM package that enables effortless transaction support. The package utilizes async local storage in order to share transactional entity manager accross different service method calls so that transactions across multiple services are handled behind the scenes and abstracted away from developers.
Note: In the following code snippets, only the imports relvant to this package are shown
You should ideally import TypeOrmTransactionModule.forRoot()
in your app.module.ts
file and the configuration options are exactly the same as @nestjs/typeorm
package.
Add the following to the imports array of your module:
1import { TypeOrmTransactionModule } from 'nestjs-typeorm-transactions';
2
3@Module({
4 imports: [
5 TypeOrmTransactionModule.forRoot({
6 type: 'mysql', // or postgres, sqlite etc
7 host: 'localhost',
8 username: 'username',
9 password: 'password',
10 database: 'test',
11 entities: [User], // list of entities
12 synchronize: true,
13 logging: true,
14 }),
15 ],
16})
17export class AppModule {}
For a repository to be avilable in the context of a sub module, we need to import TypeOrmTransactionModule.forFeature()
in that module's import array. Just like @nestjs/typeorm
package, we can pass an array of entities whose repositories will be available in this module.
1import { TypeOrmTransactionModule } from 'nestjs-typeorm-transactions'; 2 3@Module({ 4 imports: [TypeOrmTransactionModule.forFeature([User])], 5 controllers: [UsersController], 6 providers: [UsersService], 7}) 8export class UsersModule {}
The way repositories are injected are almost the same as @nestjs/typeorm
package. Only difference is that you need to use @InjectTransactionalRepository
. The entity for which repository will be injected should be povided to this decorator as well.
1import { 2 InjectTransactionalRepository, 3 TransactionalRepository, 4} from 'nestjs-typeorm-transactions'; 5 6class UsersService { 7 constructor( 8 @InjectTransactionalRepository(User) 9 private userRepository: TransactionalRepository<User>, 10 ) {} 11 12 async doSomethingWithUser() { 13 // ... 14 } 15}
As seen above, the type of injected repository is TransactionalRepository
. By default, the queries are NOT wrapped inside a transaction even if you inject TransactionalRepository into your service class. In order to run queries in a transaction, @Transactional
decorator must be used on the route handler that is calling the service method. Here's an example:
1import { Transactional } from 'nestjs-typeorm-transactions'; 2 3@Controller('users') 4export class UsersController { 5 constructor(private usersService: UsersService) {} 6 7 @Post('with-transaction') 8 @Transactional() 9 async withTransaction() { 10 await this.usersService.doSomethingWithUser(); 11 } 12 13 @Post('without-transaction') 14 async withTransaction() { 15 await this.usersService.doSomethingWithUser(); 16 } 17}
If a request hits the endpoint /users/with-transcation
, any database query executed by doSomethingWithUser
or any other service method that
doSomethingWithUser
method calls, all of these queries will be wrapped in a transaction as we have used @Transactional
decorator.
However, if a request hits the other endpoint /users/without-transcation
, no transaction will be created. So it's crucial to remember to add @Transactional
decorator on route handlers where we need atomicity.
Connecting to multiple databases is supported by @nestjs/typeorm
package and it's also supported by this package. In order to accomplish this, TypeOrmTransactionModule.forRoot()
should be imported multiple times as follows:
1import { TypeOrmTransactionModule } from 'nestjs-typeorm-transactions';
2
3@Module({
4 imports: [
5 TypeOrmTransactionModule.forRoot({
6 type: 'mysql',
7 host: 'mysql_host',
8 username: 'mysql_username',
9 password: 'mysql_password',
10 database: 'test_mysql',
11 entities: [User],
12 synchronize: true,
13 logging: true,
14 }),
15 TypeOrmTransactionModule.forRoot({
16 name: 'second_db', // name for the postgresql db connection
17 type: 'postgres',
18 host: 'postgres_host',
19 username: 'postgres_username',
20 password: 'postgres_password',
21 database: 'test_postgres',
22 entities: [Article],
23 synchronize: true,
24 logging: true,
25 }),
26 ],
27})
28export class AppModule {}
In this case, we have two database connections. The first one is the default one and it's the mysql database. The second connection is for a postgresql database and it's used to store the articles. The name specified for the postgresql database is second_db
and it will be used later on.
In order to make repositories for both of these connections available in the users module's context, we import them both as follows:
1import { TypeOrmTransactionModule } from 'nestjs-typeorm-transactions'; 2 3@Module({ 4 imports: [ 5 TypeOrmTransactionModule.forFeature([User]), // will use the default connection (mysql) 6 TypeOrmTransactionModule.forFeature([Article], 'second_db'), // will use postgresql connection 7 ], 8 controllers: [UsersController], 9 providers: [UsersService], 10}) 11export class UsersModule {}
How we inject repositories remains exactly the same:
1import { 2 InjectTransactionalRepository, 3 TransactionalRepository, 4} from 'nestjs-typeorm-transactions'; 5 6class UsersService { 7 constructor( 8 @InjectTransactionalRepository(User) 9 private userRepository: TransactionalRepository<User>, 10 @InjectTransactionalRepository(Article) 11 private userRepository: TransactionalRepository<Article>, 12 ) {} 13 14 async doSomethingWithUser() { 15 // ... 16 } 17 18 async doSomethingWithArticle() { 19 // ... 20 } 21}
And lastly, for transactional query execution, we need to use @Transactional
decorator as follows:
1import { Transactional } from 'nestjs-typeorm-transactions'; 2 3@Controller('users') 4export class UsersController { 5 constructor(private usersService: UsersService) {} 6 7 @Post() 8 @Transactional() 9 async withTransaction() { 10 await this.usersService.doSomethingWithUser(); 11 } 12 13 @Post('/articles') 14 @Transactional('second_db') 15 async withTransaction() { 16 await this.usersService.doSomethingWithArticle(); 17 } 18}
When @Transactional
decorator is added without any argument, it will wrap all database queries executed by the default connection in a transaction (mysql connection in this case). However, the second decorator has the argument second_db
which means in the route /users/articles
, only the database queries that are sent to the postgres database will be in a transaction.
No vulnerabilities found.
No security vulnerabilities found.