metal-orm 1.0.72 → 1.0.74

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
@@ -2661,6 +2661,18 @@ interface SelectQueryBuilderEnvironment {
2661
2661
  readonly deps: SelectQueryBuilderDependencies;
2662
2662
  }
2663
2663
 
2664
+ type RelationIncludeInput<TTable extends TableDef> = {
2665
+ [K in keyof RelationMap<TTable> & string]?: true | RelationIncludeNodeInput<TTable['relations'][K]>;
2666
+ };
2667
+ type RelationIncludeNodeInput<TRel extends RelationDef> = TypedRelationIncludeOptions<TRel> & {
2668
+ include?: RelationIncludeInput<RelationTargetTable<TRel>>;
2669
+ };
2670
+ type NormalizedRelationIncludeNode = {
2671
+ options?: RelationIncludeOptions;
2672
+ include?: NormalizedRelationIncludeTree;
2673
+ };
2674
+ type NormalizedRelationIncludeTree = Record<string, NormalizedRelationIncludeNode>;
2675
+
2664
2676
  type QueryResult = {
2665
2677
  columns: string[];
2666
2678
  values: unknown[][];
@@ -3795,6 +3807,7 @@ declare class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Ta
3795
3807
  private readonly lazyRelations;
3796
3808
  private readonly lazyRelationOptions;
3797
3809
  private readonly entityConstructor?;
3810
+ private readonly includeTree;
3798
3811
  /**
3799
3812
  * Creates a new SelectQueryBuilder instance
3800
3813
  * @param table - Table definition to query
@@ -3802,7 +3815,7 @@ declare class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Ta
3802
3815
  * @param hydration - Optional hydration manager
3803
3816
  * @param dependencies - Optional query builder dependencies
3804
3817
  */
3805
- constructor(table: TTable, state?: SelectQueryState, hydration?: HydrationManager, dependencies?: Partial<SelectQueryBuilderDependencies>, lazyRelations?: Set<string>, lazyRelationOptions?: Map<string, RelationIncludeOptions>, entityConstructor?: EntityConstructor);
3818
+ constructor(table: TTable, state?: SelectQueryState, hydration?: HydrationManager, dependencies?: Partial<SelectQueryBuilderDependencies>, lazyRelations?: Set<string>, lazyRelationOptions?: Map<string, RelationIncludeOptions>, entityConstructor?: EntityConstructor, includeTree?: NormalizedRelationIncludeTree);
3806
3819
  /**
3807
3820
  * Creates a new SelectQueryBuilder instance with updated context and lazy relations
3808
3821
  * @param context - Updated query context
@@ -4057,8 +4070,11 @@ declare class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Ta
4057
4070
  * columns: ['id', 'title'],
4058
4071
  * where: eq(postTable.columns.published, true)
4059
4072
  * });
4073
+ * @example
4074
+ * qb.include({ posts: { include: { author: true } } });
4060
4075
  */
4061
- include<K extends keyof TTable['relations'] & string>(relationName: K, options?: TypedRelationIncludeOptions<TTable['relations'][K]>): SelectQueryBuilder<T, TTable>;
4076
+ include<K extends keyof TTable['relations'] & string>(relationName: K, options?: RelationIncludeNodeInput<TTable['relations'][K]>): SelectQueryBuilder<T, TTable>;
4077
+ include(relations: RelationIncludeInput<TTable>): SelectQueryBuilder<T, TTable>;
4062
4078
  /**
4063
4079
  * Includes a relation lazily in the query results
4064
4080
  * @param relationName - Name of the relation to include lazily
@@ -4098,6 +4114,10 @@ declare class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Ta
4098
4114
  * @returns Map of relation names to include options
4099
4115
  */
4100
4116
  getLazyRelationOptions(): Map<string, RelationIncludeOptions>;
4117
+ /**
4118
+ * Gets normalized nested include information for runtime preloading.
4119
+ */
4120
+ getIncludeTree(): NormalizedRelationIncludeTree;
4101
4121
  /**
4102
4122
  * Gets the table definition for this query builder
4103
4123
  * @returns Table definition
@@ -6719,6 +6739,15 @@ declare const selectFromEntity: <TEntity extends object>(ctor: EntityConstructor
6719
6739
  * `tableRef(...)`-style proxy so users can write `u.id` instead of `u.columns.id`.
6720
6740
  */
6721
6741
  declare const entityRef: <TEntity extends object>(ctor: EntityConstructor<TEntity>) => TableRef$1<EntityTable<TEntity>>;
6742
+ type EntityRefsTuple<T extends readonly EntityConstructor<object>[]> = {
6743
+ [K in keyof T]: T[K] extends EntityConstructor<infer TEntity> ? TableRef$1<EntityTable<TEntity & object>> : never;
6744
+ };
6745
+ /**
6746
+ * Public API: variadic entity references.
6747
+ * Usage:
6748
+ * const [u, p] = entityRefs(User, Post);
6749
+ */
6750
+ declare const entityRefs: <T extends readonly EntityConstructor<object>[]>(...ctors: T) => EntityRefsTuple<T>;
6722
6751
 
6723
6752
  /**
6724
6753
  * Bag for storing decorator metadata during the decoration phase.
@@ -6983,4 +7012,4 @@ type PooledExecutorFactoryOptions<TConn> = {
6983
7012
  */
6984
7013
  declare function createPooledExecutorFactory<TConn>(opts: PooledExecutorFactoryOptions<TConn>): DbExecutorFactory;
6985
7014
 
6986
- export { type AliasRefNode, type AnyDomainEvent, type ArithmeticExpressionNode, type TableRef as AstTableRef, AsyncLocalStorage, BelongsTo, BelongsToMany, type BelongsToManyOptions, type BelongsToManyRelation, type BelongsToOptions, type BelongsToReference, type BelongsToReferenceApi, type BelongsToRelation, type BetweenExpressionNode, type BinaryExpressionNode, type BitwiseExpressionNode, type CascadeMode, type CaseExpressionNode, type CastExpressionNode, type CheckConstraint, type CollateExpressionNode, Column, type ColumnDef, type ColumnDiff, type ColumnInput, type ColumnNode, type ColumnOptions, type ColumnRef, type ColumnToTs, type ColumnType, ConstructorMaterializationStrategy, type CreateTediousClientOptions, type DatabaseCheck, type DatabaseColumn, type DatabaseIndex, type DatabaseSchema, type DatabaseTable, type DbExecutor, type DbExecutorFactory, type DecoratedEntityInstance, DefaultBelongsToReference, DefaultEntityMaterializer, DefaultHasManyCollection, DefaultManyToManyCollection, type DefaultValue, DeleteQueryBuilder, type DialectName, type DomainEvent, DomainEventBus, type DomainEventHandler, Entity, type EntityContext, type EntityInstance, type EntityMaterializationStrategy, type EntityMaterializer, type EntityOptions, type PrimaryKey$1 as EntityPrimaryKey, EntityStatus, type ExecutionContext, type ExistsExpressionNode, type ExpressionNode, type ExpressionVisitor, type ForeignKeyReference, type FunctionNode, type GroupConcatOptions, type HasDomainEvents, HasMany, type HasManyCollection, type HasManyOptions, type HasManyRelation, HasOne, type HasOneOptions, type HasOneReference, type HasOneReferenceApi, type HasOneRelation, type HydrationContext, type HydrationMetadata, type HydrationPivotPlan, type HydrationPlan, type HydrationRelationPlan, type InExpressionNode, type InExpressionRight, type IndexColumn, type IndexDef, type InferRow, type InitialHandlers, InsertQueryBuilder, type IntrospectOptions, type JsonPathNode, type Jsonify, type JsonifyScalar, type LiteralNode, type LiteralValue, type LogicalExpressionNode, type ManyToManyCollection, type MssqlClientLike, MySqlDialect, type MysqlClientLike, type NullExpressionNode, type OperandNode, type OperandVisitor, Orm, type OrmDomainEvent, type OrmInterceptor, type OrmOptions, OrmSession, type OrmSessionOptions, Pool, type PoolAdapter, type PoolLease, type PoolOptions, type PooledConnectionAdapter, type PostgresClientLike, PostgresDialect, PrimaryKey, type Primitive, PrototypeMaterializationStrategy, type QueryLogEntry, type QueryLogger, type QueryResult, type RawDefaultValue, type ReferentialAction, type RelationChange, type RelationChangeEntry, type RelationDef, type RelationKey$1 as RelationKey, RelationKinds, type RelationMap, type RelationTargetTable, type RelationType, type RenderColumnOptions, STANDARD_COLUMN_TYPES, type SaveGraphInputPayload, type SaveGraphInputScalar, type SaveGraphJsonScalar, type SaveGraphSessionOptions, type ScalarSubqueryNode, type SchemaChange, type SchemaChangeKind, type SchemaDiffOptions, type SchemaGenerateResult, type SchemaIntrospector, type SchemaPlan, SelectQueryBuilder, type SelectQueryInput, type SelectableKeys, type SimpleQueryRunner, SqlServerDialect, type SqliteClientLike, SqliteDialect, type StandardColumnType, type SynchronizeOptions, type TableDef, type TableHooks, type TableOptions, type TableRef$1 as TableRef, type TediousColumn, type TediousConnectionLike, type TediousModule, type TediousRequest, type TediousRequestCtor, type TediousTypes, type TrackedEntity, TypeScriptGenerator, type TypedExpression, type TypedLike, UpdateQueryBuilder, type ValueOperandInput, type WindowFunctionNode, abs, acos, add, addDomainEvent, age, aliasRef, and, arrayAppend, asType, ascii, asin, atan, atan2, avg, belongsTo, belongsToMany, between, bitAnd, bitLength, bitOr, bitXor, bootstrapEntities, caseWhen, cast, cbrt, ceil, ceiling, char, charLength, chr, clearExpressionDispatchers, clearOperandDispatchers, coalesce, col, collate, columnOperand, concat, concatWs, correlateBy, cos, cot, count, countAll, createEntityFromRow, createEntityProxy, createExecutorFromQueryRunner, createMssqlExecutor, createMysqlExecutor, createPooledExecutorFactory, createPostgresExecutor, createQueryLoggingExecutor, createSqliteExecutor, createTediousExecutor, createTediousMssqlClient, currentDate, currentTime, dateAdd, dateDiff, dateFormat, dateSub, dateTrunc, day, dayOfWeek, defineTable, degrees, deleteFrom, denseRank, diffSchema, div, endOfMonth, entityRef, eq, esel, executeHydrated, executeHydratedPlain, executeHydratedPlainWithContexts, executeHydratedWithContexts, executeSchemaSql, executeSchemaSqlFor, exists, exp, extract, firstValue, floor, fromUnixTime, generateCreateTableSql, generateSchemaSql, generateSchemaSqlFor, getColumn, getDecoratorMetadata, getSchemaIntrospector, getTableDefFromEntity, greatest, groupConcat, gt, gte, hasMany, hasOne, hour, hydrateRows, ifNull, inList, inSubquery, initcap, insertInto, instr, introspectSchema, isCaseExpressionNode, isCastExpressionNode, isCollateExpressionNode, isExpressionSelectionNode, isFunctionNode, isNotNull, isNull, isOperandNode, isValueOperandInput, isWindowFunctionNode, jsonArrayAgg, jsonContains, jsonLength, jsonPath, jsonSet, jsonify, lag, lastValue, lead, least, left, length, like, ln, loadBelongsToManyRelation, loadBelongsToRelation, loadHasManyRelation, loadHasOneRelation, localTime, localTimestamp, locate, log, log10, log2, logBase, lower, lpad, lt, lte, ltrim, materializeAs, max, md5, min, minute, mod, month, mul, neq, normalizeColumnType, notBetween, notExists, notInList, notInSubquery, notLike, now, ntile, nullif, octetLength, or, outerRef, pi, position, pow, power, quarter, radians, rand, random, rank, registerExpressionDispatcher, registerOperandDispatcher, registerSchemaIntrospector, relationLoaderCache, renderColumnDefinition, renderTypeWithArgs, repeat, replace, reverse, right, round, rowNumber, rowsToQueryResult, rpad, rtrim, second, sel, selectFrom, selectFromEntity, setRelations, sha1, sha2, shiftLeft, shiftRight, sign, sin, space, sqrt, stddev, sub, substr, sum, synchronizeSchema, tableRef, tan, toColumnRef, toTableRef, trim, trunc, truncate, unixTimestamp, update, upper, utcNow, valueToOperand, variance, visitExpression, visitOperand, weekOfYear, windowFunction, year };
7015
+ export { type AliasRefNode, type AnyDomainEvent, type ArithmeticExpressionNode, type TableRef as AstTableRef, AsyncLocalStorage, BelongsTo, BelongsToMany, type BelongsToManyOptions, type BelongsToManyRelation, type BelongsToOptions, type BelongsToReference, type BelongsToReferenceApi, type BelongsToRelation, type BetweenExpressionNode, type BinaryExpressionNode, type BitwiseExpressionNode, type CascadeMode, type CaseExpressionNode, type CastExpressionNode, type CheckConstraint, type CollateExpressionNode, Column, type ColumnDef, type ColumnDiff, type ColumnInput, type ColumnNode, type ColumnOptions, type ColumnRef, type ColumnToTs, type ColumnType, ConstructorMaterializationStrategy, type CreateTediousClientOptions, type DatabaseCheck, type DatabaseColumn, type DatabaseIndex, type DatabaseSchema, type DatabaseTable, type DbExecutor, type DbExecutorFactory, type DecoratedEntityInstance, DefaultBelongsToReference, DefaultEntityMaterializer, DefaultHasManyCollection, DefaultManyToManyCollection, type DefaultValue, DeleteQueryBuilder, type DialectName, type DomainEvent, DomainEventBus, type DomainEventHandler, Entity, type EntityContext, type EntityInstance, type EntityMaterializationStrategy, type EntityMaterializer, type EntityOptions, type PrimaryKey$1 as EntityPrimaryKey, EntityStatus, type ExecutionContext, type ExistsExpressionNode, type ExpressionNode, type ExpressionVisitor, type ForeignKeyReference, type FunctionNode, type GroupConcatOptions, type HasDomainEvents, HasMany, type HasManyCollection, type HasManyOptions, type HasManyRelation, HasOne, type HasOneOptions, type HasOneReference, type HasOneReferenceApi, type HasOneRelation, type HydrationContext, type HydrationMetadata, type HydrationPivotPlan, type HydrationPlan, type HydrationRelationPlan, type InExpressionNode, type InExpressionRight, type IndexColumn, type IndexDef, type InferRow, type InitialHandlers, InsertQueryBuilder, type IntrospectOptions, type JsonPathNode, type Jsonify, type JsonifyScalar, type LiteralNode, type LiteralValue, type LogicalExpressionNode, type ManyToManyCollection, type MssqlClientLike, MySqlDialect, type MysqlClientLike, type NullExpressionNode, type OperandNode, type OperandVisitor, Orm, type OrmDomainEvent, type OrmInterceptor, type OrmOptions, OrmSession, type OrmSessionOptions, Pool, type PoolAdapter, type PoolLease, type PoolOptions, type PooledConnectionAdapter, type PostgresClientLike, PostgresDialect, PrimaryKey, type Primitive, PrototypeMaterializationStrategy, type QueryLogEntry, type QueryLogger, type QueryResult, type RawDefaultValue, type ReferentialAction, type RelationChange, type RelationChangeEntry, type RelationDef, type RelationKey$1 as RelationKey, RelationKinds, type RelationMap, type RelationTargetTable, type RelationType, type RenderColumnOptions, STANDARD_COLUMN_TYPES, type SaveGraphInputPayload, type SaveGraphInputScalar, type SaveGraphJsonScalar, type SaveGraphSessionOptions, type ScalarSubqueryNode, type SchemaChange, type SchemaChangeKind, type SchemaDiffOptions, type SchemaGenerateResult, type SchemaIntrospector, type SchemaPlan, SelectQueryBuilder, type SelectQueryInput, type SelectableKeys, type SimpleQueryRunner, SqlServerDialect, type SqliteClientLike, SqliteDialect, type StandardColumnType, type SynchronizeOptions, type TableDef, type TableHooks, type TableOptions, type TableRef$1 as TableRef, type TediousColumn, type TediousConnectionLike, type TediousModule, type TediousRequest, type TediousRequestCtor, type TediousTypes, type TrackedEntity, TypeScriptGenerator, type TypedExpression, type TypedLike, UpdateQueryBuilder, type ValueOperandInput, type WindowFunctionNode, abs, acos, add, addDomainEvent, age, aliasRef, and, arrayAppend, asType, ascii, asin, atan, atan2, avg, belongsTo, belongsToMany, between, bitAnd, bitLength, bitOr, bitXor, bootstrapEntities, caseWhen, cast, cbrt, ceil, ceiling, char, charLength, chr, clearExpressionDispatchers, clearOperandDispatchers, coalesce, col, collate, columnOperand, concat, concatWs, correlateBy, cos, cot, count, countAll, createEntityFromRow, createEntityProxy, createExecutorFromQueryRunner, createMssqlExecutor, createMysqlExecutor, createPooledExecutorFactory, createPostgresExecutor, createQueryLoggingExecutor, createSqliteExecutor, createTediousExecutor, createTediousMssqlClient, currentDate, currentTime, dateAdd, dateDiff, dateFormat, dateSub, dateTrunc, day, dayOfWeek, defineTable, degrees, deleteFrom, denseRank, diffSchema, div, endOfMonth, entityRef, entityRefs, eq, esel, executeHydrated, executeHydratedPlain, executeHydratedPlainWithContexts, executeHydratedWithContexts, executeSchemaSql, executeSchemaSqlFor, exists, exp, extract, firstValue, floor, fromUnixTime, generateCreateTableSql, generateSchemaSql, generateSchemaSqlFor, getColumn, getDecoratorMetadata, getSchemaIntrospector, getTableDefFromEntity, greatest, groupConcat, gt, gte, hasMany, hasOne, hour, hydrateRows, ifNull, inList, inSubquery, initcap, insertInto, instr, introspectSchema, isCaseExpressionNode, isCastExpressionNode, isCollateExpressionNode, isExpressionSelectionNode, isFunctionNode, isNotNull, isNull, isOperandNode, isValueOperandInput, isWindowFunctionNode, jsonArrayAgg, jsonContains, jsonLength, jsonPath, jsonSet, jsonify, lag, lastValue, lead, least, left, length, like, ln, loadBelongsToManyRelation, loadBelongsToRelation, loadHasManyRelation, loadHasOneRelation, localTime, localTimestamp, locate, log, log10, log2, logBase, lower, lpad, lt, lte, ltrim, materializeAs, max, md5, min, minute, mod, month, mul, neq, normalizeColumnType, notBetween, notExists, notInList, notInSubquery, notLike, now, ntile, nullif, octetLength, or, outerRef, pi, position, pow, power, quarter, radians, rand, random, rank, registerExpressionDispatcher, registerOperandDispatcher, registerSchemaIntrospector, relationLoaderCache, renderColumnDefinition, renderTypeWithArgs, repeat, replace, reverse, right, round, rowNumber, rowsToQueryResult, rpad, rtrim, second, sel, selectFrom, selectFromEntity, setRelations, sha1, sha2, shiftLeft, shiftRight, sign, sin, space, sqrt, stddev, sub, substr, sum, synchronizeSchema, tableRef, tan, toColumnRef, toTableRef, trim, trunc, truncate, unixTimestamp, update, upper, utcNow, valueToOperand, variance, visitExpression, visitOperand, weekOfYear, windowFunction, year };
package/dist/index.d.ts CHANGED
@@ -2661,6 +2661,18 @@ interface SelectQueryBuilderEnvironment {
2661
2661
  readonly deps: SelectQueryBuilderDependencies;
2662
2662
  }
2663
2663
 
2664
+ type RelationIncludeInput<TTable extends TableDef> = {
2665
+ [K in keyof RelationMap<TTable> & string]?: true | RelationIncludeNodeInput<TTable['relations'][K]>;
2666
+ };
2667
+ type RelationIncludeNodeInput<TRel extends RelationDef> = TypedRelationIncludeOptions<TRel> & {
2668
+ include?: RelationIncludeInput<RelationTargetTable<TRel>>;
2669
+ };
2670
+ type NormalizedRelationIncludeNode = {
2671
+ options?: RelationIncludeOptions;
2672
+ include?: NormalizedRelationIncludeTree;
2673
+ };
2674
+ type NormalizedRelationIncludeTree = Record<string, NormalizedRelationIncludeNode>;
2675
+
2664
2676
  type QueryResult = {
2665
2677
  columns: string[];
2666
2678
  values: unknown[][];
@@ -3795,6 +3807,7 @@ declare class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Ta
3795
3807
  private readonly lazyRelations;
3796
3808
  private readonly lazyRelationOptions;
3797
3809
  private readonly entityConstructor?;
3810
+ private readonly includeTree;
3798
3811
  /**
3799
3812
  * Creates a new SelectQueryBuilder instance
3800
3813
  * @param table - Table definition to query
@@ -3802,7 +3815,7 @@ declare class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Ta
3802
3815
  * @param hydration - Optional hydration manager
3803
3816
  * @param dependencies - Optional query builder dependencies
3804
3817
  */
3805
- constructor(table: TTable, state?: SelectQueryState, hydration?: HydrationManager, dependencies?: Partial<SelectQueryBuilderDependencies>, lazyRelations?: Set<string>, lazyRelationOptions?: Map<string, RelationIncludeOptions>, entityConstructor?: EntityConstructor);
3818
+ constructor(table: TTable, state?: SelectQueryState, hydration?: HydrationManager, dependencies?: Partial<SelectQueryBuilderDependencies>, lazyRelations?: Set<string>, lazyRelationOptions?: Map<string, RelationIncludeOptions>, entityConstructor?: EntityConstructor, includeTree?: NormalizedRelationIncludeTree);
3806
3819
  /**
3807
3820
  * Creates a new SelectQueryBuilder instance with updated context and lazy relations
3808
3821
  * @param context - Updated query context
@@ -4057,8 +4070,11 @@ declare class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Ta
4057
4070
  * columns: ['id', 'title'],
4058
4071
  * where: eq(postTable.columns.published, true)
4059
4072
  * });
4073
+ * @example
4074
+ * qb.include({ posts: { include: { author: true } } });
4060
4075
  */
4061
- include<K extends keyof TTable['relations'] & string>(relationName: K, options?: TypedRelationIncludeOptions<TTable['relations'][K]>): SelectQueryBuilder<T, TTable>;
4076
+ include<K extends keyof TTable['relations'] & string>(relationName: K, options?: RelationIncludeNodeInput<TTable['relations'][K]>): SelectQueryBuilder<T, TTable>;
4077
+ include(relations: RelationIncludeInput<TTable>): SelectQueryBuilder<T, TTable>;
4062
4078
  /**
4063
4079
  * Includes a relation lazily in the query results
4064
4080
  * @param relationName - Name of the relation to include lazily
@@ -4098,6 +4114,10 @@ declare class SelectQueryBuilder<T = EntityInstance<TableDef>, TTable extends Ta
4098
4114
  * @returns Map of relation names to include options
4099
4115
  */
4100
4116
  getLazyRelationOptions(): Map<string, RelationIncludeOptions>;
4117
+ /**
4118
+ * Gets normalized nested include information for runtime preloading.
4119
+ */
4120
+ getIncludeTree(): NormalizedRelationIncludeTree;
4101
4121
  /**
4102
4122
  * Gets the table definition for this query builder
4103
4123
  * @returns Table definition
@@ -6719,6 +6739,15 @@ declare const selectFromEntity: <TEntity extends object>(ctor: EntityConstructor
6719
6739
  * `tableRef(...)`-style proxy so users can write `u.id` instead of `u.columns.id`.
6720
6740
  */
6721
6741
  declare const entityRef: <TEntity extends object>(ctor: EntityConstructor<TEntity>) => TableRef$1<EntityTable<TEntity>>;
6742
+ type EntityRefsTuple<T extends readonly EntityConstructor<object>[]> = {
6743
+ [K in keyof T]: T[K] extends EntityConstructor<infer TEntity> ? TableRef$1<EntityTable<TEntity & object>> : never;
6744
+ };
6745
+ /**
6746
+ * Public API: variadic entity references.
6747
+ * Usage:
6748
+ * const [u, p] = entityRefs(User, Post);
6749
+ */
6750
+ declare const entityRefs: <T extends readonly EntityConstructor<object>[]>(...ctors: T) => EntityRefsTuple<T>;
6722
6751
 
6723
6752
  /**
6724
6753
  * Bag for storing decorator metadata during the decoration phase.
@@ -6983,4 +7012,4 @@ type PooledExecutorFactoryOptions<TConn> = {
6983
7012
  */
6984
7013
  declare function createPooledExecutorFactory<TConn>(opts: PooledExecutorFactoryOptions<TConn>): DbExecutorFactory;
6985
7014
 
6986
- export { type AliasRefNode, type AnyDomainEvent, type ArithmeticExpressionNode, type TableRef as AstTableRef, AsyncLocalStorage, BelongsTo, BelongsToMany, type BelongsToManyOptions, type BelongsToManyRelation, type BelongsToOptions, type BelongsToReference, type BelongsToReferenceApi, type BelongsToRelation, type BetweenExpressionNode, type BinaryExpressionNode, type BitwiseExpressionNode, type CascadeMode, type CaseExpressionNode, type CastExpressionNode, type CheckConstraint, type CollateExpressionNode, Column, type ColumnDef, type ColumnDiff, type ColumnInput, type ColumnNode, type ColumnOptions, type ColumnRef, type ColumnToTs, type ColumnType, ConstructorMaterializationStrategy, type CreateTediousClientOptions, type DatabaseCheck, type DatabaseColumn, type DatabaseIndex, type DatabaseSchema, type DatabaseTable, type DbExecutor, type DbExecutorFactory, type DecoratedEntityInstance, DefaultBelongsToReference, DefaultEntityMaterializer, DefaultHasManyCollection, DefaultManyToManyCollection, type DefaultValue, DeleteQueryBuilder, type DialectName, type DomainEvent, DomainEventBus, type DomainEventHandler, Entity, type EntityContext, type EntityInstance, type EntityMaterializationStrategy, type EntityMaterializer, type EntityOptions, type PrimaryKey$1 as EntityPrimaryKey, EntityStatus, type ExecutionContext, type ExistsExpressionNode, type ExpressionNode, type ExpressionVisitor, type ForeignKeyReference, type FunctionNode, type GroupConcatOptions, type HasDomainEvents, HasMany, type HasManyCollection, type HasManyOptions, type HasManyRelation, HasOne, type HasOneOptions, type HasOneReference, type HasOneReferenceApi, type HasOneRelation, type HydrationContext, type HydrationMetadata, type HydrationPivotPlan, type HydrationPlan, type HydrationRelationPlan, type InExpressionNode, type InExpressionRight, type IndexColumn, type IndexDef, type InferRow, type InitialHandlers, InsertQueryBuilder, type IntrospectOptions, type JsonPathNode, type Jsonify, type JsonifyScalar, type LiteralNode, type LiteralValue, type LogicalExpressionNode, type ManyToManyCollection, type MssqlClientLike, MySqlDialect, type MysqlClientLike, type NullExpressionNode, type OperandNode, type OperandVisitor, Orm, type OrmDomainEvent, type OrmInterceptor, type OrmOptions, OrmSession, type OrmSessionOptions, Pool, type PoolAdapter, type PoolLease, type PoolOptions, type PooledConnectionAdapter, type PostgresClientLike, PostgresDialect, PrimaryKey, type Primitive, PrototypeMaterializationStrategy, type QueryLogEntry, type QueryLogger, type QueryResult, type RawDefaultValue, type ReferentialAction, type RelationChange, type RelationChangeEntry, type RelationDef, type RelationKey$1 as RelationKey, RelationKinds, type RelationMap, type RelationTargetTable, type RelationType, type RenderColumnOptions, STANDARD_COLUMN_TYPES, type SaveGraphInputPayload, type SaveGraphInputScalar, type SaveGraphJsonScalar, type SaveGraphSessionOptions, type ScalarSubqueryNode, type SchemaChange, type SchemaChangeKind, type SchemaDiffOptions, type SchemaGenerateResult, type SchemaIntrospector, type SchemaPlan, SelectQueryBuilder, type SelectQueryInput, type SelectableKeys, type SimpleQueryRunner, SqlServerDialect, type SqliteClientLike, SqliteDialect, type StandardColumnType, type SynchronizeOptions, type TableDef, type TableHooks, type TableOptions, type TableRef$1 as TableRef, type TediousColumn, type TediousConnectionLike, type TediousModule, type TediousRequest, type TediousRequestCtor, type TediousTypes, type TrackedEntity, TypeScriptGenerator, type TypedExpression, type TypedLike, UpdateQueryBuilder, type ValueOperandInput, type WindowFunctionNode, abs, acos, add, addDomainEvent, age, aliasRef, and, arrayAppend, asType, ascii, asin, atan, atan2, avg, belongsTo, belongsToMany, between, bitAnd, bitLength, bitOr, bitXor, bootstrapEntities, caseWhen, cast, cbrt, ceil, ceiling, char, charLength, chr, clearExpressionDispatchers, clearOperandDispatchers, coalesce, col, collate, columnOperand, concat, concatWs, correlateBy, cos, cot, count, countAll, createEntityFromRow, createEntityProxy, createExecutorFromQueryRunner, createMssqlExecutor, createMysqlExecutor, createPooledExecutorFactory, createPostgresExecutor, createQueryLoggingExecutor, createSqliteExecutor, createTediousExecutor, createTediousMssqlClient, currentDate, currentTime, dateAdd, dateDiff, dateFormat, dateSub, dateTrunc, day, dayOfWeek, defineTable, degrees, deleteFrom, denseRank, diffSchema, div, endOfMonth, entityRef, eq, esel, executeHydrated, executeHydratedPlain, executeHydratedPlainWithContexts, executeHydratedWithContexts, executeSchemaSql, executeSchemaSqlFor, exists, exp, extract, firstValue, floor, fromUnixTime, generateCreateTableSql, generateSchemaSql, generateSchemaSqlFor, getColumn, getDecoratorMetadata, getSchemaIntrospector, getTableDefFromEntity, greatest, groupConcat, gt, gte, hasMany, hasOne, hour, hydrateRows, ifNull, inList, inSubquery, initcap, insertInto, instr, introspectSchema, isCaseExpressionNode, isCastExpressionNode, isCollateExpressionNode, isExpressionSelectionNode, isFunctionNode, isNotNull, isNull, isOperandNode, isValueOperandInput, isWindowFunctionNode, jsonArrayAgg, jsonContains, jsonLength, jsonPath, jsonSet, jsonify, lag, lastValue, lead, least, left, length, like, ln, loadBelongsToManyRelation, loadBelongsToRelation, loadHasManyRelation, loadHasOneRelation, localTime, localTimestamp, locate, log, log10, log2, logBase, lower, lpad, lt, lte, ltrim, materializeAs, max, md5, min, minute, mod, month, mul, neq, normalizeColumnType, notBetween, notExists, notInList, notInSubquery, notLike, now, ntile, nullif, octetLength, or, outerRef, pi, position, pow, power, quarter, radians, rand, random, rank, registerExpressionDispatcher, registerOperandDispatcher, registerSchemaIntrospector, relationLoaderCache, renderColumnDefinition, renderTypeWithArgs, repeat, replace, reverse, right, round, rowNumber, rowsToQueryResult, rpad, rtrim, second, sel, selectFrom, selectFromEntity, setRelations, sha1, sha2, shiftLeft, shiftRight, sign, sin, space, sqrt, stddev, sub, substr, sum, synchronizeSchema, tableRef, tan, toColumnRef, toTableRef, trim, trunc, truncate, unixTimestamp, update, upper, utcNow, valueToOperand, variance, visitExpression, visitOperand, weekOfYear, windowFunction, year };
7015
+ export { type AliasRefNode, type AnyDomainEvent, type ArithmeticExpressionNode, type TableRef as AstTableRef, AsyncLocalStorage, BelongsTo, BelongsToMany, type BelongsToManyOptions, type BelongsToManyRelation, type BelongsToOptions, type BelongsToReference, type BelongsToReferenceApi, type BelongsToRelation, type BetweenExpressionNode, type BinaryExpressionNode, type BitwiseExpressionNode, type CascadeMode, type CaseExpressionNode, type CastExpressionNode, type CheckConstraint, type CollateExpressionNode, Column, type ColumnDef, type ColumnDiff, type ColumnInput, type ColumnNode, type ColumnOptions, type ColumnRef, type ColumnToTs, type ColumnType, ConstructorMaterializationStrategy, type CreateTediousClientOptions, type DatabaseCheck, type DatabaseColumn, type DatabaseIndex, type DatabaseSchema, type DatabaseTable, type DbExecutor, type DbExecutorFactory, type DecoratedEntityInstance, DefaultBelongsToReference, DefaultEntityMaterializer, DefaultHasManyCollection, DefaultManyToManyCollection, type DefaultValue, DeleteQueryBuilder, type DialectName, type DomainEvent, DomainEventBus, type DomainEventHandler, Entity, type EntityContext, type EntityInstance, type EntityMaterializationStrategy, type EntityMaterializer, type EntityOptions, type PrimaryKey$1 as EntityPrimaryKey, EntityStatus, type ExecutionContext, type ExistsExpressionNode, type ExpressionNode, type ExpressionVisitor, type ForeignKeyReference, type FunctionNode, type GroupConcatOptions, type HasDomainEvents, HasMany, type HasManyCollection, type HasManyOptions, type HasManyRelation, HasOne, type HasOneOptions, type HasOneReference, type HasOneReferenceApi, type HasOneRelation, type HydrationContext, type HydrationMetadata, type HydrationPivotPlan, type HydrationPlan, type HydrationRelationPlan, type InExpressionNode, type InExpressionRight, type IndexColumn, type IndexDef, type InferRow, type InitialHandlers, InsertQueryBuilder, type IntrospectOptions, type JsonPathNode, type Jsonify, type JsonifyScalar, type LiteralNode, type LiteralValue, type LogicalExpressionNode, type ManyToManyCollection, type MssqlClientLike, MySqlDialect, type MysqlClientLike, type NullExpressionNode, type OperandNode, type OperandVisitor, Orm, type OrmDomainEvent, type OrmInterceptor, type OrmOptions, OrmSession, type OrmSessionOptions, Pool, type PoolAdapter, type PoolLease, type PoolOptions, type PooledConnectionAdapter, type PostgresClientLike, PostgresDialect, PrimaryKey, type Primitive, PrototypeMaterializationStrategy, type QueryLogEntry, type QueryLogger, type QueryResult, type RawDefaultValue, type ReferentialAction, type RelationChange, type RelationChangeEntry, type RelationDef, type RelationKey$1 as RelationKey, RelationKinds, type RelationMap, type RelationTargetTable, type RelationType, type RenderColumnOptions, STANDARD_COLUMN_TYPES, type SaveGraphInputPayload, type SaveGraphInputScalar, type SaveGraphJsonScalar, type SaveGraphSessionOptions, type ScalarSubqueryNode, type SchemaChange, type SchemaChangeKind, type SchemaDiffOptions, type SchemaGenerateResult, type SchemaIntrospector, type SchemaPlan, SelectQueryBuilder, type SelectQueryInput, type SelectableKeys, type SimpleQueryRunner, SqlServerDialect, type SqliteClientLike, SqliteDialect, type StandardColumnType, type SynchronizeOptions, type TableDef, type TableHooks, type TableOptions, type TableRef$1 as TableRef, type TediousColumn, type TediousConnectionLike, type TediousModule, type TediousRequest, type TediousRequestCtor, type TediousTypes, type TrackedEntity, TypeScriptGenerator, type TypedExpression, type TypedLike, UpdateQueryBuilder, type ValueOperandInput, type WindowFunctionNode, abs, acos, add, addDomainEvent, age, aliasRef, and, arrayAppend, asType, ascii, asin, atan, atan2, avg, belongsTo, belongsToMany, between, bitAnd, bitLength, bitOr, bitXor, bootstrapEntities, caseWhen, cast, cbrt, ceil, ceiling, char, charLength, chr, clearExpressionDispatchers, clearOperandDispatchers, coalesce, col, collate, columnOperand, concat, concatWs, correlateBy, cos, cot, count, countAll, createEntityFromRow, createEntityProxy, createExecutorFromQueryRunner, createMssqlExecutor, createMysqlExecutor, createPooledExecutorFactory, createPostgresExecutor, createQueryLoggingExecutor, createSqliteExecutor, createTediousExecutor, createTediousMssqlClient, currentDate, currentTime, dateAdd, dateDiff, dateFormat, dateSub, dateTrunc, day, dayOfWeek, defineTable, degrees, deleteFrom, denseRank, diffSchema, div, endOfMonth, entityRef, entityRefs, eq, esel, executeHydrated, executeHydratedPlain, executeHydratedPlainWithContexts, executeHydratedWithContexts, executeSchemaSql, executeSchemaSqlFor, exists, exp, extract, firstValue, floor, fromUnixTime, generateCreateTableSql, generateSchemaSql, generateSchemaSqlFor, getColumn, getDecoratorMetadata, getSchemaIntrospector, getTableDefFromEntity, greatest, groupConcat, gt, gte, hasMany, hasOne, hour, hydrateRows, ifNull, inList, inSubquery, initcap, insertInto, instr, introspectSchema, isCaseExpressionNode, isCastExpressionNode, isCollateExpressionNode, isExpressionSelectionNode, isFunctionNode, isNotNull, isNull, isOperandNode, isValueOperandInput, isWindowFunctionNode, jsonArrayAgg, jsonContains, jsonLength, jsonPath, jsonSet, jsonify, lag, lastValue, lead, least, left, length, like, ln, loadBelongsToManyRelation, loadBelongsToRelation, loadHasManyRelation, loadHasOneRelation, localTime, localTimestamp, locate, log, log10, log2, logBase, lower, lpad, lt, lte, ltrim, materializeAs, max, md5, min, minute, mod, month, mul, neq, normalizeColumnType, notBetween, notExists, notInList, notInSubquery, notLike, now, ntile, nullif, octetLength, or, outerRef, pi, position, pow, power, quarter, radians, rand, random, rank, registerExpressionDispatcher, registerOperandDispatcher, registerSchemaIntrospector, relationLoaderCache, renderColumnDefinition, renderTypeWithArgs, repeat, replace, reverse, right, round, rowNumber, rowsToQueryResult, rpad, rtrim, second, sel, selectFrom, selectFromEntity, setRelations, sha1, sha2, shiftLeft, shiftRight, sign, sin, space, sqrt, stddev, sub, substr, sum, synchronizeSchema, tableRef, tan, toColumnRef, toTableRef, trim, trunc, truncate, unixTimestamp, update, upper, utcNow, valueToOperand, variance, visitExpression, visitOperand, weekOfYear, windowFunction, year };
package/dist/index.js CHANGED
@@ -4618,6 +4618,58 @@ var resolveSelectQueryBuilderDependencies = (overrides = {}) => {
4618
4618
  };
4619
4619
  var defaultSelectQueryBuilderDependencies = resolveSelectQueryBuilderDependencies();
4620
4620
 
4621
+ // src/query-builder/relation-include-tree.ts
4622
+ var isObject = (value) => Boolean(value && typeof value === "object");
4623
+ var normalizeRelationIncludeNode = (value) => {
4624
+ if (!value || value === true) {
4625
+ return {};
4626
+ }
4627
+ if (!isObject(value)) {
4628
+ return {};
4629
+ }
4630
+ const { include, ...rest } = value;
4631
+ const options = Object.keys(rest).length ? rest : void 0;
4632
+ const normalizedInclude = isObject(include) ? normalizeRelationInclude(include) : void 0;
4633
+ if (normalizedInclude && Object.keys(normalizedInclude).length > 0) {
4634
+ return { options, include: normalizedInclude };
4635
+ }
4636
+ return { options };
4637
+ };
4638
+ var normalizeRelationInclude = (input) => {
4639
+ if (!input) return {};
4640
+ const tree = {};
4641
+ for (const [key, value] of Object.entries(input)) {
4642
+ tree[key] = normalizeRelationIncludeNode(value);
4643
+ }
4644
+ return tree;
4645
+ };
4646
+ var mergeRelationIncludeTrees = (base, next) => {
4647
+ const merged = { ...base };
4648
+ for (const [key, node] of Object.entries(next)) {
4649
+ const existing = merged[key];
4650
+ if (!existing) {
4651
+ merged[key] = node;
4652
+ continue;
4653
+ }
4654
+ const include = existing.include && node.include ? mergeRelationIncludeTrees(existing.include, node.include) : node.include ?? existing.include;
4655
+ merged[key] = {
4656
+ options: node.options ?? existing.options,
4657
+ ...include ? { include } : {}
4658
+ };
4659
+ }
4660
+ return merged;
4661
+ };
4662
+ var cloneRelationIncludeTree = (tree) => {
4663
+ const cloned = {};
4664
+ for (const [key, node] of Object.entries(tree)) {
4665
+ cloned[key] = {
4666
+ options: node.options,
4667
+ ...node.include ? { include: cloneRelationIncludeTree(node.include) } : {}
4668
+ };
4669
+ }
4670
+ return cloned;
4671
+ };
4672
+
4621
4673
  // src/orm/hydration.ts
4622
4674
  var hydrateRows = (rows, plan) => {
4623
4675
  if (!plan || !rows.length) return rows;
@@ -5750,14 +5802,14 @@ var getRelationWrapper = (meta, relationName, owner, createEntity) => {
5750
5802
  };
5751
5803
  var instantiateWrapper = (meta, relationName, relation, owner, createEntity) => {
5752
5804
  const metaBase = meta;
5753
- const lazyOptions = meta.lazyRelationOptions.get(relationName);
5754
5805
  const loadCached = (factory) => relationLoaderCache(metaBase, relationName, factory);
5806
+ const resolveOptions = () => meta.lazyRelationOptions.get(relationName);
5755
5807
  switch (relation.type) {
5756
5808
  case RelationKinds.HasOne: {
5757
5809
  const hasOne2 = relation;
5758
5810
  const localKey = hasOne2.localKey || findPrimaryKey(meta.table);
5759
5811
  const loader = () => loadCached(
5760
- () => loadHasOneRelation(meta.ctx, meta.table, relationName, hasOne2, lazyOptions)
5812
+ () => loadHasOneRelation(meta.ctx, meta.table, relationName, hasOne2, resolveOptions())
5761
5813
  );
5762
5814
  return new DefaultHasOneReference(
5763
5815
  meta.ctx,
@@ -5775,7 +5827,7 @@ var instantiateWrapper = (meta, relationName, relation, owner, createEntity) =>
5775
5827
  const hasMany2 = relation;
5776
5828
  const localKey = hasMany2.localKey || findPrimaryKey(meta.table);
5777
5829
  const loader = () => loadCached(
5778
- () => loadHasManyRelation(meta.ctx, meta.table, relationName, hasMany2, lazyOptions)
5830
+ () => loadHasManyRelation(meta.ctx, meta.table, relationName, hasMany2, resolveOptions())
5779
5831
  );
5780
5832
  return new DefaultHasManyCollection(
5781
5833
  meta.ctx,
@@ -5793,7 +5845,7 @@ var instantiateWrapper = (meta, relationName, relation, owner, createEntity) =>
5793
5845
  const belongsTo2 = relation;
5794
5846
  const targetKey = belongsTo2.localKey || findPrimaryKey(belongsTo2.target);
5795
5847
  const loader = () => loadCached(
5796
- () => loadBelongsToRelation(meta.ctx, meta.table, relationName, belongsTo2, lazyOptions)
5848
+ () => loadBelongsToRelation(meta.ctx, meta.table, relationName, belongsTo2, resolveOptions())
5797
5849
  );
5798
5850
  return new DefaultBelongsToReference(
5799
5851
  meta.ctx,
@@ -5811,7 +5863,7 @@ var instantiateWrapper = (meta, relationName, relation, owner, createEntity) =>
5811
5863
  const many = relation;
5812
5864
  const localKey = many.localKey || findPrimaryKey(meta.table);
5813
5865
  const loader = () => loadCached(
5814
- () => loadBelongsToManyRelation(meta.ctx, meta.table, relationName, many, lazyOptions)
5866
+ () => loadBelongsToManyRelation(meta.ctx, meta.table, relationName, many, resolveOptions())
5815
5867
  );
5816
5868
  return new DefaultManyToManyCollection(
5817
5869
  meta.ctx,
@@ -5918,6 +5970,58 @@ var createEntityFromRow = (ctx, table, row, lazyRelations = [], lazyRelationOpti
5918
5970
  return entity;
5919
5971
  };
5920
5972
 
5973
+ // src/orm/relation-preload.ts
5974
+ var collectEntities = (value) => {
5975
+ if (!value) return [];
5976
+ if (Array.isArray(value)) {
5977
+ return value.filter((item) => item && typeof item === "object");
5978
+ }
5979
+ if (typeof value === "object") {
5980
+ return [value];
5981
+ }
5982
+ return [];
5983
+ };
5984
+ var loadRelation = async (entity, relationName) => {
5985
+ const wrapper = entity[relationName];
5986
+ if (!wrapper) return [];
5987
+ if (typeof wrapper.load === "function") {
5988
+ const loaded = await wrapper.load();
5989
+ return collectEntities(loaded);
5990
+ }
5991
+ if (typeof wrapper.getItems === "function") {
5992
+ return collectEntities(wrapper.getItems());
5993
+ }
5994
+ if (typeof wrapper.get === "function") {
5995
+ return collectEntities(wrapper.get());
5996
+ }
5997
+ return collectEntities(wrapper);
5998
+ };
5999
+ var setLazyOptionsIfEmpty = (entity, relationName, options) => {
6000
+ if (!options) return;
6001
+ const meta = getEntityMeta(entity);
6002
+ if (!meta || meta.lazyRelationOptions.has(relationName)) return;
6003
+ meta.lazyRelationOptions.set(relationName, options);
6004
+ };
6005
+ var preloadRelationIncludes = async (entities, includeTree, depth = 0) => {
6006
+ if (!entities.length) return;
6007
+ const entries = Object.entries(includeTree);
6008
+ if (!entries.length) return;
6009
+ for (const [relationName, node] of entries) {
6010
+ const shouldLoad = depth > 0 || Boolean(node.include);
6011
+ if (!shouldLoad) continue;
6012
+ for (const entity of entities) {
6013
+ setLazyOptionsIfEmpty(entity, relationName, node.options);
6014
+ }
6015
+ const loaded = await Promise.all(
6016
+ entities.map((entity) => loadRelation(entity, relationName))
6017
+ );
6018
+ const relatedEntities = loaded.flat();
6019
+ if (node.include && relatedEntities.length) {
6020
+ await preloadRelationIncludes(relatedEntities, node.include, depth + 1);
6021
+ }
6022
+ }
6023
+ };
6024
+
5921
6025
  // src/orm/execute.ts
5922
6026
  var flattenResults = (results) => {
5923
6027
  const rows = [];
@@ -5940,14 +6044,17 @@ var executeWithContexts = async (execCtx, entityCtx, qb) => {
5940
6044
  const rows = flattenResults(executed);
5941
6045
  const lazyRelations = qb.getLazyRelations();
5942
6046
  const lazyRelationOptions = qb.getLazyRelationOptions();
6047
+ const includeTree = qb.getIncludeTree();
5943
6048
  if (ast.setOps && ast.setOps.length > 0) {
5944
6049
  const proxies = rows.map((row) => createEntityProxy(entityCtx, qb.getTable(), row, lazyRelations, lazyRelationOptions));
5945
6050
  await loadLazyRelationsForTable(entityCtx, qb.getTable(), lazyRelations, lazyRelationOptions);
6051
+ await preloadRelationIncludes(proxies, includeTree);
5946
6052
  return proxies;
5947
6053
  }
5948
6054
  const hydrated = hydrateRows(rows, qb.getHydrationPlan());
5949
6055
  const entities = hydrated.map((row) => createEntityFromRow(entityCtx, qb.getTable(), row, lazyRelations, lazyRelationOptions));
5950
6056
  await loadLazyRelationsForTable(entityCtx, qb.getTable(), lazyRelations, lazyRelationOptions);
6057
+ await preloadRelationIncludes(entities, includeTree);
5951
6058
  return entities;
5952
6059
  };
5953
6060
  var executePlainWithContexts = async (execCtx, qb) => {
@@ -6574,6 +6681,7 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
6574
6681
  lazyRelations;
6575
6682
  lazyRelationOptions;
6576
6683
  entityConstructor;
6684
+ includeTree;
6577
6685
  /**
6578
6686
  * Creates a new SelectQueryBuilder instance
6579
6687
  * @param table - Table definition to query
@@ -6581,7 +6689,7 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
6581
6689
  * @param hydration - Optional hydration manager
6582
6690
  * @param dependencies - Optional query builder dependencies
6583
6691
  */
6584
- constructor(table, state, hydration, dependencies, lazyRelations, lazyRelationOptions, entityConstructor) {
6692
+ constructor(table, state, hydration, dependencies, lazyRelations, lazyRelationOptions, entityConstructor, includeTree) {
6585
6693
  const deps = resolveSelectQueryBuilderDependencies(dependencies);
6586
6694
  this.env = { table, deps };
6587
6695
  const createAstService = (nextState) => deps.createQueryAstService(table, nextState);
@@ -6594,6 +6702,7 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
6594
6702
  this.lazyRelations = new Set(lazyRelations ?? []);
6595
6703
  this.lazyRelationOptions = new Map(lazyRelationOptions ?? []);
6596
6704
  this.entityConstructor = entityConstructor;
6705
+ this.includeTree = includeTree ?? {};
6597
6706
  this.columnSelector = deps.createColumnSelector(this.env);
6598
6707
  const relationManager = deps.createRelationManager(this.env);
6599
6708
  this.fromFacet = new SelectFromFacet(this.env, createAstService);
@@ -6610,7 +6719,7 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
6610
6719
  * @param lazyRelations - Updated lazy relations set
6611
6720
  * @returns New SelectQueryBuilder instance
6612
6721
  */
6613
- clone(context = this.context, lazyRelations = new Set(this.lazyRelations), lazyRelationOptions = new Map(this.lazyRelationOptions)) {
6722
+ clone(context = this.context, lazyRelations = new Set(this.lazyRelations), lazyRelationOptions = new Map(this.lazyRelationOptions), includeTree = this.includeTree) {
6614
6723
  return new _SelectQueryBuilder(
6615
6724
  this.env.table,
6616
6725
  context.state,
@@ -6618,7 +6727,8 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
6618
6727
  this.env.deps,
6619
6728
  lazyRelations,
6620
6729
  lazyRelationOptions,
6621
- this.entityConstructor
6730
+ this.entityConstructor,
6731
+ includeTree
6622
6732
  );
6623
6733
  }
6624
6734
  /**
@@ -6906,24 +7016,22 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
6906
7016
  const nextContext = this.relationFacet.joinRelation(this.context, relationName, joinKind, extraCondition);
6907
7017
  return this.clone(nextContext);
6908
7018
  }
6909
- /**
6910
- * Includes related data in the query results
6911
- * @param relationName - Name of the relationship to include
6912
- * @param options - Optional include options
6913
- * @returns New query builder instance with the relationship inclusion
6914
- * @example
6915
- * qb.include('posts');
6916
- * @example
6917
- * qb.include('posts', { columns: ['id', 'title', 'published'] });
6918
- * @example
6919
- * qb.include('posts', {
6920
- * columns: ['id', 'title'],
6921
- * where: eq(postTable.columns.published, true)
6922
- * });
6923
- */
6924
- include(relationName, options) {
6925
- const nextContext = this.relationFacet.include(this.context, relationName, options);
6926
- return this.clone(nextContext);
7019
+ include(relationNameOrRelations, options) {
7020
+ if (typeof relationNameOrRelations === "object" && relationNameOrRelations !== null) {
7021
+ const normalized = normalizeRelationInclude(relationNameOrRelations);
7022
+ let nextContext2 = this.context;
7023
+ for (const [relationName2, node] of Object.entries(normalized)) {
7024
+ nextContext2 = this.relationFacet.include(nextContext2, relationName2, node.options);
7025
+ }
7026
+ const nextTree2 = mergeRelationIncludeTrees(this.includeTree, normalized);
7027
+ return this.clone(nextContext2, void 0, void 0, nextTree2);
7028
+ }
7029
+ const relationName = relationNameOrRelations;
7030
+ const normalizedNode = normalizeRelationIncludeNode(options);
7031
+ const nextContext = this.relationFacet.include(this.context, relationName, normalizedNode.options);
7032
+ const shouldStore = Boolean(normalizedNode.include || normalizedNode.options);
7033
+ const nextTree = shouldStore ? mergeRelationIncludeTrees(this.includeTree, { [relationName]: normalizedNode }) : this.includeTree;
7034
+ return this.clone(nextContext, void 0, void 0, nextTree);
6927
7035
  }
6928
7036
  /**
6929
7037
  * Includes a relation lazily in the query results
@@ -7007,6 +7115,12 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
7007
7115
  getLazyRelationOptions() {
7008
7116
  return new Map(this.lazyRelationOptions);
7009
7117
  }
7118
+ /**
7119
+ * Gets normalized nested include information for runtime preloading.
7120
+ */
7121
+ getIncludeTree() {
7122
+ return cloneRelationIncludeTree(this.includeTree);
7123
+ }
7010
7124
  /**
7011
7125
  * Gets the table definition for this query builder
7012
7126
  * @returns Table definition
@@ -7543,6 +7657,9 @@ var entityRef = (ctor) => {
7543
7657
  }
7544
7658
  return tableRef(table);
7545
7659
  };
7660
+ var entityRefs = (...ctors) => {
7661
+ return ctors.map((ctor) => entityRef(ctor));
7662
+ };
7546
7663
 
7547
7664
  // src/query-builder/select-helpers.ts
7548
7665
  function sel(table, ...cols) {
@@ -12969,6 +13086,7 @@ export {
12969
13086
  div,
12970
13087
  endOfMonth,
12971
13088
  entityRef,
13089
+ entityRefs,
12972
13090
  eq,
12973
13091
  esel,
12974
13092
  executeHydrated,