metal-orm 1.1.3 → 1.1.4

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.
Files changed (37) hide show
  1. package/README.md +715 -703
  2. package/dist/index.cjs +655 -75
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +170 -8
  5. package/dist/index.d.ts +170 -8
  6. package/dist/index.js +649 -75
  7. package/dist/index.js.map +1 -1
  8. package/package.json +1 -1
  9. package/scripts/naming-strategy.mjs +16 -1
  10. package/src/core/ast/procedure.ts +21 -0
  11. package/src/core/ast/query.ts +47 -19
  12. package/src/core/ddl/introspect/utils.ts +56 -56
  13. package/src/core/dialect/abstract.ts +560 -547
  14. package/src/core/dialect/base/sql-dialect.ts +43 -29
  15. package/src/core/dialect/mssql/index.ts +369 -232
  16. package/src/core/dialect/mysql/index.ts +99 -7
  17. package/src/core/dialect/postgres/index.ts +121 -60
  18. package/src/core/dialect/sqlite/index.ts +97 -64
  19. package/src/core/execution/db-executor.ts +108 -90
  20. package/src/core/execution/executors/mssql-executor.ts +28 -24
  21. package/src/core/execution/executors/mysql-executor.ts +62 -27
  22. package/src/core/execution/executors/sqlite-executor.ts +10 -9
  23. package/src/index.ts +9 -6
  24. package/src/orm/execute-procedure.ts +77 -0
  25. package/src/orm/execute.ts +74 -73
  26. package/src/orm/interceptor-pipeline.ts +21 -17
  27. package/src/orm/pooled-executor-factory.ts +41 -20
  28. package/src/orm/unit-of-work.ts +6 -4
  29. package/src/query/index.ts +8 -5
  30. package/src/query-builder/delete.ts +3 -2
  31. package/src/query-builder/insert-query-state.ts +47 -19
  32. package/src/query-builder/insert.ts +142 -28
  33. package/src/query-builder/procedure-call.ts +122 -0
  34. package/src/query-builder/select/select-operations.ts +5 -2
  35. package/src/query-builder/select.ts +1146 -1105
  36. package/src/query-builder/update.ts +3 -2
  37. package/src/tree/tree-manager.ts +754 -754
package/README.md CHANGED
@@ -1,189 +1,191 @@
1
- # MetalORM ⚙️
2
-
3
- [![npm version](https://img.shields.io/npm/v/metal-orm.svg)](https://www.npmjs.com/package/metal-orm)
4
- [![license](https://img.shields.io/npm/l/metal-orm.svg)](https://github.com/celsowm/metal-orm/blob/main/LICENSE)
5
- [![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%23007ACC.svg)](https://www.typescriptlang.org/)
6
-
7
- > **TypeScript-first ORM that adapts to your needs**: use it as a type-safe query builder, a full-featured ORM runtime, or anything in between.
8
-
9
- ## Why MetalORM? 💡
10
-
11
- - 🎯 **Gradual adoption**: Start with just SQL building, add ORM features when you need them
12
- - 🔒 **Exceptionally strongly typed**: Built with TypeScript generics and type inference—**zero** `any` types in the entire codebase
13
- - 🏗️ **Well-architected**: Implements proven design patterns (Strategy, Visitor, Builder, Unit of Work, Identity Map, Interceptor, and more)
14
- - 🎨 **One AST, multiple levels**: All features share the same SQL AST foundation—no magic, just composable layers
15
- - 🚀 **Multi-dialect from the start**: MySQL, PostgreSQL, SQLite, SQL Server support built-in
16
-
17
- ---
18
-
19
- ## ⚡ 30-Second Quick Start
20
-
21
- ```ts
22
- import { defineTable, col, selectFrom, MySqlDialect } from 'metal-orm';
23
-
24
- const users = defineTable('users', {
25
- id: col.primaryKey(col.int()),
26
- name: col.varchar(255),
27
- });
28
-
29
- const query = selectFrom(users).select('id', 'name').limit(10);
30
- const { sql, params } = query.compile(new MySqlDialect());
31
- // That's it! Use sql + params with any driver.
32
- // ↑ Fully typed—no casting, no 'any', just strong types all the way down
33
- ```
34
-
35
- ---
36
-
37
- ## Three Levels of Abstraction
38
-
39
- MetalORM is a TypeScript-first, AST-driven SQL toolkit you can dial up or down depending on how "ORM-y" you want to be:
40
-
41
- - **Level 1 – Query builder & hydration 🧩**
42
- Define tables with `defineTable` / `col.*`, build strongly-typed queries on a real SQL AST, and hydrate flat result sets into nested objects – no ORM runtime involved.
43
-
44
- - **Level 2 – ORM runtime (entities + Unit of Work 🧠)**
45
- Let `OrmSession` (created from `Orm`) turn rows into tracked entities with lazy relations, cascades, and a [Unit of Work](https://en.wikipedia.org/wiki/Unit_of_work) that flushes changes with `session.commit()`.
46
-
47
- - **Level 3 – Decorator entities (classes + metadata ✨)**
48
- Use `@Entity`, `@Column`, `@PrimaryKey`, relation decorators, `bootstrapEntities()` (or the lazy bootstrapping in `getTableDefFromEntity` / `selectFromEntity`) to describe your model classes. MetalORM bootstraps schema & relations from metadata and plugs them into the same runtime and query builder.
49
-
50
- **Use only the layer you need in each part of your codebase.**
51
-
52
- ---
53
-
54
- <a id="table-of-contents"></a>
55
- ## Table of Contents 🧭
56
-
57
- - [Documentation](#documentation)
58
- - [Features](#features)
59
- - [Installation](#installation)
60
- - [Quick start - three levels](#quick-start)
61
- - [Level 1 – Query builder & hydration](#level-1)
62
- - [Level 2 – Entities + Unit of Work](#level-2)
63
- - [Level 3 – Decorator entities](#level-3)
64
- - [When to use which level?](#when-to-use-which-level)
65
- - [Design & Architecture](#design-notes)
66
- - [FAQ](#frequently-asked-questions-)
67
- - [Performance & Production](#performance--production-)
68
- - [Community & Support](#community--support-)
69
- - [Contributing](#contributing)
70
- - [License](#license)
71
-
72
- ---
73
-
74
- <a id="documentation"></a>
75
- ## Documentation 📚
76
-
77
- Full docs live in the `docs/` folder:
78
-
79
- - [Introduction](https://github.com/celsowm/metal-orm/blob/main/docs/index.md)
80
- - [Getting Started](https://github.com/celsowm/metal-orm/blob/main/docs/getting-started.md)
81
- - [Level 3 Backend Tutorial](https://github.com/celsowm/metal-orm/blob/main/docs/level-3-backend-tutorial.md)
1
+ # MetalORM ⚙️
2
+
3
+ [![npm version](https://img.shields.io/npm/v/metal-orm.svg)](https://www.npmjs.com/package/metal-orm)
4
+ [![license](https://img.shields.io/npm/l/metal-orm.svg)](https://github.com/celsowm/metal-orm/blob/main/LICENSE)
5
+ [![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%23007ACC.svg)](https://www.typescriptlang.org/)
6
+
7
+ > **TypeScript-first ORM that adapts to your needs**: use it as a type-safe query builder, a full-featured ORM runtime, or anything in between.
8
+
9
+ ## Why MetalORM? 💡
10
+
11
+ - 🎯 **Gradual adoption**: Start with just SQL building, add ORM features when you need them
12
+ - 🔒 **Exceptionally strongly typed**: Built with TypeScript generics and type inference—**zero** `any` types in the entire codebase
13
+ - 🏗️ **Well-architected**: Implements proven design patterns (Strategy, Visitor, Builder, Unit of Work, Identity Map, Interceptor, and more)
14
+ - 🎨 **One AST, multiple levels**: All features share the same SQL AST foundation—no magic, just composable layers
15
+ - 🚀 **Multi-dialect from the start**: MySQL, PostgreSQL, SQLite, SQL Server support built-in
16
+
17
+ ---
18
+
19
+ ## ⚡ 30-Second Quick Start
20
+
21
+ ```ts
22
+ import { defineTable, col, selectFrom, MySqlDialect } from 'metal-orm';
23
+
24
+ const users = defineTable('users', {
25
+ id: col.primaryKey(col.int()),
26
+ name: col.varchar(255),
27
+ });
28
+
29
+ const query = selectFrom(users).select('id', 'name').limit(10);
30
+ const { sql, params } = query.compile(new MySqlDialect());
31
+ // That's it! Use sql + params with any driver.
32
+ // ↑ Fully typed—no casting, no 'any', just strong types all the way down
33
+ ```
34
+
35
+ ---
36
+
37
+ ## Three Levels of Abstraction
38
+
39
+ MetalORM is a TypeScript-first, AST-driven SQL toolkit you can dial up or down depending on how "ORM-y" you want to be:
40
+
41
+ - **Level 1 – Query builder & hydration 🧩**
42
+ Define tables with `defineTable` / `col.*`, build strongly-typed queries on a real SQL AST, and hydrate flat result sets into nested objects – no ORM runtime involved.
43
+
44
+ - **Level 2 – ORM runtime (entities + Unit of Work 🧠)**
45
+ Let `OrmSession` (created from `Orm`) turn rows into tracked entities with lazy relations, cascades, and a [Unit of Work](https://en.wikipedia.org/wiki/Unit_of_work) that flushes changes with `session.commit()`.
46
+
47
+ - **Level 3 – Decorator entities (classes + metadata ✨)**
48
+ Use `@Entity`, `@Column`, `@PrimaryKey`, relation decorators, `bootstrapEntities()` (or the lazy bootstrapping in `getTableDefFromEntity` / `selectFromEntity`) to describe your model classes. MetalORM bootstraps schema & relations from metadata and plugs them into the same runtime and query builder.
49
+
50
+ **Use only the layer you need in each part of your codebase.**
51
+
52
+ ---
53
+
54
+ <a id="table-of-contents"></a>
55
+ ## Table of Contents 🧭
56
+
57
+ - [Documentation](#documentation)
58
+ - [Features](#features)
59
+ - [Installation](#installation)
60
+ - [Quick start - three levels](#quick-start)
61
+ - [Level 1 – Query builder & hydration](#level-1)
62
+ - [Level 2 – Entities + Unit of Work](#level-2)
63
+ - [Level 3 – Decorator entities](#level-3)
64
+ - [When to use which level?](#when-to-use-which-level)
65
+ - [Design & Architecture](#design-notes)
66
+ - [FAQ](#frequently-asked-questions-)
67
+ - [Performance & Production](#performance--production-)
68
+ - [Community & Support](#community--support-)
69
+ - [Contributing](#contributing)
70
+ - [License](#license)
71
+
72
+ ---
73
+
74
+ <a id="documentation"></a>
75
+ ## Documentation 📚
76
+
77
+ Full docs live in the `docs/` folder:
78
+
79
+ - [Introduction](https://github.com/celsowm/metal-orm/blob/main/docs/index.md)
80
+ - [Getting Started](https://github.com/celsowm/metal-orm/blob/main/docs/getting-started.md)
81
+ - [Level 3 Backend Tutorial](https://github.com/celsowm/metal-orm/blob/main/docs/level-3-backend-tutorial.md)
82
82
  - [Schema Definition](https://github.com/celsowm/metal-orm/blob/main/docs/schema-definition.md)
83
83
  - [Query Builder](https://github.com/celsowm/metal-orm/blob/main/docs/query-builder.md)
84
84
  - [Tree Behavior (Nested Set/MPTT)](https://github.com/celsowm/metal-orm/blob/main/docs/tree.md)
85
85
  - [DTO (Data Transfer Objects)](https://github.com/celsowm/metal-orm/blob/main/docs/dto.md)
86
86
  - [OpenAPI Schema Generation](https://github.com/celsowm/metal-orm/blob/main/docs/openapi.md)
87
87
  - [DML Operations](https://github.com/celsowm/metal-orm/blob/main/docs/dml-operations.md)
88
- - [Hydration & Entities](https://github.com/celsowm/metal-orm/blob/main/docs/hydration.md)
88
+ - [Hydration & Entities](https://github.com/celsowm/metal-orm/blob/main/docs/hydration.md)
89
89
  - [Runtime & Unit of Work](https://github.com/celsowm/metal-orm/blob/main/docs/runtime.md)
90
90
  - [Save Graph](https://github.com/celsowm/metal-orm/blob/main/docs/save-graph.md)
91
91
  - [Caching](https://github.com/celsowm/metal-orm/blob/main/docs/caching.md)
92
92
  - [Advanced Features](https://github.com/celsowm/metal-orm/blob/main/docs/advanced-features.md)
93
- - [Multi-Dialect Support](https://github.com/celsowm/metal-orm/blob/main/docs/multi-dialect-support.md)
94
- - [Schema Generation (DDL)](https://github.com/celsowm/metal-orm/blob/main/docs/schema-generation.md)
95
- - [API Reference](https://github.com/celsowm/metal-orm/blob/main/docs/api-reference.md)
96
- - [DB ➜ TS Type Mapping](https://github.com/celsowm/metal-orm/blob/main/docs/db-to-ts-types.md)
97
-
98
- ---
99
-
100
- <a id="features"></a>
101
- ## Features 🚀
102
-
103
- ### Level 1 – Query builder & hydration
104
-
105
- - **Declarative schema definition** with `defineTable`, `col.*`, and typed relations.
106
- - **Typed temporal columns**: `col.date()` / `col.datetime()` / `col.timestamp()` default to `string` but accept a generic when your driver returns `Date` (e.g. `col.date<Date>()`).
107
- - **Fluent query builder** over a real SQL AST
108
- (`SelectQueryBuilder`, `InsertQueryBuilder`, `UpdateQueryBuilder`, `DeleteQueryBuilder`).
109
- - **Advanced SQL**: CTEs, aggregates, window functions, subqueries, bitwise operators (`&`, `|`, `^`, `<<`, `>>`), JSON, CASE, EXISTS, and the full SQL function catalog (e.g. `STDDEV`, `VARIANCE`, `LOG2`, `CBRT`, `COALESCE`, `NULLIF`, `GREATEST`, `LEAST`, `IFNULL`, `LOCALTIME`, `LOCALTIMESTAMP`, `AGE`).
110
- - **Table-valued functions**: use the new `tvf(key, …)` helper when you want portable intents such as `ARRAY_UNNEST`, letting the dialects’ `TableFunctionStrategy` renderers emit dialect-specific syntax (`LATERAL`/`WITH ORDINALITY`, alias validation, quoting, etc.). `fnTable()` remains available as the raw escape hatch when you need to emit a specific SQL function directly.
111
- - **String helpers**: `lower`, `upper`, `trim`, `ltrim/rtrim`, `concat/concatWs`, `substr/left/right`, `position/instr/locate`, `replace`, `repeat`, `lpad/rpad`, `space`, and more with dialect-aware rendering.
112
- - **Set operations**: `union`, `unionAll`, `intersect`, `except` across all dialects (ORDER/LIMIT apply to the combined result; hydration is disabled for compound queries so rows are returned as-is without collapsing duplicates).
93
+ - [Multi-Dialect Support](https://github.com/celsowm/metal-orm/blob/main/docs/multi-dialect-support.md)
94
+ - [Schema Generation (DDL)](https://github.com/celsowm/metal-orm/blob/main/docs/schema-generation.md)
95
+ - [API Reference](https://github.com/celsowm/metal-orm/blob/main/docs/api-reference.md)
96
+ - [DB ➜ TS Type Mapping](https://github.com/celsowm/metal-orm/blob/main/docs/db-to-ts-types.md)
97
+ - [Stored Procedures](https://github.com/celsowm/metal-orm/blob/main/docs/stored-procedures.md)
98
+
99
+ ---
100
+
101
+ <a id="features"></a>
102
+ ## Features 🚀
103
+
104
+ ### Level 1 – Query builder & hydration
105
+
106
+ - **Declarative schema definition** with `defineTable`, `col.*`, and typed relations.
107
+ - **Typed temporal columns**: `col.date()` / `col.datetime()` / `col.timestamp()` default to `string` but accept a generic when your driver returns `Date` (e.g. `col.date<Date>()`).
108
+ - **Fluent query builder** over a real SQL AST
109
+ (`SelectQueryBuilder`, `InsertQueryBuilder`, `UpdateQueryBuilder`, `DeleteQueryBuilder`).
110
+ - **Advanced SQL**: CTEs, aggregates, window functions, subqueries, bitwise operators (`&`, `|`, `^`, `<<`, `>>`), JSON, CASE, EXISTS, and the full SQL function catalog (e.g. `STDDEV`, `VARIANCE`, `LOG2`, `CBRT`, `COALESCE`, `NULLIF`, `GREATEST`, `LEAST`, `IFNULL`, `LOCALTIME`, `LOCALTIMESTAMP`, `AGE`).
111
+ - **Table-valued functions**: use the new `tvf(key, …)` helper when you want portable intents such as `ARRAY_UNNEST`, letting the dialects’ `TableFunctionStrategy` renderers emit dialect-specific syntax (`LATERAL`/`WITH ORDINALITY`, alias validation, quoting, etc.). `fnTable()` remains available as the raw escape hatch when you need to emit a specific SQL function directly.
112
+ - **String helpers**: `lower`, `upper`, `trim`, `ltrim/rtrim`, `concat/concatWs`, `substr/left/right`, `position/instr/locate`, `replace`, `repeat`, `lpad/rpad`, `space`, and more with dialect-aware rendering.
113
+ - **Set operations**: `union`, `unionAll`, `intersect`, `except` across all dialects (ORDER/LIMIT apply to the combined result; hydration is disabled for compound queries so rows are returned as-is without collapsing duplicates).
113
114
  - **Expression builders**: `eq`, `and`, `or`, `between`, `inList`, `exists`, `jsonPath`, `caseWhen`, window functions like `rowNumber`, `rank`, `lag`, `lead`, etc., all backed by typed AST nodes.
114
115
  - **Operator safety**: scalar operators (`eq`, `neq`, `gt`, `gte`, `lt`, `lte`) are for single values; for arrays, use `inList`/`notInList`.
115
116
  - Migration example: `where(eq(tipoAcao.columns.codigo, codigos))` -> `where(inList(tipoAcao.columns.codigo, codigos))`.
116
117
  - **Relation-aware hydration**: turn flat rows into nested objects (`user.posts`, `user.roles`, etc.) using a hydration plan derived from the AST metadata.
117
- - **Multi-dialect**: compile once, run on MySQL/MariaDB, PostgreSQL, SQLite, or SQL Server via pluggable dialects.
118
- - **DML**: type-safe INSERT / UPDATE / DELETE with `RETURNING` where supported.
119
-
120
- Level 1 is ideal when you:
121
-
122
- - Already have a domain model and just want a serious SQL builder.
123
- - Want deterministic SQL (no magical query generation).
124
- - Need to share the same AST across tooling (e.g. codegen, diagnostics, logging).
125
-
126
- ### Level 2 – ORM runtime (`OrmSession`)
127
-
128
- On top of the query builder, MetalORM ships a focused runtime managed by `Orm` and its request-scoped `OrmSession`s:
129
-
130
- - **Entities inferred from your `TableDef`s** (no separate mapping file).
131
- - **Lazy, batched relations**: `user.posts.load()`, `user.roles.syncByIds([...])`, etc.
118
+ - **Multi-dialect**: compile once, run on MySQL/MariaDB, PostgreSQL, SQLite, or SQL Server via pluggable dialects.
119
+ - **DML**: type-safe INSERT / UPDATE / DELETE with `RETURNING` where supported.
120
+ - Includes upsert support via `.onConflict(...).doUpdate(...)` / `.doNothing()` with dialect-specific SQL generation.
121
+
122
+ Level 1 is ideal when you:
123
+
124
+ - Already have a domain model and just want a serious SQL builder.
125
+ - Want deterministic SQL (no magical query generation).
126
+ - Need to share the same AST across tooling (e.g. codegen, diagnostics, logging).
127
+
128
+ ### Level 2 – ORM runtime (`OrmSession`)
129
+
130
+ On top of the query builder, MetalORM ships a focused runtime managed by `Orm` and its request-scoped `OrmSession`s:
131
+
132
+ - **Entities inferred from your `TableDef`s** (no separate mapping file).
133
+ - **Lazy, batched relations**: `user.posts.load()`, `user.roles.syncByIds([...])`, etc.
132
134
  - **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.
133
135
  - **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)).
134
136
  - **Caching**: Flexible caching with `MemoryCacheAdapter` (dev), `KeyvCacheAdapter` (simple production), or `RedisCacheAdapter` (full-featured with tag support). Features human-readable TTL (`'30m'`, `'2h'`), tag-based invalidation, and multi-tenant cache isolation.
135
137
  - **Tree Behavior (Nested Set/MPTT)**: hierarchical data with `TreeManager`, `treeQuery()`, and `@Tree` decorators. Efficient O(log n) operations for moves, inserts, and deletes. Supports multi-tree scoping, recovery, and validation.
136
138
  - **DTO/OpenAPI helpers**: the `metal-orm/dto` module generates DTOs and OpenAPI schemas, including tree schemas (`TreeNode`, `TreeNodeResult`, threaded trees).
137
139
  - **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/wiki/Unit_of_work).
138
- - **Graph persistence**: mutate a whole object graph and flush once with `session.commit()`.
139
- - **Partial updates**: use `session.patchGraph()` to update only specific fields of an entity and its relations (returns `null` if entity doesn't exist).
140
- - **Relation change processor** that knows how to deal with has-many and many-to-many pivot tables.
141
- - **Interceptors**: `beforeFlush` / `afterFlush` hooks for cross-cutting concerns (auditing, multi-tenant filters, soft delete filters, etc.).
142
- - **Domain events**: `addDomainEvent` and a DomainEventBus integrated into `session.commit()`, aligned with domain events from [Domain-driven design](https://en.wikipedia.org/wiki/Domain-driven_design).
143
- - **JSON-safe entities**: relation wrappers hide internal references and implement `toJSON`, so `JSON.stringify` of hydrated entities works without circular reference errors.
144
-
145
- Use this layer where:
146
-
147
- - A request-scoped context fits (web/API handlers, jobs).
148
- - You want change tracking, cascades, and relation helpers instead of manual SQL for every update.
149
-
150
- ### Level 3 – Decorator entities
151
-
152
- If you like explicit model classes, you can add a thin decorator layer on top of the same schema/runtime:
153
-
154
- - `@Entity()` on a class to derive and register a table name (by default snake_case plural of the class name, with an optional `tableName` override).
155
- - `@Column(...)` and `@PrimaryKey(...)` on properties; decorators collect column metadata and later build `TableDef`s from it.
156
- - Relation decorators:
157
- - `@HasMany({ target, foreignKey, ... })`
158
- - `@HasOne({ target, foreignKey, ... })`
159
- - `@BelongsTo({ target, foreignKey, ... })`
160
- - `@BelongsToMany({ target, pivotTable, ... })`
161
- - `bootstrapEntities()` scans metadata, builds `TableDef`s, wires relations with the same `hasOne` / `hasMany` / `belongsTo` / `belongsToMany` helpers you would use manually, and returns the resulting tables. (If you forget to call it, `getTableDefFromEntity` / `selectFromEntity` will bootstrap lazily on first use, but bootstrapping once at startup lets you reuse the same table defs and generate schema SQL.)
162
- - `selectFromEntity(MyEntity)` lets you start a `SelectQueryBuilder` directly from the class. By default, `execute(session)` returns actual entity instances with all columns selected.
163
- - **Generate entities from an existing DB**: `npx metal-orm-gen -- --dialect=postgres --url=$DATABASE_URL --schema=public --out=src/entities.ts` introspects your schema and spits out `@Entity` / `@Column` classes you can immediately `bootstrapEntities()` with.
164
-
165
- You don’t have to use decorators, but when you do, you’re still on the same AST + dialect + runtime foundation.
166
-
167
- ---
168
-
169
- <a id="installation"></a>
170
- ## Installation 📦
171
-
172
- **Requirements:** Node.js ≥ 20.0.0. For TypeScript projects, use TS 5.6+ to get the standard decorators API and typings.
173
-
174
- ```bash
175
- # npm
176
- npm install metal-orm
177
-
178
- # yarn
179
- yarn add metal-orm
180
-
181
- # pnpm
182
- pnpm add metal-orm
183
- ```
184
-
185
- MetalORM compiles SQL; you bring your own driver:
186
-
140
+ - **Graph persistence**: mutate a whole object graph and flush once with `session.commit()`.
141
+ - **Partial updates**: use `session.patchGraph()` to update only specific fields of an entity and its relations (returns `null` if entity doesn't exist).
142
+ - **Relation change processor** that knows how to deal with has-many and many-to-many pivot tables.
143
+ - **Interceptors**: `beforeFlush` / `afterFlush` hooks for cross-cutting concerns (auditing, multi-tenant filters, soft delete filters, etc.).
144
+ - **Domain events**: `addDomainEvent` and a DomainEventBus integrated into `session.commit()`, aligned with domain events from [Domain-driven design](https://en.wikipedia.org/wiki/Domain-driven_design).
145
+ - **JSON-safe entities**: relation wrappers hide internal references and implement `toJSON`, so `JSON.stringify` of hydrated entities works without circular reference errors.
146
+
147
+ Use this layer where:
148
+
149
+ - A request-scoped context fits (web/API handlers, jobs).
150
+ - You want change tracking, cascades, and relation helpers instead of manual SQL for every update.
151
+
152
+ ### Level 3 – Decorator entities
153
+
154
+ If you like explicit model classes, you can add a thin decorator layer on top of the same schema/runtime:
155
+
156
+ - `@Entity()` on a class to derive and register a table name (by default snake_case plural of the class name, with an optional `tableName` override).
157
+ - `@Column(...)` and `@PrimaryKey(...)` on properties; decorators collect column metadata and later build `TableDef`s from it.
158
+ - Relation decorators:
159
+ - `@HasMany({ target, foreignKey, ... })`
160
+ - `@HasOne({ target, foreignKey, ... })`
161
+ - `@BelongsTo({ target, foreignKey, ... })`
162
+ - `@BelongsToMany({ target, pivotTable, ... })`
163
+ - `bootstrapEntities()` scans metadata, builds `TableDef`s, wires relations with the same `hasOne` / `hasMany` / `belongsTo` / `belongsToMany` helpers you would use manually, and returns the resulting tables. (If you forget to call it, `getTableDefFromEntity` / `selectFromEntity` will bootstrap lazily on first use, but bootstrapping once at startup lets you reuse the same table defs and generate schema SQL.)
164
+ - `selectFromEntity(MyEntity)` lets you start a `SelectQueryBuilder` directly from the class. By default, `execute(session)` returns actual entity instances with all columns selected.
165
+ - **Generate entities from an existing DB**: `npx metal-orm-gen -- --dialect=postgres --url=$DATABASE_URL --schema=public --out=src/entities.ts` introspects your schema and spits out `@Entity` / `@Column` classes you can immediately `bootstrapEntities()` with.
166
+
167
+ You don’t have to use decorators, but when you do, you’re still on the same AST + dialect + runtime foundation.
168
+
169
+ ---
170
+
171
+ <a id="installation"></a>
172
+ ## Installation 📦
173
+
174
+ **Requirements:** Node.js ≥ 20.0.0. For TypeScript projects, use TS 5.6+ to get the standard decorators API and typings.
175
+
176
+ ```bash
177
+ # npm
178
+ npm install metal-orm
179
+
180
+ # yarn
181
+ yarn add metal-orm
182
+
183
+ # pnpm
184
+ pnpm add metal-orm
185
+ ```
186
+
187
+ MetalORM compiles SQL; you bring your own driver:
188
+
187
189
  | Dialect | Driver | Install |
188
190
  | ------------------ | --------- | ---------------------- |
189
191
  | MySQL / MariaDB | `mysql2` | `npm install mysql2` |
@@ -213,540 +215,550 @@ npm install keyv @keyv/redis
213
215
  ```
214
216
 
215
217
  > Caching packages are optional peer dependencies. MetalORM includes `MemoryCacheAdapter` for development without external dependencies.
216
-
217
- ### Playground (optional) 🧪
218
-
219
- The React playground lives in `playground/` and is no longer part of the published package or its dependency tree. To run it locally:
220
-
221
- 1. `cd playground && npm install`
222
- 2. `npm run dev` (uses the root `vite.config.ts`)
223
-
224
- It boots against an in-memory SQLite database seeded from fixtures under `playground/shared/`.
225
-
226
- ---
227
-
228
- <a id="quick-start"></a>
229
- ## Quick start – three levels
230
-
231
- <a id="level-1"></a>
232
- ### Level 1: Query builder & hydration 🧩
233
-
234
- #### 1. Tiny table, tiny query
235
-
236
- MetalORM can be just a straightforward query builder.
237
-
238
- ```ts
239
- import mysql from 'mysql2/promise';
240
- import {
241
- defineTable,
242
- tableRef,
243
- col,
244
- selectFrom,
245
- eq,
246
- MySqlDialect,
247
- } from 'metal-orm';
248
-
249
- // 1) A very small table
250
- const todos = defineTable('todos', {
251
- id: col.primaryKey(col.int()),
252
- title: col.varchar(255),
253
- done: col.boolean(),
254
- });
255
- // Add constraints
256
- todos.columns.title.notNull = true;
257
- todos.columns.done.default = false;
258
-
259
- // Optional: opt-in ergonomic column access
260
- const t = tableRef(todos);
261
-
262
- // 2) Build a simple query
263
- const listOpenTodos = selectFrom(todos)
264
- .select('id', 'title', 'done')
265
- .where(eq(t.done, false))
266
- .orderBy(t.id, 'ASC');
267
-
268
- // 3) Compile to SQL + params
269
- const dialect = new MySqlDialect();
270
- const { sql, params } = listOpenTodos.compile(dialect);
271
-
272
- // 4) Run with your favorite driver
273
- const connection = await mysql.createConnection({ /* ... */ });
274
- const [rows] = await connection.execute(sql, params);
275
-
276
- console.log(rows);
277
- // [
278
- // { id: 1, title: 'Write docs', done: 0 },
279
- // { id: 2, title: 'Ship feature', done: 0 },
280
- // ]
281
- ```
282
-
283
- If you keep a reusable array of column names (e.g. shared across helpers or pulled from config), you can spread it into `.select(...)` since the method accepts rest arguments:
284
-
285
- ```ts
286
- const defaultColumns = ['id', 'title', 'done'] as const;
287
- const listOpenTodos = selectFrom(todos).select(...defaultColumns);
288
- ```
289
-
290
- That's it: schema, query, SQL, done.
291
-
292
- If you are using the Level 2 runtime (`OrmSession`), `SelectQueryBuilder` also provides `count(session)` and `executePaged(session, { page, pageSize })` for common pagination patterns.
293
-
294
- #### Column pickers (preferred selection helpers)
295
-
296
- `defineTable` still exposes the full `table.columns` map for schema metadata and constraint tweaks, but modern queries usually benefit from higher-level helpers instead of spelling `todo.columns.*` everywhere.
297
-
298
- ```ts
299
- const t = tableRef(todos);
300
-
301
- const listOpenTodos = selectFrom(todos)
302
- .select('id', 'title', 'done') // typed shorthand for the same fields
303
- .where(eq(t.done, false))
304
- .orderBy(t.id, 'ASC');
305
- ```
306
-
307
- `select`, `include` (with `columns`), `includePick`, `selectColumnsDeep`, the `sel()` helpers for tables, and `esel()` for entities all build typed selection maps without repeating `table.columns.*`. Use those helpers when building query selections and reserve `table.columns.*` for schema definition, relations, or rare cases where you need a column reference outside of a picker. See the [Query Builder docs](./docs/query-builder.md#selection-helpers) for the reference, examples, and best practices for these helpers.
308
-
309
- #### Ergonomic column access (opt-in) with `tableRef`
310
-
311
- If you still want the convenience of accessing columns without spelling `.columns`, you can opt-in with `tableRef()`:
312
-
313
- ```ts
314
- import { tableRef, eq, selectFrom } from 'metal-orm';
315
-
316
- // Existing style (always works)
317
- const listOpenTodos = selectFrom(todos)
318
- .select('id', 'title', 'done')
319
- .where(eq(todos.columns.done, false))
320
- .orderBy(todos.columns.id, 'ASC');
321
-
322
- // Opt-in ergonomic style
323
- const t = tableRef(todos);
324
-
325
- const listOpenTodos2 = selectFrom(todos)
326
- .select('id', 'title', 'done')
327
- .where(eq(t.done, false))
328
- .orderBy(t.id, 'ASC');
329
- ```
330
-
331
- Collision rule: real table fields win.
332
-
333
- - `t.name` is the table name (string)
334
- - `t.$.name` is the column definition for a colliding column name (escape hatch)
335
-
336
- #### 2. Relations & hydration (still no ORM)
337
-
338
- Now add relations and get nested objects, still without committing to a runtime.
339
-
340
- ```ts
341
- import {
342
- defineTable,
343
- col,
344
- hasMany,
345
- selectFrom,
346
- eq,
347
- count,
348
- rowNumber,
349
- MySqlDialect,
350
- sel,
351
- hydrateRows,
352
- } from 'metal-orm';
353
-
354
- const posts = defineTable('posts', {
355
- id: col.primaryKey(col.int()),
356
- title: col.varchar(255),
357
- userId: col.int(),
358
- createdAt: col.timestamp(),
359
- });
360
-
361
- // Add constraints
362
- posts.columns.title.notNull = true;
363
- posts.columns.userId.notNull = true;
364
-
365
- const users = defineTable('users', {
366
- id: col.primaryKey(col.int()),
367
- name: col.varchar(255),
368
- email: col.varchar(255),
369
- });
370
-
371
- // Add relations and constraints
372
- users.relations = {
373
- posts: hasMany(posts, 'userId'),
374
- };
375
- users.columns.name.notNull = true;
376
- users.columns.email.unique = true;
377
-
378
- // Build a query with relation & window function
379
- const u = sel(users, 'id', 'name', 'email');
380
- const p = sel(posts, 'id', 'userId');
381
-
382
- const builder = selectFrom(users)
383
- .select({
384
- ...u,
385
- postCount: count(p.id),
386
- rank: rowNumber(), // window function helper
387
- })
388
- .leftJoin(posts, eq(p.userId, u.id))
389
- .groupBy(u.id)
390
- .groupBy(u.name)
391
- .groupBy(u.email)
392
- .orderBy(count(p.id), 'DESC')
393
- .limit(10)
394
- .includePick('posts', ['id', 'title', 'createdAt']); // eager relation for hydration
395
-
396
- const dialect = new MySqlDialect();
397
- const { sql, params } = builder.compile(dialect);
398
- const [rows] = await connection.execute(sql, params);
399
-
400
- // Turn flat rows into nested objects
401
- const hydrated = hydrateRows(
402
- rows as Record<string, unknown>[],
403
- builder.getHydrationPlan(),
404
- );
405
-
406
- console.log(hydrated);
407
- // [
408
- // {
409
- // id: 1,
410
- // name: 'John Doe',
411
- // email: 'john@example.com',
412
- // postCount: 15,
413
- // rank: 1,
414
- // posts: [
415
- // { id: 101, title: 'Latest Post', createdAt: '2023-05-15T10:00:00Z' },
416
- // // ...
417
- // ],
418
- // },
419
- // // ...
420
- // ]
421
- ```
422
-
423
- Use this mode anywhere you want powerful SQL + nice nested results, without changing how you manage your models.
424
-
425
- <a id="level-2"></a>
426
- ### Level 2: Entities + Unit of Work (ORM runtime) 🧠
427
-
428
- When you're ready, you can let MetalORM manage entities and relations for you.
429
-
430
- Instead of “naked objects”, your queries can return entities attached to an `OrmSession`:
431
-
432
- ```ts
433
- import mysql from 'mysql2/promise';
434
- import {
435
- Orm,
436
- OrmSession,
437
- MySqlDialect,
438
- selectFrom,
439
- eq,
440
- tableRef,
441
- createMysqlExecutor,
442
- } from 'metal-orm';
443
-
444
- // 1) Create an Orm + session for this request
445
-
446
- const connection = await mysql.createConnection({ /* ... */ });
447
- const executor = createMysqlExecutor(connection);
448
- const orm = new Orm({
449
- dialect: new MySqlDialect(),
450
- executorFactory: {
451
- createExecutor: () => executor,
452
- createTransactionalExecutor: () => executor,
453
- dispose: async () => {},
454
- },
455
- });
456
- const session = new OrmSession({ orm, executor });
457
-
458
- const u = tableRef(users);
459
-
460
- // 2) Load entities with lazy relations
461
- const [user] = await selectFrom(users)
462
- .select('id', 'name', 'email')
463
- .includeLazy('posts') // HasMany as a lazy collection
464
- .includeLazy('roles') // BelongsToMany as a lazy collection
465
- .where(eq(u.id, 1))
466
- .execute(session);
467
-
468
- // user is an EntityInstance<typeof users>
469
- // scalar props are normal:
470
- user.name = 'Updated Name'; // marks entity as Dirty
471
-
472
- // relations are live collections:
473
- const postsCollection = await user.posts.load(); // batched lazy load
474
- const newPost = user.posts.add({ title: 'Hello from ORM mode' });
475
-
476
- // Many-to-many via pivot:
477
- await user.roles.syncByIds([1, 2, 3]);
478
-
479
- // 3) Persist the entire graph
480
- await session.commit();
481
- // INSERT/UPDATE/DELETE + pivot updates happen in a single Unit of Work.
482
- ```
483
-
484
- What the runtime gives you:
485
-
486
- - [Identity map](https://en.wikipedia.org/wiki/Identity_map_pattern) (per context).
487
- - [Unit of Work](https://en.wikipedia.org/wiki/Unit_of_work) style change tracking on scalar properties.
488
- - Relation tracking (add/remove/sync on collections).
489
- - Cascades on relations: `'all' | 'persist' | 'remove' | 'link'`.
490
- - Single flush: `session.commit()` figures out inserts, updates, deletes, and pivot changes.
491
- - Column pickers to stay DRY: `select` on the root table, `include` (with `columns`) or `includePick` on relations, and `selectColumnsDeep` or the `sel`/`esel` helpers to build typed selection maps without repeating `table.columns.*`.
492
- - Tip: if you assign relations after `defineTable`, use `setRelations(table, { ... })` so TypeScript can validate `include(..., { columns: [...] })` and pivot columns. See `docs/query-builder.md`.
493
-
494
- <a id="level-3"></a>
495
- ### Level 3: Decorator entities ✨
496
-
497
- Finally, you can describe your models with decorators and still use the same runtime and query builder.
498
-
499
- The decorator layer is built on the TC39 Stage 3 standard (TypeScript 5.6+), so you simply decorate class fields (or accessors if you need custom logic) and the standard `ClassFieldDecoratorContext` keeps a metadata bag on `context.metadata`/`Symbol.metadata`. `@Entity` reads that bag when it runs and builds your `TableDef`s—no `experimentalDecorators`, parameter decorators, or extra polyfills required.
500
-
501
- ```ts
502
- import mysql from 'mysql2/promise';
503
- import {
504
- Orm,
505
- OrmSession,
506
- MySqlDialect,
507
- col,
508
- createMysqlExecutor,
509
- Entity,
510
- Column,
511
- PrimaryKey,
512
- HasMany,
513
- BelongsTo,
514
- bootstrapEntities,
515
- selectFromEntity,
516
- entityRef,
517
- eq,
518
- } from 'metal-orm';
519
-
520
- @Entity()
521
- class User {
522
- @PrimaryKey(col.int())
523
- id!: number;
524
-
525
- @Column(col.varchar(255))
526
- name!: string;
527
-
528
- @Column(col.varchar(255))
529
- email?: string;
530
-
531
- @HasMany({
532
- target: () => Post,
533
- foreignKey: 'userId',
534
- })
535
- posts!: any; // relation wrapper; type omitted for brevity
536
- }
537
-
538
- @Entity()
539
- class Post {
540
- @PrimaryKey(col.int())
541
- id!: number;
542
-
543
- @Column(col.varchar(255))
544
- title!: string;
545
-
546
- @Column(col.int())
547
- userId!: number;
548
-
549
- @BelongsTo({
550
- target: () => User,
551
- foreignKey: 'userId',
552
- })
553
- user!: any;
554
- }
555
-
556
- // 1) Bootstrap metadata once at startup (recommended so you reuse the same TableDefs)
557
- const tables = bootstrapEntities(); // getTableDefFromEntity/selectFromEntity can bootstrap lazily if you forget
558
- // tables: TableDef[] – compatible with the rest of MetalORM
559
-
560
- // 2) Create an Orm + session
561
- const connection = await mysql.createConnection({ /* ... */ });
562
- const executor = createMysqlExecutor(connection);
563
- const orm = new Orm({
564
- dialect: new MySqlDialect(),
565
- executorFactory: {
566
- createExecutor: () => executor,
567
- createTransactionalExecutor: () => executor,
568
- dispose: async () => {},
569
- },
570
- });
571
- const session = new OrmSession({ orm, executor });
572
-
573
- // 3) Query starting from the entity class
574
- const U = entityRef(User);
575
- const [user] = await selectFromEntity(User)
576
- .select('id', 'name')
577
- .includeLazy('posts')
578
- .where(eq(U.id, 1))
579
- .execute(session); // user is an actual instance of the User class!
580
-
581
- // Use executePlain() if you want raw POJOs instead of class instances
582
- // Return type is inferred from selected columns: { id: number; name: string }[]
583
- const rawUsers = await selectFromEntity(User)
584
- .select('id', 'name')
585
- .executePlain(session);
586
-
587
- user.posts.add({ title: 'From decorators' });
588
- await session.commit();
589
- ```
590
-
591
- Note: relation helpers like `add`/`attach` are only available on tracked entities returned by `execute(session)`. `executePlain()` returns POJOs without relation wrappers, with return types inferred from your `.select()` calls—no manual casting needed. Make sure the primary key (e.g. `id`) is selected so relation adds can link correctly.
592
-
593
- Tip: to keep selections terse, use `select`, `include` (with `columns`), or the `sel`/`esel` helpers instead of spelling `table.columns.*` over and over. By default, `selectFromEntity` selects all columns if you don't specify any.
594
-
595
-
596
- This level is nice when:
597
-
598
- - You want classes as your domain model, but don't want a separate schema DSL.
599
- - You like decorators for explicit mapping but still want AST-first SQL and a disciplined runtime.
600
-
601
- ---
602
-
603
- <a id="when-to-use-which-level"></a>
604
- ## When to use which level? 🤔
605
-
606
- - **Query builder + hydration (Level 1)**
607
- Great for reporting/analytics, existing codebases with their own models, and services that need strong SQL but minimal runtime magic.
608
-
609
- - **ORM runtime (Level 2)**
610
- Great for request-scoped application logic and domain modeling where lazy relations, cascades, and graph persistence pay off.
611
-
612
- - **Decorator entities (Level 3)**
613
- Great when you want class-based entities and decorators, but still want to keep the underlying architecture explicit and layered.
614
-
615
- All three levels share the same schema, AST, and dialects, so you can mix them as needed and migrate gradually.
616
-
617
- ---
618
-
619
- <a id="design-notes"></a>
620
- ## Design & Architecture 🏗️
621
-
622
- MetalORM is built on solid software engineering principles and proven design patterns.
623
-
624
- ### Architecture Layers
625
-
626
- ```
627
- ┌─────────────────────────────────────────────────┐
628
- │ Your Application │
629
- └─────────────────────────────────────────────────┘
630
-
631
- ┌──────────────────┼──────────────────┐
632
- │ │ │
633
- ▼ ▼ ▼
634
- ┌─────────┐ ┌──────────┐ ┌──────────┐
635
- │ Level 1 │ │ Level 2 │ │ Level 3 │
636
- │ Query │◄─────┤ ORM │◄─────┤Decorators│
637
- │ Builder │ │ Runtime │ │ │
638
- └─────────┘ └──────────┘ └──────────┘
639
- │ │ │
640
- └──────────────────┼──────────────────┘
641
-
642
- ┌────────────────┐
643
- │ SQL AST │
644
- │ (Typed Nodes) │
645
- └────────────────┘
646
-
647
- ┌────────────────────────────────────────────────┐
648
- │ Strategy Pattern: Dialects │
649
- MySQL | PostgreSQL | SQLite | SQL Server
650
- └────────────────────────────────────────────────┘
651
-
652
- ┌────────────────┐
653
- Database
654
- └────────────────┘
655
- ```
656
-
657
- ### Design Patterns
658
-
659
- - **Strategy Pattern**: Pluggable dialects (MySQL, PostgreSQL, SQLite, SQL Server) and function renderers allow the same query to target different databases
660
- - **Visitor Pattern**: AST traversal for SQL compilation and expression processing
661
- - **Builder Pattern**: Fluent query builders (Select, Insert, Update, Delete) for constructing queries step-by-step
662
- - **Factory Pattern**: Dialect factory and executor creation abstract instantiation logic
663
- - **Unit of Work**: Change tracking and batch persistence in `OrmSession` coordinate all modifications
664
- - **Identity Map**: One entity instance per row within a session prevents duplicate object issues
665
- - **Interceptor/Pipeline**: Query interceptors and flush lifecycle hooks enable cross-cutting concerns
666
- - **Adapter Pattern**: Connection pooling adapters allow different pool implementations
667
-
668
- ### Type Safety
669
-
670
- - **Zero `any` types**: The entire src codebase contains zero `any` types—every value is properly typed
671
- - **100% typed public API**: Every public method, parameter, and return value is fully typed
672
- - **Full type inference**: From schema definition through query building to result hydration
673
- - **Compile-time safety**: Catch SQL errors at TypeScript compile time, not runtime
674
- - **Generic-driven**: Leverages TypeScript generics extensively for type propagation
675
-
676
- ### Separation of Concerns
677
-
678
- Each layer has a clear, focused responsibility:
679
-
680
- - **Core AST layer**: SQL representation independent of any specific dialect
681
- - **Dialect layer**: Vendor-specific SQL compilation (MySQL, PostgreSQL, etc.)
682
- - **Schema layer**: Table and column definitions with relations
683
- - **Query builder layer**: Fluent API for building type-safe queries
684
- - **Hydration layer**: Transforms flat result sets into nested object graphs
685
- - **ORM runtime layer**: Entity management, change tracking, lazy relations, transactions
686
-
687
- You can use just the layers you need and stay at the low level (AST + dialects) or adopt higher levels when beneficial.
688
-
689
- ---
690
-
691
- ## Frequently Asked Questions
692
-
693
- **Q: How does MetalORM differ from other ORMs?**
694
- A: MetalORM's unique three-level architecture lets you choose your abstraction level—use just the query builder, add the ORM runtime when needed, or go full decorator-based entities. This gradual adoption path is uncommon in the TypeScript ecosystem. You're not locked into an all-or-nothing ORM approach.
695
-
696
- **Q: Can I use this in production?**
697
- A: Yes! MetalORM is designed for production use with robust patterns like Unit of Work, Identity Map, and connection pooling support. The type-safe query builder ensures SQL correctness at compile time.
698
-
699
- **Q: Do I need to use all three levels?**
700
- A: No! Use only what you need. Many projects stay at Level 1 (query builder) for its type-safe SQL building without any ORM overhead. Add runtime features (Level 2) or decorators (Level 3) only where they provide value.
701
-
702
- **Q: What about migrations?**
703
- A: MetalORM provides schema generation via DDL builders. See the [Schema Generation docs](./docs/schema-generation.md) for details on generating CREATE TABLE statements from your table definitions.
704
-
705
- **Q: How type-safe is it really?**
706
- A: Exceptionally. The entire codebase contains **zero** `any` types—every value is properly typed with TypeScript generics and inference. All public APIs are fully typed, and your queries, entities, and results get full TypeScript checking at compile time.
707
-
708
- **Q: What design patterns are used?**
709
- A: MetalORM implements several well-known patterns: Strategy (dialects & functions), Visitor (AST traversal), Builder (query construction), Factory (dialect & executor creation), Unit of Work (change tracking), Identity Map (entity caching), Interceptor (query hooks), and Adapter (pooling). This makes the codebase maintainable and extensible.
710
-
711
- ---
712
-
713
- ## Performance & Production 🚀
714
-
715
- - **Zero runtime overhead for Level 1** (query builder) - it's just SQL compilation and hydration
716
- - **Efficient batching** for Level 2 lazy relations minimizes database round-trips
717
- - **Identity Map** prevents duplicate entity instances and unnecessary queries
718
- - **Connection pooling** supported via executor factory pattern (see [pooling docs](./docs/pooling.md))
719
- - **Prepared statements** with parameterized queries protect against SQL injection
720
-
721
- **Production checklist:**
722
- - ✅ Use connection pooling for better resource management
723
- - Enable query logging in development for debugging
724
- - ✅ Set up proper error handling and retries
725
- - Use transactions for multi-statement operations
726
- - Monitor query performance with interceptors
727
-
728
- ---
729
-
730
- ## Community & Support 💬
731
-
732
- - 🐛 **Issues:** [GitHub Issues](https://github.com/celsowm/metal-orm/issues)
733
- - 💡 **Discussions:** [GitHub Discussions](https://github.com/celsowm/metal-orm/discussions)
734
- - 📖 **Documentation:** [Full docs](./docs/index.md)
735
- - 🗺️ **Roadmap:** [See what's planned](./ROADMAP.md)
736
- - 📦 **Changelog:** [View releases](https://github.com/celsowm/metal-orm/releases)
737
-
738
- ---
739
-
740
- <a id="contributing"></a>
741
- ## Contributing 🤝
742
-
743
- Issues and PRs are welcome! If you're interested in pushing the runtime/ORM side further (soft deletes, multi-tenant filters, outbox patterns, etc.), contributions are especially appreciated.
744
-
745
- See the contributing guide for details.
746
-
747
- ---
748
-
749
- <a id="license"></a>
750
- ## License 📄
751
-
752
- MetalORM is MIT licensed.
218
+
219
+ ### Playground (optional) 🧪
220
+
221
+ The React playground lives in `playground/` and is no longer part of the published package or its dependency tree. To run it locally:
222
+
223
+ 1. `cd playground && npm install`
224
+ 2. `npm run dev` (uses the root `vite.config.ts`)
225
+
226
+ It boots against an in-memory SQLite database seeded from fixtures under `playground/shared/`.
227
+
228
+ ---
229
+
230
+ <a id="quick-start"></a>
231
+ ## Quick start – three levels
232
+
233
+ <a id="level-1"></a>
234
+ ### Level 1: Query builder & hydration 🧩
235
+
236
+ #### 1. Tiny table, tiny query
237
+
238
+ MetalORM can be just a straightforward query builder.
239
+
240
+ ```ts
241
+ import mysql from 'mysql2/promise';
242
+ import {
243
+ defineTable,
244
+ tableRef,
245
+ col,
246
+ selectFrom,
247
+ eq,
248
+ MySqlDialect,
249
+ } from 'metal-orm';
250
+
251
+ // 1) A very small table
252
+ const todos = defineTable('todos', {
253
+ id: col.primaryKey(col.int()),
254
+ title: col.varchar(255),
255
+ done: col.boolean(),
256
+ });
257
+ // Add constraints
258
+ todos.columns.title.notNull = true;
259
+ todos.columns.done.default = false;
260
+
261
+ // Optional: opt-in ergonomic column access
262
+ const t = tableRef(todos);
263
+
264
+ // 2) Build a simple query
265
+ const listOpenTodos = selectFrom(todos)
266
+ .select('id', 'title', 'done')
267
+ .where(eq(t.done, false))
268
+ .orderBy(t.id, 'ASC');
269
+
270
+ // 3) Compile to SQL + params
271
+ const dialect = new MySqlDialect();
272
+ const { sql, params } = listOpenTodos.compile(dialect);
273
+
274
+ // 4) Run with your favorite driver
275
+ const connection = await mysql.createConnection({ /* ... */ });
276
+ const [rows] = await connection.execute(sql, params);
277
+
278
+ console.log(rows);
279
+ // [
280
+ // { id: 1, title: 'Write docs', done: 0 },
281
+ // { id: 2, title: 'Ship feature', done: 0 },
282
+ // ]
283
+ ```
284
+
285
+ If you keep a reusable array of column names (e.g. shared across helpers or pulled from config), you can spread it into `.select(...)` since the method accepts rest arguments:
286
+
287
+ ```ts
288
+ const defaultColumns = ['id', 'title', 'done'] as const;
289
+ const listOpenTodos = selectFrom(todos).select(...defaultColumns);
290
+ ```
291
+
292
+ That's it: schema, query, SQL, done.
293
+
294
+ If you are using the Level 2 runtime (`OrmSession`), `SelectQueryBuilder` also provides `count(session)` and `executePaged(session, { page, pageSize })` for common pagination patterns.
295
+
296
+ #### Column pickers (preferred selection helpers)
297
+
298
+ `defineTable` still exposes the full `table.columns` map for schema metadata and constraint tweaks, but modern queries usually benefit from higher-level helpers instead of spelling `todo.columns.*` everywhere.
299
+
300
+ ```ts
301
+ const t = tableRef(todos);
302
+
303
+ const listOpenTodos = selectFrom(todos)
304
+ .select('id', 'title', 'done') // typed shorthand for the same fields
305
+ .where(eq(t.done, false))
306
+ .orderBy(t.id, 'ASC');
307
+ ```
308
+
309
+ `select`, `include` (with `columns`), `includePick`, `selectColumnsDeep`, the `sel()` helpers for tables, and `esel()` for entities all build typed selection maps without repeating `table.columns.*`. Use those helpers when building query selections and reserve `table.columns.*` for schema definition, relations, or rare cases where you need a column reference outside of a picker. See the [Query Builder docs](./docs/query-builder.md#selection-helpers) for the reference, examples, and best practices for these helpers.
310
+
311
+ #### Ergonomic column access (opt-in) with `tableRef`
312
+
313
+ If you still want the convenience of accessing columns without spelling `.columns`, you can opt-in with `tableRef()`:
314
+
315
+ ```ts
316
+ import { tableRef, eq, selectFrom } from 'metal-orm';
317
+
318
+ // Existing style (always works)
319
+ const listOpenTodos = selectFrom(todos)
320
+ .select('id', 'title', 'done')
321
+ .where(eq(todos.columns.done, false))
322
+ .orderBy(todos.columns.id, 'ASC');
323
+
324
+ // Opt-in ergonomic style
325
+ const t = tableRef(todos);
326
+
327
+ const listOpenTodos2 = selectFrom(todos)
328
+ .select('id', 'title', 'done')
329
+ .where(eq(t.done, false))
330
+ .orderBy(t.id, 'ASC');
331
+ ```
332
+
333
+ Collision rule: real table fields win.
334
+
335
+ - `t.name` is the table name (string)
336
+ - `t.$.name` is the column definition for a colliding column name (escape hatch)
337
+
338
+ #### 2. Relations & hydration (still no ORM)
339
+
340
+ Now add relations and get nested objects, still without committing to a runtime.
341
+
342
+ ```ts
343
+ import {
344
+ defineTable,
345
+ col,
346
+ hasMany,
347
+ selectFrom,
348
+ eq,
349
+ count,
350
+ rowNumber,
351
+ MySqlDialect,
352
+ sel,
353
+ hydrateRows,
354
+ } from 'metal-orm';
355
+
356
+ const posts = defineTable('posts', {
357
+ id: col.primaryKey(col.int()),
358
+ title: col.varchar(255),
359
+ userId: col.int(),
360
+ createdAt: col.timestamp(),
361
+ });
362
+
363
+ // Add constraints
364
+ posts.columns.title.notNull = true;
365
+ posts.columns.userId.notNull = true;
366
+
367
+ const users = defineTable('users', {
368
+ id: col.primaryKey(col.int()),
369
+ name: col.varchar(255),
370
+ email: col.varchar(255),
371
+ });
372
+
373
+ // Add relations and constraints
374
+ users.relations = {
375
+ posts: hasMany(posts, 'userId'),
376
+ };
377
+ users.columns.name.notNull = true;
378
+ users.columns.email.unique = true;
379
+
380
+ // Build a query with relation & window function
381
+ const u = sel(users, 'id', 'name', 'email');
382
+ const p = sel(posts, 'id', 'userId');
383
+
384
+ const builder = selectFrom(users)
385
+ .select({
386
+ ...u,
387
+ postCount: count(p.id),
388
+ rank: rowNumber(), // window function helper
389
+ })
390
+ .leftJoin(posts, eq(p.userId, u.id))
391
+ .groupBy(u.id)
392
+ .groupBy(u.name)
393
+ .groupBy(u.email)
394
+ .orderBy(count(p.id), 'DESC')
395
+ .limit(10)
396
+ .includePick('posts', ['id', 'title', 'createdAt']); // eager relation for hydration
397
+
398
+ const dialect = new MySqlDialect();
399
+ const { sql, params } = builder.compile(dialect);
400
+ const [rows] = await connection.execute(sql, params);
401
+
402
+ // Turn flat rows into nested objects
403
+ const hydrated = hydrateRows(
404
+ rows as Record<string, unknown>[],
405
+ builder.getHydrationPlan(),
406
+ );
407
+
408
+ console.log(hydrated);
409
+ // [
410
+ // {
411
+ // id: 1,
412
+ // name: 'John Doe',
413
+ // email: 'john@example.com',
414
+ // postCount: 15,
415
+ // rank: 1,
416
+ // posts: [
417
+ // { id: 101, title: 'Latest Post', createdAt: '2023-05-15T10:00:00Z' },
418
+ // // ...
419
+ // ],
420
+ // },
421
+ // // ...
422
+ // ]
423
+ ```
424
+
425
+ Use this mode anywhere you want powerful SQL + nice nested results, without changing how you manage your models.
426
+
427
+ <a id="level-2"></a>
428
+ ### Level 2: Entities + Unit of Work (ORM runtime) 🧠
429
+
430
+ When you're ready, you can let MetalORM manage entities and relations for you.
431
+
432
+ Instead of “naked objects”, your queries can return entities attached to an `OrmSession`:
433
+
434
+ ```ts
435
+ import mysql from 'mysql2/promise';
436
+ import {
437
+ Orm,
438
+ OrmSession,
439
+ MySqlDialect,
440
+ selectFrom,
441
+ eq,
442
+ tableRef,
443
+ createMysqlExecutor,
444
+ } from 'metal-orm';
445
+
446
+ // 1) Create an Orm + session for this request
447
+
448
+ const connection = await mysql.createConnection({ /* ... */ });
449
+ const executor = createMysqlExecutor(connection);
450
+ const orm = new Orm({
451
+ dialect: new MySqlDialect(),
452
+ executorFactory: {
453
+ createExecutor: () => executor,
454
+ createTransactionalExecutor: () => executor,
455
+ dispose: async () => {},
456
+ },
457
+ });
458
+ const session = new OrmSession({ orm, executor });
459
+
460
+ const u = tableRef(users);
461
+
462
+ // 2) Load entities with lazy relations
463
+ const [user] = await selectFrom(users)
464
+ .select('id', 'name', 'email')
465
+ .includeLazy('posts') // HasMany as a lazy collection
466
+ .includeLazy('roles') // BelongsToMany as a lazy collection
467
+ .where(eq(u.id, 1))
468
+ .execute(session);
469
+
470
+ // user is an EntityInstance<typeof users>
471
+ // scalar props are normal:
472
+ user.name = 'Updated Name'; // marks entity as Dirty
473
+
474
+ // relations are live collections:
475
+ const postsCollection = await user.posts.load(); // batched lazy load
476
+ const newPost = user.posts.add({ title: 'Hello from ORM mode' });
477
+
478
+ // Many-to-many via pivot:
479
+ await user.roles.syncByIds([1, 2, 3]);
480
+
481
+ // 3) Persist the entire graph
482
+ await session.commit();
483
+ // INSERT/UPDATE/DELETE + pivot updates happen in a single Unit of Work.
484
+ ```
485
+
486
+ What the runtime gives you:
487
+
488
+ - [Identity map](https://en.wikipedia.org/wiki/Identity_map_pattern) (per context).
489
+ - [Unit of Work](https://en.wikipedia.org/wiki/Unit_of_work) style change tracking on scalar properties.
490
+ - Relation tracking (add/remove/sync on collections).
491
+ - Cascades on relations: `'all' | 'persist' | 'remove' | 'link'`.
492
+ - Single flush: `session.commit()` figures out inserts, updates, deletes, and pivot changes.
493
+ - Column pickers to stay DRY: `select` on the root table, `include` (with `columns`) or `includePick` on relations, and `selectColumnsDeep` or the `sel`/`esel` helpers to build typed selection maps without repeating `table.columns.*`.
494
+ - Tip: if you assign relations after `defineTable`, use `setRelations(table, { ... })` so TypeScript can validate `include(..., { columns: [...] })` and pivot columns. See `docs/query-builder.md`.
495
+
496
+ <a id="level-3"></a>
497
+ ### Level 3: Decorator entities ✨
498
+
499
+ Finally, you can describe your models with decorators and still use the same runtime and query builder.
500
+
501
+ The decorator layer is built on the TC39 Stage 3 standard (TypeScript 5.6+), so you simply decorate class fields (or accessors if you need custom logic) and the standard `ClassFieldDecoratorContext` keeps a metadata bag on `context.metadata`/`Symbol.metadata`. `@Entity` reads that bag when it runs and builds your `TableDef`s—no `experimentalDecorators`, parameter decorators, or extra polyfills required.
502
+
503
+ ```ts
504
+ import mysql from 'mysql2/promise';
505
+ import {
506
+ Orm,
507
+ OrmSession,
508
+ MySqlDialect,
509
+ col,
510
+ createMysqlExecutor,
511
+ Entity,
512
+ Column,
513
+ PrimaryKey,
514
+ HasMany,
515
+ BelongsTo,
516
+ bootstrapEntities,
517
+ selectFromEntity,
518
+ entityRef,
519
+ eq,
520
+ } from 'metal-orm';
521
+
522
+ @Entity()
523
+ class User {
524
+ @PrimaryKey(col.int())
525
+ id!: number;
526
+
527
+ @Column(col.varchar(255))
528
+ name!: string;
529
+
530
+ @Column(col.varchar(255))
531
+ email?: string;
532
+
533
+ @HasMany({
534
+ target: () => Post,
535
+ foreignKey: 'userId',
536
+ })
537
+ posts!: any; // relation wrapper; type omitted for brevity
538
+ }
539
+
540
+ @Entity()
541
+ class Post {
542
+ @PrimaryKey(col.int())
543
+ id!: number;
544
+
545
+ @Column(col.varchar(255))
546
+ title!: string;
547
+
548
+ @Column(col.int())
549
+ userId!: number;
550
+
551
+ @BelongsTo({
552
+ target: () => User,
553
+ foreignKey: 'userId',
554
+ })
555
+ user!: any;
556
+ }
557
+
558
+ // 1) Bootstrap metadata once at startup (recommended so you reuse the same TableDefs)
559
+ const tables = bootstrapEntities(); // getTableDefFromEntity/selectFromEntity can bootstrap lazily if you forget
560
+ // tables: TableDef[] – compatible with the rest of MetalORM
561
+
562
+ // 2) Create an Orm + session
563
+ const connection = await mysql.createConnection({ /* ... */ });
564
+ const executor = createMysqlExecutor(connection);
565
+ const orm = new Orm({
566
+ dialect: new MySqlDialect(),
567
+ executorFactory: {
568
+ createExecutor: () => executor,
569
+ createTransactionalExecutor: () => executor,
570
+ dispose: async () => {},
571
+ },
572
+ });
573
+ const session = new OrmSession({ orm, executor });
574
+
575
+ // 3) Query starting from the entity class
576
+ const U = entityRef(User);
577
+ const [user] = await selectFromEntity(User)
578
+ .select('id', 'name')
579
+ .includeLazy('posts')
580
+ .where(eq(U.id, 1))
581
+ .execute(session); // user is an actual instance of the User class!
582
+
583
+ // Use executePlain() if you want raw POJOs instead of class instances
584
+ // Return type is inferred from selected columns: { id: number; name: string }[]
585
+ const rawUsers = await selectFromEntity(User)
586
+ .select('id', 'name')
587
+ .executePlain(session);
588
+
589
+ // Use firstOrFail() to get a single record or throw if not found
590
+ const admin = await selectFromEntity(User)
591
+ .where(eq(U.role, 'admin'))
592
+ .firstOrFail(session); // throws Error('No results found') if no match
593
+
594
+ // firstOrFailPlain() works the same but returns a POJO
595
+ const adminPlain = await selectFromEntity(User)
596
+ .where(eq(U.role, 'admin'))
597
+ .firstOrFailPlain(session);
598
+
599
+ user.posts.add({ title: 'From decorators' });
600
+ await session.commit();
601
+ ```
602
+
603
+ Note: relation helpers like `add`/`attach` are only available on tracked entities returned by `execute(session)`. `executePlain()` returns POJOs without relation wrappers, with return types inferred from your `.select()` calls—no manual casting needed. Make sure the primary key (e.g. `id`) is selected so relation adds can link correctly.
604
+
605
+ Tip: to keep selections terse, use `select`, `include` (with `columns`), or the `sel`/`esel` helpers instead of spelling `table.columns.*` over and over. By default, `selectFromEntity` selects all columns if you don't specify any.
606
+
607
+
608
+ This level is nice when:
609
+
610
+ - You want classes as your domain model, but don't want a separate schema DSL.
611
+ - You like decorators for explicit mapping but still want AST-first SQL and a disciplined runtime.
612
+
613
+ ---
614
+
615
+ <a id="when-to-use-which-level"></a>
616
+ ## When to use which level? 🤔
617
+
618
+ - **Query builder + hydration (Level 1)**
619
+ Great for reporting/analytics, existing codebases with their own models, and services that need strong SQL but minimal runtime magic.
620
+
621
+ - **ORM runtime (Level 2)**
622
+ Great for request-scoped application logic and domain modeling where lazy relations, cascades, and graph persistence pay off.
623
+
624
+ - **Decorator entities (Level 3)**
625
+ Great when you want class-based entities and decorators, but still want to keep the underlying architecture explicit and layered.
626
+
627
+ All three levels share the same schema, AST, and dialects, so you can mix them as needed and migrate gradually.
628
+
629
+ ---
630
+
631
+ <a id="design-notes"></a>
632
+ ## Design & Architecture 🏗️
633
+
634
+ MetalORM is built on solid software engineering principles and proven design patterns.
635
+
636
+ ### Architecture Layers
637
+
638
+ ```
639
+ ┌─────────────────────────────────────────────────┐
640
+ │ Your Application │
641
+ └─────────────────────────────────────────────────┘
642
+
643
+ ┌──────────────────┼──────────────────┐
644
+ │ │ │
645
+ ▼ ▼ ▼
646
+ ┌─────────┐ ┌──────────┐ ┌──────────┐
647
+ │ Level 1 │ │ Level 2 │ │ Level 3 │
648
+ │ Query │◄─────┤ ORM │◄─────┤Decorators│
649
+ │ Builder │ │ Runtime │ │ │
650
+ └─────────┘ └──────────┘ └──────────┘
651
+
652
+ └──────────────────┼──────────────────┘
653
+
654
+ ┌────────────────┐
655
+ SQL AST
656
+ │ (Typed Nodes) │
657
+ └────────────────┘
658
+
659
+ ┌────────────────────────────────────────────────┐
660
+ │ Strategy Pattern: Dialects │
661
+ │ MySQL | PostgreSQL | SQLite | SQL Server
662
+ └────────────────────────────────────────────────┘
663
+
664
+ ┌────────────────┐
665
+ │ Database │
666
+ └────────────────┘
667
+ ```
668
+
669
+ ### Design Patterns
670
+
671
+ - **Strategy Pattern**: Pluggable dialects (MySQL, PostgreSQL, SQLite, SQL Server) and function renderers allow the same query to target different databases
672
+ - **Visitor Pattern**: AST traversal for SQL compilation and expression processing
673
+ - **Builder Pattern**: Fluent query builders (Select, Insert, Update, Delete) for constructing queries step-by-step
674
+ - **Factory Pattern**: Dialect factory and executor creation abstract instantiation logic
675
+ - **Unit of Work**: Change tracking and batch persistence in `OrmSession` coordinate all modifications
676
+ - **Identity Map**: One entity instance per row within a session prevents duplicate object issues
677
+ - **Interceptor/Pipeline**: Query interceptors and flush lifecycle hooks enable cross-cutting concerns
678
+ - **Adapter Pattern**: Connection pooling adapters allow different pool implementations
679
+
680
+ ### Type Safety
681
+
682
+ - **Zero `any` types**: The entire src codebase contains zero `any` types—every value is properly typed
683
+ - **100% typed public API**: Every public method, parameter, and return value is fully typed
684
+ - **Full type inference**: From schema definition through query building to result hydration
685
+ - **Compile-time safety**: Catch SQL errors at TypeScript compile time, not runtime
686
+ - **Generic-driven**: Leverages TypeScript generics extensively for type propagation
687
+
688
+ ### Separation of Concerns
689
+
690
+ Each layer has a clear, focused responsibility:
691
+
692
+ - **Core AST layer**: SQL representation independent of any specific dialect
693
+ - **Dialect layer**: Vendor-specific SQL compilation (MySQL, PostgreSQL, etc.)
694
+ - **Schema layer**: Table and column definitions with relations
695
+ - **Query builder layer**: Fluent API for building type-safe queries
696
+ - **Hydration layer**: Transforms flat result sets into nested object graphs
697
+ - **ORM runtime layer**: Entity management, change tracking, lazy relations, transactions
698
+
699
+ You can use just the layers you need and stay at the low level (AST + dialects) or adopt higher levels when beneficial.
700
+
701
+ ---
702
+
703
+ ## Frequently Asked Questions ❓
704
+
705
+ **Q: How does MetalORM differ from other ORMs?**
706
+ A: MetalORM's unique three-level architecture lets you choose your abstraction level—use just the query builder, add the ORM runtime when needed, or go full decorator-based entities. This gradual adoption path is uncommon in the TypeScript ecosystem. You're not locked into an all-or-nothing ORM approach.
707
+
708
+ **Q: Can I use this in production?**
709
+ A: Yes! MetalORM is designed for production use with robust patterns like Unit of Work, Identity Map, and connection pooling support. The type-safe query builder ensures SQL correctness at compile time.
710
+
711
+ **Q: Do I need to use all three levels?**
712
+ A: No! Use only what you need. Many projects stay at Level 1 (query builder) for its type-safe SQL building without any ORM overhead. Add runtime features (Level 2) or decorators (Level 3) only where they provide value.
713
+
714
+ **Q: What about migrations?**
715
+ A: MetalORM provides schema generation via DDL builders. See the [Schema Generation docs](./docs/schema-generation.md) for details on generating CREATE TABLE statements from your table definitions.
716
+
717
+ **Q: How type-safe is it really?**
718
+ A: Exceptionally. The entire codebase contains **zero** `any` types—every value is properly typed with TypeScript generics and inference. All public APIs are fully typed, and your queries, entities, and results get full TypeScript checking at compile time.
719
+
720
+ **Q: What design patterns are used?**
721
+ A: MetalORM implements several well-known patterns: Strategy (dialects & functions), Visitor (AST traversal), Builder (query construction), Factory (dialect & executor creation), Unit of Work (change tracking), Identity Map (entity caching), Interceptor (query hooks), and Adapter (pooling). This makes the codebase maintainable and extensible.
722
+
723
+ ---
724
+
725
+ ## Performance & Production 🚀
726
+
727
+ - **Zero runtime overhead for Level 1** (query builder) - it's just SQL compilation and hydration
728
+ - **Efficient batching** for Level 2 lazy relations minimizes database round-trips
729
+ - **Identity Map** prevents duplicate entity instances and unnecessary queries
730
+ - **Connection pooling** supported via executor factory pattern (see [pooling docs](./docs/pooling.md))
731
+ - **Prepared statements** with parameterized queries protect against SQL injection
732
+
733
+ **Production checklist:**
734
+ - Use connection pooling for better resource management
735
+ - Enable query logging in development for debugging
736
+ - Set up proper error handling and retries
737
+ - Use transactions for multi-statement operations
738
+ - Monitor query performance with interceptors
739
+
740
+ ---
741
+
742
+ ## Community & Support 💬
743
+
744
+ - 🐛 **Issues:** [GitHub Issues](https://github.com/celsowm/metal-orm/issues)
745
+ - 💡 **Discussions:** [GitHub Discussions](https://github.com/celsowm/metal-orm/discussions)
746
+ - 📖 **Documentation:** [Full docs](./docs/index.md)
747
+ - 🗺️ **Roadmap:** [See what's planned](./ROADMAP.md)
748
+ - 📦 **Changelog:** [View releases](https://github.com/celsowm/metal-orm/releases)
749
+
750
+ ---
751
+
752
+ <a id="contributing"></a>
753
+ ## Contributing 🤝
754
+
755
+ Issues and PRs are welcome! If you're interested in pushing the runtime/ORM side further (soft deletes, multi-tenant filters, outbox patterns, etc.), contributions are especially appreciated.
756
+
757
+ See the contributing guide for details.
758
+
759
+ ---
760
+
761
+ <a id="license"></a>
762
+ ## License 📄
763
+
764
+ MetalORM is MIT licensed.