metal-orm 1.0.30 → 1.0.32

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
@@ -11,7 +11,7 @@ MetalORM is a TypeScript-first, AST-driven SQL toolkit you can dial up or down d
11
11
  - **Level 2 – ORM runtime (entities + Unit of Work 🧠)**
12
12
  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()`.
13
13
  - **Level 3 – Decorator entities (classes + metadata ✨)**
14
- Use `@Entity`, `@Column`, `@PrimaryKey`, relation decorators, `bootstrapEntities()` and `selectFromEntity()` to describe your model classes. MetalORM bootstraps schema & relations from metadata and plugs them into the same runtime and query builder.
14
+ 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.
15
15
 
16
16
  Use only the layer you need in each part of your codebase.
17
17
 
@@ -102,11 +102,11 @@ If you like explicit model classes, you can add a thin decorator layer on top of
102
102
  - `@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).
103
103
  - `@Column(...)` and `@PrimaryKey(...)` on properties; decorators collect column metadata and later build `TableDef`s from it.
104
104
  - Relation decorators:
105
- - `@HasMany({ target, foreignKey, ... })`
106
- - `@HasOne({ target, foreignKey, ... })`
107
- - `@BelongsTo({ target, foreignKey, ... })`
105
+ - `@HasMany({ target, foreignKey, ... })`
106
+ - `@HasOne({ target, foreignKey, ... })`
107
+ - `@BelongsTo({ target, foreignKey, ... })`
108
108
  - `@BelongsToMany({ target, pivotTable, ... })`
109
- - `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.
109
+ - `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.)
110
110
  - `selectFromEntity(MyEntity)` lets you start a `SelectQueryBuilder` directly from the class.
111
111
  - **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.
112
112
 
@@ -416,8 +416,8 @@ class Post {
416
416
  user!: any;
417
417
  }
418
418
 
419
- // 1) Bootstrap metadata once at startup
420
- const tables = bootstrapEntities();
419
+ // 1) Bootstrap metadata once at startup (recommended so you reuse the same TableDefs)
420
+ const tables = bootstrapEntities(); // getTableDefFromEntity/selectFromEntity can bootstrap lazily if you forget
421
421
  // tables: TableDef[] – compatible with the rest of MetalORM
422
422
 
423
423
  // 2) Create an Orm + session
@@ -4940,12 +4940,15 @@ var bootstrapEntities = () => {
4940
4940
  var getTableDefFromEntity = (ctor) => {
4941
4941
  const meta = getEntityMetadata(ctor);
4942
4942
  if (!meta) return void 0;
4943
+ if (!meta.table) {
4944
+ bootstrapEntities();
4945
+ }
4943
4946
  return meta.table;
4944
4947
  };
4945
4948
  var selectFromEntity = (ctor) => {
4946
4949
  const table = getTableDefFromEntity(ctor);
4947
4950
  if (!table) {
4948
- throw new Error("Entity metadata has not been bootstrapped");
4951
+ throw new Error(`Entity '${ctor.name}' is not registered with decorators or has not been bootstrapped`);
4949
4952
  }
4950
4953
  return new SelectQueryBuilder(table);
4951
4954
  };