metal-orm 1.0.62 → 1.0.63

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.cjs CHANGED
@@ -81,6 +81,7 @@ __export(index_exports, {
81
81
  aliasRef: () => aliasRef,
82
82
  and: () => and,
83
83
  arrayAppend: () => arrayAppend,
84
+ asType: () => asType,
84
85
  ascii: () => ascii,
85
86
  asin: () => asin,
86
87
  atan: () => atan,
@@ -146,6 +147,8 @@ __export(index_exports, {
146
147
  eq: () => eq,
147
148
  esel: () => esel,
148
149
  executeHydrated: () => executeHydrated,
150
+ executeHydratedPlain: () => executeHydratedPlain,
151
+ executeHydratedPlainWithContexts: () => executeHydratedPlainWithContexts,
149
152
  executeHydratedWithContexts: () => executeHydratedWithContexts,
150
153
  executeSchemaSql: () => executeSchemaSql,
151
154
  executeSchemaSqlFor: () => executeSchemaSqlFor,
@@ -1103,6 +1106,9 @@ var toTableRef = (table) => ({
1103
1106
  alias: hasAlias(table) ? table.alias : void 0
1104
1107
  });
1105
1108
 
1109
+ // src/core/ast/expression.ts
1110
+ var asType = (expr) => expr;
1111
+
1106
1112
  // src/core/functions/function-registry.ts
1107
1113
  var FunctionRegistry = class {
1108
1114
  renderers = /* @__PURE__ */ new Map();
@@ -3716,10 +3722,9 @@ var HydrationPlanner = class _HydrationPlanner {
3716
3722
  const rootCols = new Set(currentPlan.rootColumns);
3717
3723
  let changed = false;
3718
3724
  columns.forEach((node) => {
3719
- if (node.type !== "Column") return;
3720
- if (node.table !== this.table.name) return;
3721
- const alias = node.alias || node.name;
3722
- if (isRelationAlias(alias)) return;
3725
+ const alias = node.type === "Column" ? node.alias || node.name : node.alias;
3726
+ if (!alias || isRelationAlias(alias)) return;
3727
+ if (node.type === "Column" && node.table !== this.table.name) return;
3723
3728
  if (!rootCols.has(alias)) {
3724
3729
  rootCols.add(alias);
3725
3730
  changed = true;
@@ -6185,9 +6190,22 @@ var executeWithContexts = async (execCtx, entityCtx, qb) => {
6185
6190
  await loadLazyRelationsForTable(entityCtx, qb.getTable(), lazyRelations, lazyRelationOptions);
6186
6191
  return entities;
6187
6192
  };
6193
+ var executePlainWithContexts = async (execCtx, qb) => {
6194
+ const ast = qb.getAST();
6195
+ const compiled = execCtx.dialect.compileSelect(ast);
6196
+ const executed = await execCtx.interceptors.run({ sql: compiled.sql, params: compiled.params }, execCtx.executor);
6197
+ const rows = flattenResults(executed);
6198
+ if (ast.setOps && ast.setOps.length > 0) {
6199
+ return rows;
6200
+ }
6201
+ return hydrateRows(rows, qb.getHydrationPlan());
6202
+ };
6188
6203
  async function executeHydrated(session, qb) {
6189
6204
  return executeWithContexts(session.getExecutionContext(), session, qb);
6190
6205
  }
6206
+ async function executeHydratedPlain(session, qb) {
6207
+ return executePlainWithContexts(session.getExecutionContext(), qb);
6208
+ }
6191
6209
  async function executeHydratedWithContexts(execCtx, hydCtx, qb) {
6192
6210
  const entityCtx = hydCtx.entityContext;
6193
6211
  if (!entityCtx) {
@@ -6195,6 +6213,9 @@ async function executeHydratedWithContexts(execCtx, hydCtx, qb) {
6195
6213
  }
6196
6214
  return executeWithContexts(execCtx, entityCtx, qb);
6197
6215
  }
6216
+ async function executeHydratedPlainWithContexts(execCtx, qb) {
6217
+ return executePlainWithContexts(execCtx, qb);
6218
+ }
6198
6219
  var loadLazyRelationsForTable = async (ctx, table, lazyRelations, lazyRelationOptions) => {
6199
6220
  if (!lazyRelations.length) return;
6200
6221
  const tracked = ctx.getEntitiesForTable(table);
@@ -6328,8 +6349,11 @@ var DefaultEntityMaterializer = class {
6328
6349
  this.strategy = strategy;
6329
6350
  }
6330
6351
  materialize(ctor, row) {
6352
+ if (hasEntityMeta(row)) {
6353
+ return this.materializeEntityProxy(ctor, row);
6354
+ }
6331
6355
  const instance = this.strategy.materialize(ctor, row);
6332
- this.materializeRelations(instance, ctor);
6356
+ this.materializeRelations(instance);
6333
6357
  return instance;
6334
6358
  }
6335
6359
  materializeMany(ctor, rows) {
@@ -6338,9 +6362,9 @@ var DefaultEntityMaterializer = class {
6338
6362
  /**
6339
6363
  * Recursively materializes nested relation data.
6340
6364
  */
6341
- materializeRelations(instance, _ctor) {
6365
+ materializeRelations(instance) {
6342
6366
  rebuildRegistry();
6343
- for (const [key, value] of Object.entries(instance)) {
6367
+ for (const value of Object.values(instance)) {
6344
6368
  if (value === null || value === void 0) continue;
6345
6369
  if (typeof value === "object" && !Array.isArray(value)) {
6346
6370
  const nested = value;
@@ -6362,6 +6386,17 @@ var DefaultEntityMaterializer = class {
6362
6386
  (k) => k.endsWith("Id") || k === "createdAt" || k === "updatedAt"
6363
6387
  );
6364
6388
  }
6389
+ materializeEntityProxy(ctor, row) {
6390
+ const proxy = row;
6391
+ const baseline = this.strategy.materialize(ctor, {});
6392
+ for (const key of Object.keys(baseline)) {
6393
+ if (!Object.prototype.hasOwnProperty.call(proxy, key)) {
6394
+ proxy[key] = baseline[key];
6395
+ }
6396
+ }
6397
+ Object.setPrototypeOf(proxy, ctor.prototype);
6398
+ return proxy;
6399
+ }
6365
6400
  };
6366
6401
  var materializeAs = (ctor, results) => {
6367
6402
  const materializer = new DefaultEntityMaterializer();
@@ -6871,7 +6906,9 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
6871
6906
  select(...args) {
6872
6907
  if (args.length === 1 && typeof args[0] === "object" && args[0] !== null && typeof args[0] !== "string") {
6873
6908
  const columns = args[0];
6874
- return this.clone(this.projectionFacet.select(this.context, columns));
6909
+ return this.clone(
6910
+ this.projectionFacet.select(this.context, columns)
6911
+ );
6875
6912
  }
6876
6913
  const cols = args;
6877
6914
  const selection = {};
@@ -6882,7 +6919,9 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
6882
6919
  }
6883
6920
  selection[key] = col2;
6884
6921
  }
6885
- return this.clone(this.projectionFacet.select(this.context, selection));
6922
+ return this.clone(
6923
+ this.projectionFacet.select(this.context, selection)
6924
+ );
6886
6925
  }
6887
6926
  /**
6888
6927
  * Selects raw column expressions
@@ -6986,7 +7025,9 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
6986
7025
  */
6987
7026
  selectSubquery(alias, sub2) {
6988
7027
  const query = resolveSelectQuery(sub2);
6989
- return this.clone(this.projectionFacet.selectSubquery(this.context, alias, query));
7028
+ return this.clone(
7029
+ this.projectionFacet.selectSubquery(this.context, alias, query)
7030
+ );
6990
7031
  }
6991
7032
  /**
6992
7033
  * Adds a JOIN against a derived table (subquery with alias)
@@ -7219,7 +7260,8 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
7219
7260
  ensureDefaultSelection() {
7220
7261
  const columns = this.context.state.ast.columns;
7221
7262
  if (!columns || columns.length === 0) {
7222
- return this.select(...Object.keys(this.env.table.columns));
7263
+ const columnKeys = Object.keys(this.env.table.columns);
7264
+ return this.select(...columnKeys);
7223
7265
  }
7224
7266
  return this;
7225
7267
  }
@@ -7255,7 +7297,8 @@ var SelectQueryBuilder = class _SelectQueryBuilder {
7255
7297
  */
7256
7298
  async executePlain(ctx) {
7257
7299
  const builder = this.ensureDefaultSelection();
7258
- return executeHydrated(ctx, builder);
7300
+ const rows = await executeHydratedPlain(ctx, builder);
7301
+ return rows;
7259
7302
  }
7260
7303
  /**
7261
7304
  * Executes the query and returns results as real class instances.
@@ -13011,6 +13054,7 @@ function createPooledExecutorFactory(opts) {
13011
13054
  aliasRef,
13012
13055
  and,
13013
13056
  arrayAppend,
13057
+ asType,
13014
13058
  ascii,
13015
13059
  asin,
13016
13060
  atan,
@@ -13076,6 +13120,8 @@ function createPooledExecutorFactory(opts) {
13076
13120
  eq,
13077
13121
  esel,
13078
13122
  executeHydrated,
13123
+ executeHydratedPlain,
13124
+ executeHydratedPlainWithContexts,
13079
13125
  executeHydratedWithContexts,
13080
13126
  executeSchemaSql,
13081
13127
  executeSchemaSqlFor,