metal-orm 1.0.36 → 1.0.38

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/dist/index.d.cts CHANGED
@@ -2297,7 +2297,9 @@ declare class OrmSession<E extends DomainEvent = OrmDomainEvent> implements Enti
2297
2297
  persist(entity: object): Promise<void>;
2298
2298
  remove(entity: object): Promise<void>;
2299
2299
  flush(): Promise<void>;
2300
+ private flushWithHooks;
2300
2301
  commit(): Promise<void>;
2302
+ transaction<T>(fn: (session: OrmSession<E>) => Promise<T>): Promise<T>;
2301
2303
  rollback(): Promise<void>;
2302
2304
  getExecutionContext(): ExecutionContext;
2303
2305
  getHydrationContext(): HydrationContext<E>;
package/dist/index.d.ts CHANGED
@@ -2297,7 +2297,9 @@ declare class OrmSession<E extends DomainEvent = OrmDomainEvent> implements Enti
2297
2297
  persist(entity: object): Promise<void>;
2298
2298
  remove(entity: object): Promise<void>;
2299
2299
  flush(): Promise<void>;
2300
+ private flushWithHooks;
2300
2301
  commit(): Promise<void>;
2302
+ transaction<T>(fn: (session: OrmSession<E>) => Promise<T>): Promise<T>;
2301
2303
  rollback(): Promise<void>;
2302
2304
  getExecutionContext(): ExecutionContext;
2303
2305
  getHydrationContext(): HydrationContext<E>;
package/dist/index.js CHANGED
@@ -7408,20 +7408,41 @@ var OrmSession = class {
7408
7408
  async flush() {
7409
7409
  await this.unitOfWork.flush();
7410
7410
  }
7411
+ async flushWithHooks() {
7412
+ for (const interceptor of this.interceptors) {
7413
+ await interceptor.beforeFlush?.(this);
7414
+ }
7415
+ await this.unitOfWork.flush();
7416
+ await this.relationChanges.process();
7417
+ await this.unitOfWork.flush();
7418
+ for (const interceptor of this.interceptors) {
7419
+ await interceptor.afterFlush?.(this);
7420
+ }
7421
+ }
7411
7422
  async commit() {
7412
7423
  await runInTransaction(this.executor, async () => {
7413
- for (const interceptor of this.interceptors) {
7414
- await interceptor.beforeFlush?.(this);
7415
- }
7416
- await this.unitOfWork.flush();
7417
- await this.relationChanges.process();
7418
- await this.unitOfWork.flush();
7419
- for (const interceptor of this.interceptors) {
7420
- await interceptor.afterFlush?.(this);
7421
- }
7424
+ await this.flushWithHooks();
7422
7425
  });
7423
7426
  await this.domainEvents.dispatch(this.unitOfWork.getTracked(), this);
7424
7427
  }
7428
+ async transaction(fn4) {
7429
+ if (!this.executor.beginTransaction) {
7430
+ const result = await fn4(this);
7431
+ await this.commit();
7432
+ return result;
7433
+ }
7434
+ await this.executor.beginTransaction();
7435
+ try {
7436
+ const result = await fn4(this);
7437
+ await this.flushWithHooks();
7438
+ await this.executor.commitTransaction?.();
7439
+ await this.domainEvents.dispatch(this.unitOfWork.getTracked(), this);
7440
+ return result;
7441
+ } catch (err) {
7442
+ await this.rollback();
7443
+ throw err;
7444
+ }
7445
+ }
7425
7446
  async rollback() {
7426
7447
  await this.executor.rollbackTransaction?.();
7427
7448
  this.unitOfWork.reset();