@artenc/nest-orm 1.0.2 → 1.1.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.
@@ -7,4 +7,6 @@ export declare abstract class AbstractOrmEntity {
7
7
  save(): Promise<this>;
8
8
  protected fillable(): string[];
9
9
  apply(fields: Record<string, any>): void;
10
+ private get repositoryOrEntityManager();
11
+ private get contextEntityManager();
10
12
  }
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.AbstractOrmEntity = void 0;
13
13
  const class_transformer_1 = require("class-transformer");
14
14
  const typeorm_1 = require("typeorm");
15
+ const transaction_context_1 = require("../lib/transaction-context");
15
16
  class AbstractOrmEntity {
16
17
  repository;
17
18
  entityManager;
@@ -28,7 +29,7 @@ class AbstractOrmEntity {
28
29
  this.entityManager = entityManager;
29
30
  }
30
31
  save() {
31
- return this.repository ? this.repository.save(this) : this.entityManager.save(this);
32
+ return this.repositoryOrEntityManager.save(this);
32
33
  }
33
34
  fillable() {
34
35
  return [];
@@ -45,6 +46,22 @@ class AbstractOrmEntity {
45
46
  }
46
47
  });
47
48
  }
49
+ get repositoryOrEntityManager() {
50
+ if (this.entityManager) {
51
+ return this.entityManager;
52
+ }
53
+ if (this.contextEntityManager) {
54
+ return this.contextEntityManager;
55
+ }
56
+ if (this.repository) {
57
+ return this.repository;
58
+ }
59
+ throw new Error('No repository or entity manager available');
60
+ }
61
+ get contextEntityManager() {
62
+ const store = transaction_context_1.txStorage.getStore();
63
+ return store?.manager;
64
+ }
48
65
  }
49
66
  exports.AbstractOrmEntity = AbstractOrmEntity;
50
67
  __decorate([
@@ -1 +1 @@
1
- {"version":3,"file":"abstract-orm.entity.js","sourceRoot":"","sources":["../../src/entities/abstract-orm.entity.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yDAA2C;AAC3C,qCAAmD;AAEnD,MAAsB,iBAAiB;IAE3B,UAAU,CAAmB;IAE7B,aAAa,CAAgB;I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C,CAAA;IACV,CAAC;IAED,IAAY,yBAAyB;QACjC,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACrB,OAAO,IAAI,CAAC,aAAa,CAAA;QAC7B,CAAC;QACD,IAAI,IAAI,CAAC,oBAAoB,EAAE,CAAC;YAC5B,OAAO,IAAI,CAAC,oBAAoB,CAAA;QACpC,CAAC;QACD,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YAClB,OAAO,IAAI,CAAC,UAAU,CAAA;QAC1B,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAA;IAChE,CAAC;IAED,IAAY,oBAAoB;QAC5B,MAAM,KAAK,GAAG,+BAAS,CAAC,QAAQ,EAAE,CAAA;QAClC,OAAO,KAAK,EAAE,OAAO,CAAA;IACzB,CAAC;CACJ;AA5DD,8CA4DC;AA1DW;IADP,IAAA,2BAAO,GAAE;8BACW,oBAAU;qDAAM;AAE7B;IADP,IAAA,2BAAO,GAAE;8BACc,uBAAa;wDAAA"}
@@ -0,0 +1,7 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import { EntityManager } from 'typeorm';
3
+ type Store = {
4
+ manager?: EntityManager;
5
+ };
6
+ export declare const txStorage: AsyncLocalStorage<Store>;
7
+ export {};
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.txStorage = void 0;
4
+ const node_async_hooks_1 = require("node:async_hooks");
5
+ exports.txStorage = new node_async_hooks_1.AsyncLocalStorage();
6
+ //# sourceMappingURL=transaction-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction-context.js","sourceRoot":"","sources":["../../src/lib/transaction-context.ts"],"names":[],"mappings":";;;AAAA,uDAAoD;AAOvC,QAAA,SAAS,GAAG,IAAI,oCAAiB,EAAS,CAAA"}
@@ -4,13 +4,13 @@ import { Type } from '@nestjs/common';
4
4
  import { AbstractOrmEntity } from '../entities/abstract-orm.entity';
5
5
  export interface IOrmService<T extends AbstractOrmEntity> {
6
6
  readonly repository: Repository<T>;
7
- create(props: DeepPartial<T>, entityManager?: EntityManager): Promise<T>;
8
- exists(condition: FindOneOptions<T>, entityManager?: EntityManager): Promise<boolean>;
9
- find(conditions: FindManyOptions<T>, entityManager?: EntityManager): Promise<T[]>;
10
- findOne(conditions: FindOneOptions<T>, entityManager?: EntityManager): Promise<T | null>;
11
- count(conditions: FindManyOptions<T>, entityManager?: EntityManager): Promise<number>;
12
- update(criteria: FindOptionsWhere<T>, props: QueryDeepPartialEntity<T>, entityManager?: EntityManager): Promise<UpdateResult>;
13
- remove(id: number, entityManager?: EntityManager): Promise<DeleteResult>;
7
+ create(props: DeepPartial<T>, enforceEntityManager?: EntityManager): Promise<T>;
8
+ exists(condition: FindOneOptions<T>, enforceEntityManager?: EntityManager): Promise<boolean>;
9
+ find(conditions: FindManyOptions<T>, enforceEntityManager?: EntityManager): Promise<T[]>;
10
+ findOne(conditions: FindOneOptions<T>, enforceEntityManager?: EntityManager): Promise<T | null>;
11
+ count(conditions: FindManyOptions<T>, enforceEntityManager?: EntityManager): Promise<number>;
12
+ update(criteria: FindOptionsWhere<T>, props: QueryDeepPartialEntity<T>, enforceEntityManager?: EntityManager): Promise<UpdateResult>;
13
+ remove(id: number, enforceEntityManager?: EntityManager): Promise<DeleteResult>;
14
14
  }
15
15
  type Constructor<I> = new (...args: any[]) => I;
16
16
  export declare function AbstractOrmService<T extends AbstractOrmEntity>(entity: Constructor<T>): Type<IOrmService<T>>;
@@ -12,67 +12,90 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.AbstractOrmService = AbstractOrmService;
13
13
  const typeorm_1 = require("typeorm");
14
14
  const typeorm_2 = require("@nestjs/typeorm");
15
+ const transaction_context_1 = require("../lib/transaction-context");
15
16
  function AbstractOrmService(entity) {
16
17
  class AbstractOrmServiceHost {
17
18
  repository;
18
- create(props, entityManager) {
19
+ async create(props, enforceEntityManager) {
20
+ const entityManager = this.getEntityManager(enforceEntityManager);
21
+ const model = entityManager ? entityManager.create(entity, props) : this.repository.create(props);
19
22
  if (entityManager) {
20
- const model = entityManager.create(entity, props);
21
- model.setEntityManager(entityManager);
22
- return entityManager.save(model);
23
+ await entityManager.save(model);
23
24
  }
24
- const model = this.repository.create(props);
25
- model.setRepository(this.repository);
26
- return this.repository.save(model);
25
+ else {
26
+ await this.repository.save(model);
27
+ }
28
+ if (enforceEntityManager) {
29
+ model.setEntityManager(enforceEntityManager);
30
+ }
31
+ else {
32
+ model.setRepository(this.repository);
33
+ }
34
+ return model;
27
35
  }
28
- async exists(condition, entityManager) {
36
+ async exists(condition, enforceEntityManager) {
37
+ const entityManager = this.getEntityManager(enforceEntityManager);
29
38
  if (entityManager) {
30
- return !!(await entityManager.findOne(entity, condition));
39
+ return Boolean(await entityManager.findOne(entity, condition));
31
40
  }
32
- return !!(await this.repository.findOne(condition));
41
+ return Boolean(await this.repository.findOne(condition));
33
42
  }
34
- async find(conditions, entityManager) {
35
- if (entityManager) {
36
- const models = await entityManager.find(entity, conditions);
37
- models.forEach((m) => m.setEntityManager(entityManager));
38
- return models;
43
+ async find(conditions, enforceEntityManager) {
44
+ const entityManager = this.getEntityManager(enforceEntityManager);
45
+ const models = await (entityManager
46
+ ? entityManager.find(entity, conditions)
47
+ : this.repository.find(conditions));
48
+ if (enforceEntityManager) {
49
+ models.forEach((m) => m.setEntityManager(enforceEntityManager));
50
+ }
51
+ else {
52
+ models.forEach((m) => m.setRepository(this.repository));
39
53
  }
40
- const models = await this.repository.find(conditions);
41
- models.forEach((m) => m.setRepository(this.repository));
42
54
  return models;
43
55
  }
44
- async findOne(conditions, entityManager) {
45
- if (entityManager) {
46
- const model = await entityManager.findOne(entity, conditions);
47
- if (model) {
48
- model.setEntityManager(entityManager);
49
- }
50
- return model;
56
+ async findOne(conditions, enforceEntityManager) {
57
+ const entityManager = this.getEntityManager(enforceEntityManager);
58
+ const model = await (entityManager
59
+ ? entityManager.findOne(entity, conditions)
60
+ : this.repository.findOne(conditions));
61
+ if (!model) {
62
+ return null;
63
+ }
64
+ if (enforceEntityManager) {
65
+ model.setEntityManager(enforceEntityManager);
51
66
  }
52
- const model = await this.repository.findOne(conditions);
53
- if (model) {
67
+ else {
54
68
  model.setRepository(this.repository);
55
69
  }
56
70
  return model;
57
71
  }
58
- count(conditions, entityManager) {
72
+ count(conditions, enforceEntityManager) {
73
+ const entityManager = this.getEntityManager(enforceEntityManager);
59
74
  if (entityManager) {
60
75
  return entityManager.count(entity, conditions);
61
76
  }
62
77
  return this.repository.count(conditions);
63
78
  }
64
- update(criteria, props, entityManager) {
79
+ update(criteria, props, enforceEntityManager) {
80
+ const entityManager = this.getEntityManager(enforceEntityManager);
65
81
  if (entityManager) {
66
82
  return entityManager.update(entity, criteria, props);
67
83
  }
68
84
  return this.repository.update(criteria, props);
69
85
  }
70
- remove(id, entityManager) {
86
+ remove(id, enforceEntityManager) {
87
+ const entityManager = this.getEntityManager(enforceEntityManager);
71
88
  if (entityManager) {
72
89
  return entityManager.delete(entity, id);
73
90
  }
74
91
  return this.repository.delete(id);
75
92
  }
93
+ getEntityManager(enforceEntityManager) {
94
+ return enforceEntityManager ?? this.contextEntityManager;
95
+ }
96
+ get contextEntityManager() {
97
+ return transaction_context_1.txStorage.getStore()?.manager;
98
+ }
76
99
  }
77
100
  __decorate([
78
101
  (0, typeorm_2.InjectRepository)(entity),
@@ -1 +1 @@
1
- {"version":3,"file":"abstract-orm.service.js","sourceRoot":"","sources":["../../src/services/abstract-orm.service.ts"],"names":[],"mappings":";;;;;;;;;;;AAiCA,gDAkFC;AAnHD,qCASgB;AAGhB,6CAAkD;AAqBlD,SAAgB,kBAAkB,CAA8B,MAAsB;IAClF,MAAM,sBAAsB;QAExB,UAAU,CAAgB;QAEnB,MAAM,CAAC,KAAqB,EAAE,aAA6B;YAC9D,IAAI,aAAa,EAAE,CAAC;gBAChB,MAAM,KAAK,GAAG,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,C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,oBAAoB;YAC5B,OAAO,+BAAS,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAA;QACxC,CAAC;KACJ;IA5GG;QADC,IAAA,0BAAgB,EAAC,MAAM,CAAC;kCACZ,oBAAU;8DAAG;IA6G9B,OAAO,sBAAsB,CAAA;AACjC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { DataSource } from 'typeorm';
2
+ export declare class TransactionService {
3
+ private readonly dataSource;
4
+ constructor(dataSource: DataSource);
5
+ transaction<T>(fn: () => Promise<T>): Promise<T>;
6
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TransactionService = void 0;
13
+ const typeorm_1 = require("typeorm");
14
+ const common_1 = require("@nestjs/common");
15
+ const transaction_context_1 = require("../lib/transaction-context");
16
+ let TransactionService = class TransactionService {
17
+ dataSource;
18
+ constructor(dataSource) {
19
+ this.dataSource = dataSource;
20
+ }
21
+ async transaction(fn) {
22
+ const existing = transaction_context_1.txStorage.getStore()?.manager;
23
+ if (existing) {
24
+ return fn();
25
+ }
26
+ return this.dataSource.transaction(async (manager) => {
27
+ return transaction_context_1.txStorage.run({ manager }, fn);
28
+ });
29
+ }
30
+ };
31
+ exports.TransactionService = TransactionService;
32
+ exports.TransactionService = TransactionService = __decorate([
33
+ (0, common_1.Injectable)(),
34
+ __metadata("design:paramtypes", [typeorm_1.DataSource])
35
+ ], TransactionService);
36
+ //# sourceMappingURL=transaction.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.service.js","sourceRoot":"","sources":["../../src/services/transaction.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAAmD;AACnD,2CAA2C;AAC3C,oEAAsD;AAG/C,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IACE;IAA7B,YAA6B,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;IAAG,CAAC;IAEhD,KAAK,CAAC,WAAW,CAAI,EAAoB;QAC5C,MAAM,QAAQ,GAAG,+BAAS,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAA;QAC9C,IAAI,QAAQ,EAAE,CAAC;YACX,OAAO,EAAE,EAAE,CAAA;QACf,CAAC;QAED,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,KAAK,EAAE,OAAsB,EAAE,EAAE;YAChE,OAAO,+BAAS,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAA;QACzC,CAAC,CAAC,CAAA;IACN,CAAC;CACJ,CAAA;AAbY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,mBAAU,GAAE;qCAEgC,oBAAU;GAD1C,kBAAkB,CAa9B"}