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/README.md CHANGED
@@ -51,6 +51,7 @@ Full docs live in the `docs/` folder:
51
51
  - [Multi-Dialect Support](https://github.com/celsowm/metal-orm/blob/main/docs/multi-dialect-support.md)
52
52
  - [Schema Generation (DDL)](https://github.com/celsowm/metal-orm/blob/main/docs/schema-generation.md)
53
53
  - [API Reference](https://github.com/celsowm/metal-orm/blob/main/docs/api-reference.md)
54
+ - [DB ➜ TS Type Mapping](https://github.com/celsowm/metal-orm/blob/main/docs/db-to-ts-types.md)
54
55
 
55
56
  ---
56
57
 
@@ -83,6 +84,7 @@ On top of the query builder, MetalORM ships a focused runtime managed by `Orm` a
83
84
 
84
85
  - **Entities inferred from your `TableDef`s** (no separate mapping file).
85
86
  - **Lazy, batched relations**: `user.posts.load()`, `user.roles.syncByIds([...])`, etc.
87
+ - **Scoped transactions**: `session.transaction(async s => { ... })` wraps `begin/commit/rollback` on the existing executor; `Orm.transaction` remains available when you want a fresh transactional executor per call.
86
88
  - **Identity map**: the same row becomes the same entity instance within a session (see the [Identity map pattern](https://en.wikipedia.org/wiki/Identity_map_pattern)).
87
89
  - **Unit of Work (`OrmSession`)** tracking New/Dirty/Removed entities and relation changes, inspired by the classic [Unit of Work pattern](https://en.wikipedia.org/wiki/Unit_of_work).
88
90
  - **Graph persistence**: mutate a whole object graph and flush once with `session.commit()`.
package/dist/index.cjs CHANGED
@@ -7613,20 +7613,41 @@ var OrmSession = class {
7613
7613
  async flush() {
7614
7614
  await this.unitOfWork.flush();
7615
7615
  }
7616
+ async flushWithHooks() {
7617
+ for (const interceptor of this.interceptors) {
7618
+ await interceptor.beforeFlush?.(this);
7619
+ }
7620
+ await this.unitOfWork.flush();
7621
+ await this.relationChanges.process();
7622
+ await this.unitOfWork.flush();
7623
+ for (const interceptor of this.interceptors) {
7624
+ await interceptor.afterFlush?.(this);
7625
+ }
7626
+ }
7616
7627
  async commit() {
7617
7628
  await runInTransaction(this.executor, async () => {
7618
- for (const interceptor of this.interceptors) {
7619
- await interceptor.beforeFlush?.(this);
7620
- }
7621
- await this.unitOfWork.flush();
7622
- await this.relationChanges.process();
7623
- await this.unitOfWork.flush();
7624
- for (const interceptor of this.interceptors) {
7625
- await interceptor.afterFlush?.(this);
7626
- }
7629
+ await this.flushWithHooks();
7627
7630
  });
7628
7631
  await this.domainEvents.dispatch(this.unitOfWork.getTracked(), this);
7629
7632
  }
7633
+ async transaction(fn4) {
7634
+ if (!this.executor.beginTransaction) {
7635
+ const result = await fn4(this);
7636
+ await this.commit();
7637
+ return result;
7638
+ }
7639
+ await this.executor.beginTransaction();
7640
+ try {
7641
+ const result = await fn4(this);
7642
+ await this.flushWithHooks();
7643
+ await this.executor.commitTransaction?.();
7644
+ await this.domainEvents.dispatch(this.unitOfWork.getTracked(), this);
7645
+ return result;
7646
+ } catch (err) {
7647
+ await this.rollback();
7648
+ throw err;
7649
+ }
7650
+ }
7630
7651
  async rollback() {
7631
7652
  await this.executor.rollbackTransaction?.();
7632
7653
  this.unitOfWork.reset();