@mikro-orm/knex 6.4.17-dev.5 → 6.4.17-dev.51

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.
@@ -333,6 +333,9 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
333
333
  if (meta && !core_1.Utils.isEmpty(populate)) {
334
334
  this.buildFields(meta, populate, joinedProps, qb, qb.alias, options, true);
335
335
  }
336
+ if (options.em) {
337
+ await qb.applyJoinedFilters(options.em, options.filters);
338
+ }
336
339
  return this.rethrow(qb.getCount());
337
340
  }
338
341
  async nativeInsert(entityName, data, options = {}) {
@@ -401,14 +404,14 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
401
404
  value = this.mapDataToFieldNames(value, false, prop.embeddedProps, options.convertCustomTypes);
402
405
  }
403
406
  }
404
- if (options.convertCustomTypes && prop.customType) {
405
- params.push(prop.customType.convertToDatabaseValue(value, this.platform, { key: prop.name, mode: 'query-data' }));
406
- return;
407
- }
408
407
  if (typeof value === 'undefined' && this.platform.usesDefaultKeyword()) {
409
408
  params.push((0, core_1.raw)('default'));
410
409
  return;
411
410
  }
411
+ if (options.convertCustomTypes && prop.customType) {
412
+ params.push(prop.customType.convertToDatabaseValue(value, this.platform, { key: prop.name, mode: 'query-data' }));
413
+ return;
414
+ }
412
415
  params.push(value);
413
416
  };
414
417
  if (fields.length > 0 || this.platform.usesDefaultKeyword()) {
@@ -1156,7 +1159,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
1156
1159
  if (!join && parentAlias === qb.alias) {
1157
1160
  continue;
1158
1161
  }
1159
- if (![core_1.ReferenceKind.SCALAR, core_1.ReferenceKind.EMBEDDED].includes(prop.kind) && typeof childOrder === 'object') {
1162
+ if (join && ![core_1.ReferenceKind.SCALAR, core_1.ReferenceKind.EMBEDDED].includes(prop.kind) && typeof childOrder === 'object') {
1160
1163
  const children = this.buildPopulateOrderBy(qb, meta2, core_1.Utils.asArray(childOrder), path, explicit, propAlias);
1161
1164
  orderBy.push(...children);
1162
1165
  continue;
@@ -1311,16 +1314,19 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
1311
1314
  ret.push('*');
1312
1315
  }
1313
1316
  if (ret.length > 0 && !hasExplicitFields && addFormulas) {
1314
- meta.props
1315
- .filter(prop => prop.formula && !lazyProps.includes(prop))
1316
- .forEach(prop => {
1317
- const a = this.platform.quoteIdentifier(alias);
1318
- const aliased = this.platform.quoteIdentifier(prop.fieldNames[0]);
1319
- ret.push((0, core_1.raw)(`${prop.formula(a)} as ${aliased}`));
1320
- });
1321
- meta.props
1322
- .filter(prop => !prop.object && (prop.hasConvertToDatabaseValueSQL || prop.hasConvertToJSValueSQL))
1323
- .forEach(prop => ret.push(prop.name));
1317
+ for (const prop of meta.props) {
1318
+ if (lazyProps.includes(prop)) {
1319
+ continue;
1320
+ }
1321
+ if (prop.formula) {
1322
+ const a = this.platform.quoteIdentifier(alias);
1323
+ const aliased = this.platform.quoteIdentifier(prop.fieldNames[0]);
1324
+ ret.push((0, core_1.raw)(`${prop.formula(a)} as ${aliased}`));
1325
+ }
1326
+ if (!prop.object && (prop.hasConvertToDatabaseValueSQL || prop.hasConvertToJSValueSQL)) {
1327
+ ret.push(prop.name);
1328
+ }
1329
+ }
1324
1330
  }
1325
1331
  // add joined relations after the root entity fields
1326
1332
  if (joinedProps.length > 0) {
@@ -204,7 +204,14 @@ class MySqlSchemaHelper extends SchemaHelper_1.SchemaHelper {
204
204
  col.defaultTo(null);
205
205
  }
206
206
  else {
207
- col.defaultTo(knex.raw(column.default + (column.extra ? ' ' + column.extra : '')));
207
+ const columnType = column.type.toLowerCase();
208
+ // https://dev.mysql.com/doc/refman/9.0/en/data-type-defaults.html
209
+ const needsExpression = ['blob', 'text', 'json', 'point', 'linestring', 'polygon', 'multipoint', 'multilinestring', 'multipolygon', 'geometrycollection'].some(type => columnType.startsWith(type));
210
+ let defaultSql = needsExpression && !column.default.startsWith('(') ? `(${column.default})` : column.default;
211
+ if (column.extra) {
212
+ defaultSql += ' ' + column.extra;
213
+ }
214
+ col.defaultTo(knex.raw(defaultSql));
208
215
  }
209
216
  }
210
217
  return col;
package/index.mjs CHANGED
@@ -219,12 +219,14 @@ export const compareBuffers = mod.compareBuffers;
219
219
  export const compareObjects = mod.compareObjects;
220
220
  export const createSqlFunction = mod.createSqlFunction;
221
221
  export const defineConfig = mod.defineConfig;
222
+ export const defineEntity = mod.defineEntity;
222
223
  export const equals = mod.equals;
223
224
  export const getOnConflictFields = mod.getOnConflictFields;
224
225
  export const getOnConflictReturningFields = mod.getOnConflictReturningFields;
225
226
  export const helper = mod.helper;
226
227
  export const knex = mod.knex;
227
228
  export const parseJsonSafe = mod.parseJsonSafe;
229
+ export const quote = mod.quote;
228
230
  export const raw = mod.raw;
229
231
  export const ref = mod.ref;
230
232
  export const rel = mod.rel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.4.17-dev.5",
3
+ "version": "6.4.17-dev.51",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -66,7 +66,7 @@
66
66
  "@mikro-orm/core": "^6.4.16"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.4.17-dev.5",
69
+ "@mikro-orm/core": "6.4.17-dev.51",
70
70
  "better-sqlite3": "*",
71
71
  "libsql": "*",
72
72
  "mariadb": "*"
@@ -49,13 +49,14 @@ class CriteriaNodeFactory {
49
49
  static createObjectItemNode(metadata, entityName, node, payload, key, meta) {
50
50
  const prop = meta?.properties[key];
51
51
  const childEntity = prop && prop.kind !== core_1.ReferenceKind.SCALAR ? prop.type : entityName;
52
- if (prop?.customType instanceof core_1.JsonType) {
52
+ const isNotEmbedded = prop?.kind !== core_1.ReferenceKind.EMBEDDED;
53
+ if (isNotEmbedded && prop?.customType instanceof core_1.JsonType) {
53
54
  return this.createScalarNode(metadata, childEntity, payload[key], node, key);
54
55
  }
55
56
  if (prop?.kind === core_1.ReferenceKind.SCALAR && payload[key] != null && Object.keys(payload[key]).some(f => core_1.Utils.isGroupOperator(f))) {
56
57
  throw core_1.ValidationError.cannotUseGroupOperatorsInsideScalars(entityName, prop.name, payload);
57
58
  }
58
- if (prop?.kind !== core_1.ReferenceKind.EMBEDDED) {
59
+ if (isNotEmbedded) {
59
60
  return this.createNode(metadata, childEntity, payload[key], node, key);
60
61
  }
61
62
  if (payload[key] == null) {
@@ -72,11 +73,12 @@ class CriteriaNodeFactory {
72
73
  throw core_1.ValidationError.cannotUseOperatorsInsideEmbeddables(entityName, prop.name, payload);
73
74
  }
74
75
  const map = Object.keys(payload[key]).reduce((oo, k) => {
75
- if (!prop.embeddedProps[k] && !allowedOperators.includes(k)) {
76
+ const embeddedProp = prop.embeddedProps[k] ?? Object.values(prop.embeddedProps).find(p => p.name === k);
77
+ if (!embeddedProp && !allowedOperators.includes(k)) {
76
78
  throw core_1.ValidationError.invalidEmbeddableQuery(entityName, k, prop.type);
77
79
  }
78
- if (prop.embeddedProps[k]) {
79
- oo[prop.embeddedProps[k].name] = payload[key][k];
80
+ if (embeddedProp) {
81
+ oo[embeddedProp.name] = payload[key][k];
80
82
  }
81
83
  else if (typeof payload[key][k] === 'object') {
82
84
  oo[k] = JSON.stringify(payload[key][k]);
@@ -1,4 +1,4 @@
1
- import { type Configuration, type DeferMode, type Dictionary, type EntityMetadata, type EntityProperty, type NamingStrategy } from '@mikro-orm/core';
1
+ import { type Configuration, type DeferMode, type Dictionary, type EntityMetadata, type EntityProperty, type NamingStrategy, type IndexCallback } from '@mikro-orm/core';
2
2
  import type { SchemaHelper } from './SchemaHelper';
3
3
  import type { CheckDef, Column, ForeignKey, IndexDef } from '../typings';
4
4
  import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
@@ -53,12 +53,13 @@ export declare class DatabaseTable {
53
53
  private getPropertyTypeForForeignKey;
54
54
  private getPropertyTypeForColumn;
55
55
  private getPropertyDefaultValue;
56
+ private processIndexExpression;
56
57
  addIndex(meta: EntityMetadata, index: {
57
- properties: string | string[];
58
+ properties?: string | string[];
58
59
  name?: string;
59
60
  type?: string;
60
- expression?: string;
61
- deferMode?: DeferMode;
61
+ expression?: string | IndexCallback<any>;
62
+ deferMode?: DeferMode | `${DeferMode}`;
62
63
  options?: Dictionary;
63
64
  }, type: 'index' | 'unique' | 'primary'): void;
64
65
  addCheck(check: CheckDef): void;
@@ -115,6 +115,7 @@ class DatabaseTable {
115
115
  localTableName: this.getShortestName(),
116
116
  referencedColumnNames: prop.referencedColumnNames,
117
117
  referencedTableName: schema ? `${schema}.${prop.referencedTableName}` : prop.referencedTableName,
118
+ createForeignKeyConstraint: prop.createForeignKeyConstraint,
118
119
  };
119
120
  const cascade = prop.cascade.includes(core_1.Cascade.REMOVE) || prop.cascade.includes(core_1.Cascade.ALL);
120
121
  if (prop.deleteRule || cascade || prop.nullable) {
@@ -699,6 +700,18 @@ class DatabaseTable {
699
700
  }
700
701
  return '' + val;
701
702
  }
703
+ processIndexExpression(expression, meta) {
704
+ if (expression instanceof Function) {
705
+ const exp = expression({ name: this.name, schema: this.schema, toString() {
706
+ if (this.schema) {
707
+ return `${this.schema}.${this.name}`;
708
+ }
709
+ return this.name;
710
+ } }, meta.createColumnMappingObject());
711
+ return exp instanceof core_1.RawQueryFragment ? this.platform.formatQuery(exp.sql, exp.params) : exp;
712
+ }
713
+ return expression;
714
+ }
702
715
  addIndex(meta, index, type) {
703
716
  const properties = core_1.Utils.unique(core_1.Utils.flatten(core_1.Utils.asArray(index.properties).map(prop => {
704
717
  const parts = prop.split('.');
@@ -740,7 +753,7 @@ class DatabaseTable {
740
753
  primary: type === 'primary',
741
754
  unique: type !== 'index',
742
755
  type: index.type,
743
- expression: index.expression,
756
+ expression: this.processIndexExpression(index.expression, meta),
744
757
  options: index.options,
745
758
  deferMode: index.deferMode,
746
759
  });
@@ -321,7 +321,7 @@ class SchemaHelper {
321
321
  });
322
322
  }
323
323
  createForeignKey(table, foreignKey, schema) {
324
- if (!this.options.createForeignKeyConstraints) {
324
+ if (!this.options.createForeignKeyConstraints || !foreignKey.createForeignKeyConstraint) {
325
325
  return;
326
326
  }
327
327
  const builder = table
package/typings.d.ts CHANGED
@@ -59,6 +59,7 @@ export interface ForeignKey {
59
59
  updateRule?: string;
60
60
  deleteRule?: string;
61
61
  deferMode?: DeferMode;
62
+ createForeignKeyConstraint: boolean;
62
63
  }
63
64
  export interface IndexDef {
64
65
  columnNames: string[];
@@ -74,7 +75,7 @@ export interface IndexDef {
74
75
  storageEngineIndexType?: 'hash' | 'btree';
75
76
  predicate?: Knex.QueryBuilder;
76
77
  }>;
77
- deferMode?: DeferMode;
78
+ deferMode?: DeferMode | `${DeferMode}`;
78
79
  }
79
80
  export interface CheckDef<T = unknown> {
80
81
  name: string;