{"id":45897725,"url":"https://github.com/chax-at/transactional-prisma-testing","last_synced_at":"2026-02-27T21:35:40.439Z","repository":{"id":57681702,"uuid":"487670059","full_name":"chax-at/transactional-prisma-testing","owner":"chax-at","description":"Provides an easy way to execute database tests in a transaction that will be rolled back after each test for fast testing","archived":false,"fork":false,"pushed_at":"2025-11-17T11:32:02.000Z","size":96,"stargazers_count":51,"open_issues_count":2,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2026-01-04T11:26:23.607Z","etag":null,"topics":["database","fast","postgres","postgresql","prisma","testing","transaction"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chax-at.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-05-02T00:02:07.000Z","updated_at":"2025-11-20T10:24:43.000Z","dependencies_parsed_at":"2024-05-18T00:27:06.716Z","dependency_job_id":"2acd85d9-556a-4afa-a51d-016ba4eeb512","html_url":"https://github.com/chax-at/transactional-prisma-testing","commit_stats":{"total_commits":16,"total_committers":1,"mean_commits":16.0,"dds":0.0,"last_synced_commit":"0f1a7c0095ceff2385dec7985e2aefed11073d46"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/chax-at/transactional-prisma-testing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chax-at%2Ftransactional-prisma-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chax-at%2Ftransactional-prisma-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chax-at%2Ftransactional-prisma-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chax-at%2Ftransactional-prisma-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chax-at","download_url":"https://codeload.github.com/chax-at/transactional-prisma-testing/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chax-at%2Ftransactional-prisma-testing/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29915345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T19:37:42.220Z","status":"ssl_error","status_checked_at":"2026-02-27T19:37:41.463Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","fast","postgres","postgresql","prisma","testing","transaction"],"created_at":"2026-02-27T21:35:39.801Z","updated_at":"2026-02-27T21:35:40.435Z","avatar_url":"https://github.com/chax-at.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @chax-at/transactional-prisma-testing\nThis package provides an easy way to run test cases inside a single database transaction that will\nbe rolled back after each test. This allows fast test execution while still providing the same source database state for each test.\nIt also allows parallel test execution against the same database.\n\n## Prerequisites\n* You are using \u003ca href=\"https://github.com/prisma/prisma\"\u003ePrisma\u003c/a\u003e 4.7.0 or later in your project.\n* You are using PostgreSQL (other DBs might work but have not been tested).\n* You are not using [Fluent API](https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#fluent-api).\n\n\n## Usage\nInstall the package by running\n```shell\nnpm i -D @chax-at/transactional-prisma-testing\n```\nNote that this will install the package as a dev dependency, intended to be used during tests only (remove the `-D` if you want to use it outside of tests).\n\n### Example\nThe following example for a \u003ca href=\"https://github.com/nestjs/nest\"\u003eNestJS\u003c/a\u003e project shows how this package can be used.\nIt is however possible to use this package with every testing framework, just check out the documentation below and adapt the code accordingly. \nYou can simply replace the `PrismaService` with `PrismaClient` if you are not using NestJS.\n```typescript\nimport { PrismaTestingHelper } from '@chax-at/transactional-prisma-testing';\n\n// Cache for the PrismaTestingHelper. Only one PrismaTestingHelper should be instantiated per test runner (i.e. only one if your tests run sequentially).\nlet prismaTestingHelper: PrismaTestingHelper\u003cPrismaService\u003e | undefined;\n// Saves the PrismaService that will be used during test cases. Will always execute queries on the currently active transaction.\nlet prismaService: PrismaService;\n\n// This function must be called before every test\nasync function before(): Promise\u003cvoid\u003e {\n  if(prismaTestingHelper == null) {\n    // Initialize testing helper if it has not been initialized before\n    const originalPrismaService = new PrismaService(); // in newer prisma versions, you may have to pass an adapter here\n    // Seed your database / Create source database state that will be used in each test case (if needed)\n    // ...\n    prismaTestingHelper = new PrismaTestingHelper(originalPrismaService);\n    // Save prismaService. All calls to this prismaService will be routed to the currently active transaction\n    prismaService = prismaTestingHelper.getProxyClient();\n  }\n\n  await prismaTestingHelper.startNewTransaction();\n}\n\n// This function must be called after every test\nfunction after(): void {\n  prismaTestingHelper?.rollbackCurrentTransaction();\n}\n\n// NestJS specific code: Replace the original PrismaService when creating a testing module\n// Note that it is possible to cache this result and use the same module for all tests. The prismaService will automatically route all calls to the currently active transaction\nfunction getMockRootModuleBuilder(): TestingModuleBuilder {\n  return Test.createTestingModule({\n    imports: [AppModule],\n  }).overrideProvider(PrismaService)\n    .useValue(prismaService);\n}\n```\n\n### PrismaTestingHelper\nThe `PrismaTestingHelper` provides a proxy to the prisma client and manages this proxy.\n\n#### constructor(prismaClient: T)\nCreate a single `PrismaTestingHelper` per test runner (or a single global one if tests are executed sequentially).\nThe constructor parameter is the original `PrismaClient` that will be used to start transaction.\nNote that it is possible to use any objects that extend the PrismaClient, e.g. a \u003ca href=\"https://docs.nestjs.com/recipes/prisma#use-prisma-client-in-your-nestjs-services\"\u003eNestJS PrismaService\u003c/a\u003e.\nAll methods that don't exist on the prisma transaction client will be routed to this original object (except for `$transaction` calls).\n\n#### getProxyClient(): T\nThis method returns a \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy\"\u003eProxy\u003c/a\u003e\nto the PrismaClient that will execute all calls inside a transaction.\nYou can save and cache this reference, all calls will always be executed inside the newest transaction.\nThis allows you to e.g. start your application once with the ProxyClient instead of the normal client\nand then execute all test cases, and all calls will always be routed to the newest transaction.\n\nIt is usually enough to fetch this once and then use this reference everywhere.\n\n#### startNewTransaction(opts?: { timeout?: number; maxWait?: number}): Promise\\\u003cvoid\\\u003e\nStarts a new transaction. Must be called before each test (and should be called before any query on the proxy client is executed).\nYou can provide timeout values - note that the transaction `timeout` must be long enough so that your\nwhole test case will be executed during this timeout.\n\nYou must call `rollbackCurrentTransaction` before calling this method again.\n\n#### rollbackCurrentTransaction(): void\nEnds the currently active transaction. Must be called after each test so that a new transaction can be started.\n\n## Limitations / Caveats\n* [Fluent API](https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#fluent-api) is not supported.\n* Sequences (auto increment IDs) are not reset when transaction are rolled back. If you need specific IDs in your tests, you can \n  \u003ca href=\"https://stackoverflow.com/a/41108598\"\u003ereset all sequences by using SETVAL\u003c/a\u003e before each test.\n* `@default(now())` in your schema (e.g. for a `createdAt` date) or similar functionality (e.g. `CURRENT_TIMESTAMP()` in PostgreSQL) will always use the **start of transaction** timestamp. Therefore, all `createdAt`-timestamps will have the same value during a test (because they are executed in the same transaction). If this behavior is problematic (e.g. because you want to find the latest entry by creation date), then you can use `@default(dbgenerated(\"statement_timestamp()\"))` instead of `@default(now())` (if you do not rely on the default \"`createdAt` = time at start of transaction instead of statement\" behavior)\n* Transactions in test cases are completely supported by using \u003ca href=\"https://www.postgresql.org/docs/current/sql-savepoint.html\"\u003ePostgreSQL Savepoints\u003c/a\u003e.\n  * If you are using parallel transactions (e.g. `await Promise.all(/* Multiple calls that will each start transactions */);`), then they will\n    automatically be executed sequentially (otherwise, Savepoints wouldn't work). You do not have to change your code for this to work.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchax-at%2Ftransactional-prisma-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchax-at%2Ftransactional-prisma-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchax-at%2Ftransactional-prisma-testing/lists"}