@wisemen/nestjs-typeorm 1.2.0 → 1.2.1

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.
@@ -1,3 +1,21 @@
1
1
  import type { DataSource, EntityManager } from 'typeorm';
2
- export declare function readonly<T>(dataSource: DataSource, fn: () => Promise<T>): Promise<T>;
2
+ /**
3
+ * Runs the provided callback in a readonly database context backed by a slave query runner.
4
+ *
5
+ * If a readonly context is already active, the callback is reused within that context instead of
6
+ * creating a new query runner. Starting a readonly context from inside a transaction is rejected
7
+ * because it would break the current transactional guarantees. Within the callback, injected
8
+ * TypeORM repositories automatically resolve against the active readonly manager, so repository
9
+ * operations also use the readonly query runner without requiring manual rebinding.
10
+ *
11
+ * @template T
12
+ * @param dataSource The TypeORM data source used to create the readonly query runner.
13
+ * @param runInReadonly Async callback that receives the readonly entity manager for all database
14
+ * access. Injected TypeORM repositories used inside this callback automatically participate in the
15
+ * same readonly context.
16
+ * @returns The value returned by `runInReadonly`.
17
+ * @throws {Error} If called while a transaction context is active.
18
+ * @throws {QueryRunnerProviderAlreadyReleasedError} If the current readonly context references a released query runner.
19
+ */
20
+ export declare function readonly<T>(dataSource: DataSource, runInReadonly: (entityManager: EntityManager) => Promise<T>): Promise<T>;
3
21
  export declare function createReadonlyManagerProxy(defaultManager: EntityManager): EntityManager;
@@ -1,7 +1,25 @@
1
1
  import { QueryRunnerProviderAlreadyReleasedError } from 'typeorm';
2
2
  import { isInTransaction, readonlyStorage } from './context.js';
3
- export async function readonly(dataSource, fn) {
4
- // Disallow starting a readonly scope while in a transaction
3
+ import assert from 'node:assert';
4
+ /**
5
+ * Runs the provided callback in a readonly database context backed by a slave query runner.
6
+ *
7
+ * If a readonly context is already active, the callback is reused within that context instead of
8
+ * creating a new query runner. Starting a readonly context from inside a transaction is rejected
9
+ * because it would break the current transactional guarantees. Within the callback, injected
10
+ * TypeORM repositories automatically resolve against the active readonly manager, so repository
11
+ * operations also use the readonly query runner without requiring manual rebinding.
12
+ *
13
+ * @template T
14
+ * @param dataSource The TypeORM data source used to create the readonly query runner.
15
+ * @param runInReadonly Async callback that receives the readonly entity manager for all database
16
+ * access. Injected TypeORM repositories used inside this callback automatically participate in the
17
+ * same readonly context.
18
+ * @returns The value returned by `runInReadonly`.
19
+ * @throws {Error} If called while a transaction context is active.
20
+ * @throws {QueryRunnerProviderAlreadyReleasedError} If the current readonly context references a released query runner.
21
+ */
22
+ export async function readonly(dataSource, runInReadonly) {
5
23
  if (isInTransaction()) {
6
24
  throw new Error('Cannot start a readonly context inside a transaction');
7
25
  }
@@ -10,11 +28,13 @@ export async function readonly(dataSource, fn) {
10
28
  throw new QueryRunnerProviderAlreadyReleasedError();
11
29
  }
12
30
  if (existing != null) {
13
- return await fn();
31
+ assert(existing.queryRunner !== undefined, 'existing readonly without queryRunner');
32
+ return await runInReadonly(existing.queryRunner.manager);
14
33
  }
15
34
  const queryRunner = dataSource.createQueryRunner('slave');
16
35
  try {
17
- return await readonlyStorage.run(queryRunner.manager, fn);
36
+ const callback = async () => await runInReadonly(queryRunner.manager);
37
+ return await readonlyStorage.run(queryRunner.manager, callback);
18
38
  }
19
39
  finally {
20
40
  await queryRunner.release();
@@ -1 +1 @@
1
- {"version":3,"file":"readonly.js","sourceRoot":"","sources":["../../lib/extensions/readonly.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uCAAuC,EAAE,MAAM,SAAS,CAAA;AACjE,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAE/D,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,UAAsB,EACtB,EAAoB;IAEpB,4DAA4D;IAC5D,IAAI,eAAe,EAAE,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAA;IACzE,CAAC;IACD,MAAM,QAAQ,GAAG,eAAe,CAAC,QAAQ,EAAE,CAAA;IAE3C,IAAI,QAAQ,EAAE,WAAW,IAAI,IAAI,IA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,iEAAiE,CAAC,CAAA;oBACpF,CAAC,CAAY,CAAA;gBACf,CAAC;gBAED,OAAO,OAAO,CAAC,IAAI,CAAY,CAAA;YACjC,CAAC;YAED,OAAO,MAAM,CAAC,IAAI,CAAY,CAAA;QAChC,CAAC;KACF,CAAC,CAAA;AACJ,CAAC"}
@@ -1,3 +1,20 @@
1
1
  import type { DataSource, EntityManager } from 'typeorm';
2
+ /**
3
+ * Runs the provided callback inside a transaction and exposes the active transaction manager.
4
+ *
5
+ * If a transaction is already active, the callback is executed as a nested TypeORM transaction on
6
+ * the current manager. Starting a transaction from inside a readonly context is rejected because
7
+ * writes must not escape the readonly boundary. Within the callback, injected TypeORM repositories
8
+ * automatically resolve against the active transaction manager, so repository operations join the
9
+ * same transaction without requiring manual rebinding.
10
+ *
11
+ * @template T
12
+ * @param dataSource The TypeORM data source used to start the transaction when none is active.
13
+ * @param runInTransaction Async callback that receives the transaction-bound entity manager.
14
+ * Injected TypeORM repositories used inside this callback automatically participate in the same
15
+ * transaction context.
16
+ * @returns The value returned by `runInTransaction`.
17
+ * @throws {Error} If called while a readonly context is active.
18
+ */
2
19
  export declare function transaction<T>(dataSource: DataSource, runInTransaction: (entityManager: EntityManager) => Promise<T>): Promise<T>;
3
20
  export declare function createTransactionManagerProxy(manager: EntityManager): EntityManager;
@@ -1,4 +1,21 @@
1
1
  import { isInReadonly, transactionStorage } from './context.js';
2
+ /**
3
+ * Runs the provided callback inside a transaction and exposes the active transaction manager.
4
+ *
5
+ * If a transaction is already active, the callback is executed as a nested TypeORM transaction on
6
+ * the current manager. Starting a transaction from inside a readonly context is rejected because
7
+ * writes must not escape the readonly boundary. Within the callback, injected TypeORM repositories
8
+ * automatically resolve against the active transaction manager, so repository operations join the
9
+ * same transaction without requiring manual rebinding.
10
+ *
11
+ * @template T
12
+ * @param dataSource The TypeORM data source used to start the transaction when none is active.
13
+ * @param runInTransaction Async callback that receives the transaction-bound entity manager.
14
+ * Injected TypeORM repositories used inside this callback automatically participate in the same
15
+ * transaction context.
16
+ * @returns The value returned by `runInTransaction`.
17
+ * @throws {Error} If called while a readonly context is active.
18
+ */
2
19
  export async function transaction(dataSource, runInTransaction) {
3
20
  if (isInReadonly()) {
4
21
  throw new Error('Cannot start a transaction inside a readonly context');
@@ -1 +1 @@
1
- {"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../lib/extensions/transaction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAE/D,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,UAAsB,EACtB,gBAA8D;IAE9D,IAAI,YAAY,EAAE,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAA;IACzE,CAAC;IACD,MAAM,kBAAkB,GAAG,kBAAkB,CAAC,QAAQ,EAAE,CAAA;IAExD,IAAI,kBAAkB,IAAI,IAAI,EAAE,CAAC;QAC/B,OAAO,MAAM,kBAAkB,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAA;IAC/D,CAAC;IAED,OAAO,MAAM,UAAU,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;QACpD,OAAO,MAAM,kBAAkB,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,IAAI,EAAE;YACtD,OAAO,MAAM,gBAAgB,CAAC,OAAO,CAAC,CAAA;QACxC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,UAAU,6BAA6B,CAAE,OAAsB;IACnE,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE;QACxB,GAAG,CAAE,MAAM,EAAE,IAAI;YACf,MAAM,OAAO,GAAG,kBAAkB,CAAC,QAAQ,EAAE,CAAA;YAE7C,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;gBACpB,OAAO,OAAO,CAAC,IAAI,CAAY,CAAA;YACjC,CAAC;iBAAM,CAAC;gBACN,OAAO,MAAM,CAAC,IAAI,CAAY,CAAA;YAChC,CAAC;QACH,CAAC;KACF,CAAC,CAAA;AACJ,CAAC"}
1
+ {"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../lib/extensions/transaction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAE/D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,UAAsB,EACtB,gBAA8D;IAE9D,IAAI,YAAY,EAAE,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAA;IACzE,CAAC;IACD,MAAM,kBAAkB,GAAG,kBAAkB,CAAC,QAAQ,EAAE,CAAA;IAExD,IAAI,kBAAkB,IAAI,IAAI,EAAE,CAAC;QAC/B,OAAO,MAAM,kBAAkB,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAA;IAC/D,CAAC;IAED,OAAO,MAAM,UAAU,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;QACpD,OAAO,MAAM,kBAAkB,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,IAAI,EAAE;YACtD,OAAO,MAAM,gBAAgB,CAAC,OAAO,CAAC,CAAA;QACxC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,UAAU,6BAA6B,CAAE,OAAsB;IACnE,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE;QACxB,GAAG,CAAE,MAAM,EAAE,IAAI;YACf,MAAM,OAAO,GAAG,kBAAkB,CAAC,QAAQ,EAAE,CAAA;YAE7C,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;gBACpB,OAAO,OAAO,CAAC,IAAI,CAAY,CAAA;YACjC,CAAC;iBAAM,CAAC;gBACN,OAAO,MAAM,CAAC,IAAI,CAAY,CAAA;YAChC,CAAC;QACH,CAAC;KACF,CAAC,CAAA;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wisemen/nestjs-typeorm",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },