metal-orm 1.0.35 → 1.0.37

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
@@ -373,7 +373,9 @@ type RelationTargetTable<TRel extends RelationDef> = TRel extends HasManyRelatio
373
373
  /**
374
374
  * Maps a ColumnDef to its TypeScript type representation
375
375
  */
376
- type ColumnToTs<T extends ColumnDef> = T['tsType'] extends undefined ? T['type'] extends 'INT' | 'INTEGER' | 'int' | 'integer' ? number : T['type'] extends 'BIGINT' | 'bigint' ? number | bigint : T['type'] extends 'DECIMAL' | 'decimal' | 'FLOAT' | 'float' | 'DOUBLE' | 'double' ? number : T['type'] extends 'BOOLEAN' | 'boolean' ? boolean : T['type'] extends 'JSON' | 'json' ? unknown : T['type'] extends 'BLOB' | 'blob' | 'BINARY' | 'binary' | 'VARBINARY' | 'varbinary' | 'BYTEA' | 'bytea' ? Buffer : T['type'] extends 'DATE' | 'date' | 'DATETIME' | 'datetime' | 'TIMESTAMP' | 'timestamp' | 'TIMESTAMPTZ' | 'timestamptz' ? string : string : Exclude<T['tsType'], undefined>;
376
+ type ColumnToTs<T extends ColumnDef> = [
377
+ unknown
378
+ ] extends [T['tsType']] ? T['type'] extends 'INT' | 'INTEGER' | 'int' | 'integer' ? number : T['type'] extends 'BIGINT' | 'bigint' ? number | bigint : T['type'] extends 'DECIMAL' | 'decimal' | 'FLOAT' | 'float' | 'DOUBLE' | 'double' ? number : T['type'] extends 'BOOLEAN' | 'boolean' ? boolean : T['type'] extends 'JSON' | 'json' ? unknown : T['type'] extends 'BLOB' | 'blob' | 'BINARY' | 'binary' | 'VARBINARY' | 'varbinary' | 'BYTEA' | 'bytea' ? Buffer : T['type'] extends 'DATE' | 'date' | 'DATETIME' | 'datetime' | 'TIMESTAMP' | 'timestamp' | 'TIMESTAMPTZ' | 'timestamptz' ? string : string : Exclude<T['tsType'], undefined>;
377
379
  /**
378
380
  * Infers a row shape from a table definition
379
381
  */
@@ -2295,7 +2297,9 @@ declare class OrmSession<E extends DomainEvent = OrmDomainEvent> implements Enti
2295
2297
  persist(entity: object): Promise<void>;
2296
2298
  remove(entity: object): Promise<void>;
2297
2299
  flush(): Promise<void>;
2300
+ private flushWithHooks;
2298
2301
  commit(): Promise<void>;
2302
+ transaction<T>(fn: (session: OrmSession<E>) => Promise<T>): Promise<T>;
2299
2303
  rollback(): Promise<void>;
2300
2304
  getExecutionContext(): ExecutionContext;
2301
2305
  getHydrationContext(): HydrationContext<E>;
@@ -3621,7 +3625,7 @@ declare class TypeScriptGenerator implements ExpressionVisitor<string>, OperandV
3621
3625
  }
3622
3626
 
3623
3627
  declare const createEntityProxy: <TTable extends TableDef, TLazy extends keyof RelationMap<TTable> = keyof RelationMap<TTable>>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: TLazy[]) => EntityInstance<TTable>;
3624
- declare const createEntityFromRow: <TTable extends TableDef>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: (keyof RelationMap<TTable>)[]) => EntityInstance<TTable>;
3628
+ declare const createEntityFromRow: <TTable extends TableDef, TResult extends EntityInstance<TTable> = EntityInstance<TTable>>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: (keyof RelationMap<TTable>)[]) => TResult;
3625
3629
 
3626
3630
  type Rows$3 = Record<string, any>[];
3627
3631
  declare const loadHasManyRelation: (ctx: EntityContext, rootTable: TableDef, _relationName: string, relation: HasManyRelation) => Promise<Map<string, Rows$3>>;
@@ -3755,8 +3759,9 @@ interface ColumnOptions {
3755
3759
  args?: ColumnDef['args'];
3756
3760
  notNull?: boolean;
3757
3761
  primary?: boolean;
3762
+ tsType?: ColumnDef['tsType'];
3758
3763
  }
3759
- type ColumnInput = ColumnOptions | ColumnDef;
3764
+ type ColumnInput = ColumnOptions | ColumnDef<any, any>;
3760
3765
  declare function Column(definition: ColumnInput): DualModePropertyDecorator;
3761
3766
  declare function PrimaryKey(definition: ColumnInput): DualModePropertyDecorator;
3762
3767
 
@@ -3791,8 +3796,8 @@ declare function BelongsTo(options: BelongsToOptions): DualModePropertyDecorator
3791
3796
  declare function BelongsToMany(options: BelongsToManyOptions): DualModePropertyDecorator;
3792
3797
 
3793
3798
  declare const bootstrapEntities: () => TableDef[];
3794
- declare const getTableDefFromEntity: (ctor: EntityConstructor) => TableDef | undefined;
3795
- declare const selectFromEntity: <TTable extends TableDef>(ctor: EntityConstructor) => SelectQueryBuilder<any, TTable>;
3799
+ declare const getTableDefFromEntity: <TTable extends TableDef = TableDef>(ctor: EntityConstructor) => TTable | undefined;
3800
+ declare const selectFromEntity: <TTable extends TableDef = TableDef>(ctor: EntityConstructor) => SelectQueryBuilder<any, TTable>;
3796
3801
 
3797
3802
  interface PostgresClientLike {
3798
3803
  query(text: string, params?: unknown[]): Promise<{
package/dist/index.d.ts CHANGED
@@ -373,7 +373,9 @@ type RelationTargetTable<TRel extends RelationDef> = TRel extends HasManyRelatio
373
373
  /**
374
374
  * Maps a ColumnDef to its TypeScript type representation
375
375
  */
376
- type ColumnToTs<T extends ColumnDef> = T['tsType'] extends undefined ? T['type'] extends 'INT' | 'INTEGER' | 'int' | 'integer' ? number : T['type'] extends 'BIGINT' | 'bigint' ? number | bigint : T['type'] extends 'DECIMAL' | 'decimal' | 'FLOAT' | 'float' | 'DOUBLE' | 'double' ? number : T['type'] extends 'BOOLEAN' | 'boolean' ? boolean : T['type'] extends 'JSON' | 'json' ? unknown : T['type'] extends 'BLOB' | 'blob' | 'BINARY' | 'binary' | 'VARBINARY' | 'varbinary' | 'BYTEA' | 'bytea' ? Buffer : T['type'] extends 'DATE' | 'date' | 'DATETIME' | 'datetime' | 'TIMESTAMP' | 'timestamp' | 'TIMESTAMPTZ' | 'timestamptz' ? string : string : Exclude<T['tsType'], undefined>;
376
+ type ColumnToTs<T extends ColumnDef> = [
377
+ unknown
378
+ ] extends [T['tsType']] ? T['type'] extends 'INT' | 'INTEGER' | 'int' | 'integer' ? number : T['type'] extends 'BIGINT' | 'bigint' ? number | bigint : T['type'] extends 'DECIMAL' | 'decimal' | 'FLOAT' | 'float' | 'DOUBLE' | 'double' ? number : T['type'] extends 'BOOLEAN' | 'boolean' ? boolean : T['type'] extends 'JSON' | 'json' ? unknown : T['type'] extends 'BLOB' | 'blob' | 'BINARY' | 'binary' | 'VARBINARY' | 'varbinary' | 'BYTEA' | 'bytea' ? Buffer : T['type'] extends 'DATE' | 'date' | 'DATETIME' | 'datetime' | 'TIMESTAMP' | 'timestamp' | 'TIMESTAMPTZ' | 'timestamptz' ? string : string : Exclude<T['tsType'], undefined>;
377
379
  /**
378
380
  * Infers a row shape from a table definition
379
381
  */
@@ -2295,7 +2297,9 @@ declare class OrmSession<E extends DomainEvent = OrmDomainEvent> implements Enti
2295
2297
  persist(entity: object): Promise<void>;
2296
2298
  remove(entity: object): Promise<void>;
2297
2299
  flush(): Promise<void>;
2300
+ private flushWithHooks;
2298
2301
  commit(): Promise<void>;
2302
+ transaction<T>(fn: (session: OrmSession<E>) => Promise<T>): Promise<T>;
2299
2303
  rollback(): Promise<void>;
2300
2304
  getExecutionContext(): ExecutionContext;
2301
2305
  getHydrationContext(): HydrationContext<E>;
@@ -3621,7 +3625,7 @@ declare class TypeScriptGenerator implements ExpressionVisitor<string>, OperandV
3621
3625
  }
3622
3626
 
3623
3627
  declare const createEntityProxy: <TTable extends TableDef, TLazy extends keyof RelationMap<TTable> = keyof RelationMap<TTable>>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: TLazy[]) => EntityInstance<TTable>;
3624
- declare const createEntityFromRow: <TTable extends TableDef>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: (keyof RelationMap<TTable>)[]) => EntityInstance<TTable>;
3628
+ declare const createEntityFromRow: <TTable extends TableDef, TResult extends EntityInstance<TTable> = EntityInstance<TTable>>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: (keyof RelationMap<TTable>)[]) => TResult;
3625
3629
 
3626
3630
  type Rows$3 = Record<string, any>[];
3627
3631
  declare const loadHasManyRelation: (ctx: EntityContext, rootTable: TableDef, _relationName: string, relation: HasManyRelation) => Promise<Map<string, Rows$3>>;
@@ -3755,8 +3759,9 @@ interface ColumnOptions {
3755
3759
  args?: ColumnDef['args'];
3756
3760
  notNull?: boolean;
3757
3761
  primary?: boolean;
3762
+ tsType?: ColumnDef['tsType'];
3758
3763
  }
3759
- type ColumnInput = ColumnOptions | ColumnDef;
3764
+ type ColumnInput = ColumnOptions | ColumnDef<any, any>;
3760
3765
  declare function Column(definition: ColumnInput): DualModePropertyDecorator;
3761
3766
  declare function PrimaryKey(definition: ColumnInput): DualModePropertyDecorator;
3762
3767
 
@@ -3791,8 +3796,8 @@ declare function BelongsTo(options: BelongsToOptions): DualModePropertyDecorator
3791
3796
  declare function BelongsToMany(options: BelongsToManyOptions): DualModePropertyDecorator;
3792
3797
 
3793
3798
  declare const bootstrapEntities: () => TableDef[];
3794
- declare const getTableDefFromEntity: (ctor: EntityConstructor) => TableDef | undefined;
3795
- declare const selectFromEntity: <TTable extends TableDef>(ctor: EntityConstructor) => SelectQueryBuilder<any, TTable>;
3799
+ declare const getTableDefFromEntity: <TTable extends TableDef = TableDef>(ctor: EntityConstructor) => TTable | undefined;
3800
+ declare const selectFromEntity: <TTable extends TableDef = TableDef>(ctor: EntityConstructor) => SelectQueryBuilder<any, TTable>;
3796
3801
 
3797
3802
  interface PostgresClientLike {
3798
3803
  query(text: string, params?: unknown[]): Promise<{
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();
@@ -7576,6 +7597,7 @@ var normalizeColumnInput = (input) => {
7576
7597
  args: asOptions.args ?? asDefinition.args,
7577
7598
  notNull: asOptions.notNull ?? asDefinition.notNull,
7578
7599
  primary: asOptions.primary ?? asDefinition.primary,
7600
+ tsType: asDefinition.tsType ?? asOptions.tsType,
7579
7601
  unique: asDefinition.unique,
7580
7602
  default: asDefinition.default,
7581
7603
  autoIncrement: asDefinition.autoIncrement,