@nathapp/nestjs-typeorm 4.3.1 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,6 +24,42 @@ repositories, `BaseSpRepository.callQuery`, `@Transactional`, and
24
24
  DataSource. Each connection has an independent context, including during nested
25
25
  and concurrent work.
26
26
 
27
+ Each context is an `AsyncLocalStorage` that lives as long as its DataSource is
28
+ referenced. This package requires Node.js 24 or later, where that has no
29
+ per-promise cost however many applications a process boots and closes. Do not run
30
+ with `--no-async-context-frame`, which brings the cost back.
31
+
32
+ ### Independent transactions (`requiresNew`)
33
+
34
+ By default `run` joins an active transaction. `await tx.run(fn, { propagation: 'requiresNew' })`
35
+ instead always starts a new transaction on its own QueryRunner. That runner is the
36
+ active one only inside `fn`; the outer transaction resumes afterwards, and a plain
37
+ `run` nested in `fn` joins the new one. Use it for writes that must persist even if
38
+ the caller later rolls back (security revocations, audit records, lockout counters):
39
+
40
+ - The inner commit survives an outer rollback.
41
+ - An error thrown inside it rolls back only the inner transaction; the outer
42
+ transaction can still commit, and the error still reaches the caller of `run`.
43
+ - `isolationLevel` applies to the new transaction. `getClient()` and
44
+ `isInTransaction()` report the inner runner inside `fn` and the outer one after.
45
+
46
+ Caveats:
47
+
48
+ - It needs a second pooled connection while the outer transaction still holds one.
49
+ On single-connection setups (a pool of size one, SQLite drivers that serialize
50
+ transactions) it deadlocks and is unsupported. The TypeORM manager does not try to
51
+ detect this, so size the pool for at least two connections.
52
+ - Each concurrent outer transaction using `requiresNew` holds two connections. Size
53
+ the pool for at least 2x the peak number of concurrent outer transactions.
54
+ TypeORM's default pools can wait forever, so set a pool acquire timeout in the
55
+ driver options (for example `connectionTimeoutMillis` in `extra` for Postgres)
56
+ so starvation fails instead of hanging.
57
+ - An unknown `propagation` value (for example `'requires_new'`) throws instead of
58
+ silently joining.
59
+ - If the outer transaction holds row locks on rows the inner one writes, the inner
60
+ transaction waits on its own caller until the lock timeout. Use it only for rows
61
+ the outer transaction has not locked.
62
+
27
63
  ## Stored procedure results
28
64
 
29
65
  `BaseSpRepository.callQuery` preserves the driver result shape: PostgreSQL's
@@ -26,7 +26,11 @@ let TypeOrmTransactionManager = class TypeOrmTransactionManager {
26
26
  this.als = (0, typeorm_transaction_context_1.registerTypeOrmTransactionContext)(dataSource, als !== null && als !== void 0 ? als : new async_hooks_1.AsyncLocalStorage());
27
27
  }
28
28
  async run(fn, options) {
29
- if (this.als.getStore()) {
29
+ const propagation = options === null || options === void 0 ? void 0 : options.propagation;
30
+ if (propagation !== undefined && propagation !== 'required' && propagation !== 'requiresNew') {
31
+ throw new Error(`Unknown transaction propagation ${JSON.stringify(propagation)}: expected 'required' or 'requiresNew'`);
32
+ }
33
+ if (this.als.getStore() && (options === null || options === void 0 ? void 0 : options.propagation) !== 'requiresNew') {
30
34
  return fn();
31
35
  }
32
36
  const isolationLevel = (options === null || options === void 0 ? void 0 : options.isolationLevel)
@@ -1 +1 @@
1
- {"version":3,"file":"typeorm-transaction-manager.js","sourceRoot":"","sources":["../../lib/transaction/typeorm-transaction-manager.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAC5C,qCAAkD;AAClD,6CAAgD;AAEhD,+EAGuC;AAiBvC,MAAM,wBAAwB,GAG1B;IACF,eAAe,EAAE,kBAAkB;IACnC,aAAa,EAAE,gBAAgB;IAC/B,cAAc,EAAE,iBAAiB;IACjC,YAAY,EAAE,cAAc;CAC7B,CAAC;AA+CK,IAAM,yBAAyB,GAA/B,MAAM,yBAAyB;IAGpC,YACmB,UAAsB,EACvC,GAAoC;QADnB,eAAU,GAAV,UAAU,CAAY;QAGvC,IAAI,CAAC,GAAG,GAAG,IAAA,+DAAiC,EAC1C,UAAU,EACV,GAAG,aAAH,GAAG,cAAH,GAAG,GAAI,IAAI,+BAAiB,EAAe,CAC5C,CAAC;IACJ,CAAC;IAcD,KAAK,CAAC,GAAG,CAAI,EAAoB,EAAE,OA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C,OAAO,YAAY,EAAE,CAAC;YACtB,IAAI,CAAC,iBAAiB,EAAE,CAAC;gBACvB,iBAAiB,GAAG,IAAI,CAAC;gBACzB,gBAAgB,GAAG,YAAY,CAAC;YAClC,CAAC;QACH,CAAC;QAED,IAAI,iBAAiB,EAAE,CAAC;YACtB,MAAM,gBAAgB,CAAC;QACzB,CAAC;QACD,OAAO,MAAO,CAAC;IACjB,CAAC;IAMD,SAAS;;QACP,OAAO,CAAC,MAAA,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,mCAAI,IAAI,CAAC,UAAU,CAAM,CAAC;IACvD,CAAC;IAKD,eAAe;QACb,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,SAAS,CAAC;IAC3C,CAAC;CACF,CAAA;AA/FY,8DAAyB;oCAAzB,yBAAyB;IADrC,IAAA,mBAAU,GAAE;qCAKoB,oBAAU;QACjC,+BAAiB;GALd,yBAAyB,CA+FrC"}
1
+ {"version":3,"file":"typeorm-transaction-manager.js","sourceRoot":"","sources":["../../lib/transaction/typeorm-transaction-manager.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAC5C,qCAAkD;AAClD,6CAAgD;AAEhD,+EAGuC;AAiBvC,MAAM,wBAAwB,GAG1B;IACF,eAAe,EAAE,kBAAkB;IACnC,aAAa,EAAE,gBAAgB;IAC/B,cAAc,EAAE,iBAAiB;IACjC,YAAY,EAAE,cAAc;CAC7B,CAAC;AA+CK,IAAM,yBAAyB,GAA/B,MAAM,yBAAyB;IAGpC,YACmB,UAAsB,EACvC,GAAoC;QADnB,eAAU,GAAV,UAAU,CAAY;QAGvC,IAAI,CAAC,GAAG,GAAG,IAAA,+DAAiC,EAC1C,UAAU,EACV,GAAG,aAAH,GAAG,cAAH,GAAG,GAAI,IAAI,+BAAiB,EAAe,CAC5C,CAAC;IACJ,CAAC;IAiBD,KAAK,CAAC,GAAG,CAAI,EAAoB,EAAE,OAA+B;QAChE,MAAM,WAAW,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iBAAiB,EAAE,CAAC;QACxC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,iBAAiB,GAAG,IAAI,CAAC;YACzB,gBAAgB,GAAG,KAAK,CAAC;YACzB,IAAI,CAAC;gBACH,MAAM,WAAW,CAAC,mBAAmB,EAAE,CAAC;YAC1C,CAAC;YAAC,WAAM,CAAC;YAET,CAAC;QACH,CAAC;QAED,IAAI,CAAC;YACH,MAAM,WAAW,CAAC,OAAO,EAAE,CAAC;QAC9B,CAAC;QAAC,OAAO,YAAY,EAAE,CAAC;YACtB,IAAI,CAAC,iBAAiB,EAAE,CAAC;gBACvB,iBAAiB,GAAG,IAAI,CAAC;gBACzB,gBAAgB,GAAG,YAAY,CAAC;YAClC,CAAC;QACH,CAAC;QAED,IAAI,iBAAiB,EAAE,CAAC;YACtB,MAAM,gBAAgB,CAAC;QACzB,CAAC;QACD,OAAO,MAAO,CAAC;IACjB,CAAC;IAMD,SAAS;;QACP,OAAO,CAAC,MAAA,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,mCAAI,IAAI,CAAC,UAAU,CAAM,CAAC;IACvD,CAAC;IAKD,eAAe;QACb,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,SAAS,CAAC;IAC3C,CAAC;CACF,CAAA;AAvGY,8DAAyB;oCAAzB,yBAAyB;IADrC,IAAA,mBAAU,GAAE;qCAKoB,oBAAU;QACjC,+BAAiB;GALd,yBAAyB,CAuGrC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nathapp/nestjs-typeorm",
3
- "version": "4.3.1",
3
+ "version": "4.4.0",
4
4
  "description": "@nathapp/nestjs-typeorm",
5
5
  "scripts": {
6
6
  "clean": "rimraf dist/ .turbo/",
@@ -17,7 +17,7 @@
17
17
  "author": "William Khoo",
18
18
  "license": "ISC",
19
19
  "engines": {
20
- "node": "^20.19.0 || >=22.12.0"
20
+ "node": ">=24.0.0"
21
21
  },
22
22
  "types": "dist/index.d.ts",
23
23
  "main": "dist/index.js",
@@ -26,8 +26,8 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@nathapp/common": "^0.1.14",
29
- "@nathapp/nestjs-common": "^4.3.1",
30
- "@nathapp/nestjs-data": "^4.3.1",
29
+ "@nathapp/nestjs-common": "^4.4.0",
30
+ "@nathapp/nestjs-data": "^4.4.0",
31
31
  "@nestjs/common": "^10.0.0 || ^11.0.0 || ^12.0.0",
32
32
  "class-transformer": "*",
33
33
  "dayjs": "^1.11.13"