Gathering detailed insights and metrics for typeorm-test-transactions
Gathering detailed insights and metrics for typeorm-test-transactions
Gathering detailed insights and metrics for typeorm-test-transactions
Gathering detailed insights and metrics for typeorm-test-transactions
typeorm
Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.
typeorm-transactional
A Transactional Method Decorator for typeorm that uses cls-hooked to handle and propagate transactions between different repositories and service methods. Inpired by Spring Trasnactional Annotation and Sequelize CLS
allan-typeorm-test-transactions
A transactional wrapper for tests that use TypeORM that automatically rolls back the transaction at the end of the test.
@n8n/typeorm
Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.
npm install typeorm-test-transactions
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
63 Stars
234 Commits
5 Forks
2 Watching
4 Branches
2 Contributors
Updated on 04 Nov 2024
TypeScript (90.43%)
Shell (6.42%)
JavaScript (2.18%)
Dockerfile (0.96%)
Cumulative downloads
Total Downloads
Last day
-26.2%
211
Compared to previous day
Last week
69.2%
3,598
Compared to previous week
Last month
118.7%
9,717
Compared to previous month
Last year
-14.1%
118,410
Compared to previous year
Have you wanted to run tests on a project that uses TypeORM directly on the database and in parallel? A lot of the time we can't do this because artefacts and data from other tests can affect the result of our current tests. What usually happens, in this case, is that our tests become quite complicated when database entities are involved because we need to track exact entities. Our count()
and other aggregations must have where
clauses so that we don't see results from other tests that have already completed.
This library introduces a way to wrap tests in a transaction and automatically roll back the commits when the test ends. By doing this, you are able to run multiple tests concurrently and their data will not be seen by others.
You may argue that we should mock out the entities and not use a database at all. This is a valid point, but sometimes we want to test database constraints and the effects they can have on application logic.
Before I start, I did not add much to get this to work. The major reason why this is possible is because of the work done by odavid in his typeorm-transactional-cls-hooked library. Thanks for the great work odavid!
It has come to my attention that this library is not consistent if you use the TypeORM
entity manager. So you'll want to use typeorm-transactional-cls-hooked
to add the @Transactional
decorator above function calls and those transactions will work correctly. What a colleague has found is that, when you use a TypeORM
entity manager, they are independent of each other and data may still write to the database.
Currently, I have created a small test suite that tests that there is a rollback in the transaction wrapper. The goal is to test as many database versions as possible, currently I have:
The test badge represents tests that run over these databases:
In order to use this project you need to install the package,
1npm install --save typeorm-test-transactions 2 3# Not removing from the typeorm-transactional-cls-hooked 4# dependency separation. If you don't have the below 5# libraries then you'll need to install them as well 6# See https://github.com/odavid/typeorm-transactional-cls-hooked 7 8npm install --save typeorm reflect-metadata
When running your tests (I'm using jest
and nestjs
as the example), you'll want to wrap your test functions in the runInTransaction
function.
1import { 2 runInTransaction, 3 initialiseTestTransactions, 4} from 'typeorm-test-transactions'; 5import { DatabaseModule } from '@modules/database/database.module'; 6import { Test } from '@nestjs/testing'; 7 8initialiseTestTransactions(); 9 10describe('Feature1Test', () => { 11 beforeEach(async () => { 12 const module = await Test.createTestingModule({ 13 imports: [DatabaseModule], 14 }).compile(); 15 }); 16 17 describe('creation of 2 users', () => { 18 it( 19 'should allow me to create multiple users if the email address is different but name is the same', 20 runInTransaction(async () => { 21 await User.create({ 22 email: 'email1@test.com', 23 name: 'Name', 24 }).save(); 25 26 await User.create({ 27 email: 'email2@test.com', 28 name: 'Name', 29 }).save(); 30 31 expect(await User.count()).toEqual(2); 32 }), 33 ); 34 }); 35 36 describe('creation of one of the users in previous step', () => { 37 it( 38 'should allow me to create a user that is the same as the one in the previous step', 39 runInTransaction(async () => { 40 await User.create({ 41 email: 'email1@test.com', 42 name: 'Name', 43 }).save(); 44 45 expect(await User.count()).toEqual(1); 46 }), 47 ); 48 }); 49});
IMPORTANT! The example above wraps the entire test in a transaction and returns a function that jest
executes. If you'd like to wrap parts of a test in a transaction, you need to call the function and await
the result:
1import { 2 runInTransaction, 3 initialiseTestTransactions, 4} from 'typeorm-test-transactions'; 5import { DatabaseModule } from '@modules/database/database.module'; 6import { Test } from '@nestjs/testing'; 7 8initialiseTestTransactions(); 9 10describe('Feature1Test', () => { 11 beforeEach(async () => { 12 const module = await Test.createTestingModule({ 13 imports: [DatabaseModule], 14 }).compile(); 15 }); 16 17 describe('creation of the same user in different sequential transactions', () => { 18 it( 19 'allows me to create the same user twice if they were each in a transaction', 20 async () => { 21 await runInTransaction(async () => { 22 await User.create({ 23 email: 'email1@test.com', 24 name: 'Name', 25 }).save(); 26 27 expect(await User.count()).toEqual(1); 28 })(); 29 await runInTransaction(async () => { 30 await User.create({ 31 email: 'email1@test.com', 32 name: 'Name', 33 }).save(); 34 35 expect(await User.count()).toEqual(1); 36 })(); 37 } 38 ); 39 }); 40});
typeorm
connection was started before this package was initialised.No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
15 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More