@mikro-orm/knex 5.9.4-dev.1 → 5.9.4

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.
@@ -357,7 +357,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
357
357
  options.convertCustomTypes ?? (options.convertCustomTypes = true);
358
358
  const meta = this.metadata.get(entityName);
359
359
  if (options.upsert) {
360
- const uniqueFields = options.onConflictFields ?? (core_1.Utils.isPlainObject(where[0]) ? Object.keys(where[0]) : meta.primaryKeys);
360
+ const uniqueFields = options.onConflictFields ?? (core_1.Utils.isPlainObject(where[0]) ? Object.keys(where[0]).flatMap(key => core_1.Utils.splitPrimaryKeys(key)) : meta.primaryKeys);
361
361
  const qb = this.createQueryBuilder(entityName, options.ctx, 'write', options.convertCustomTypes).withSchema(this.getSchemaName(meta, options));
362
362
  const returning = (0, core_1.getOnConflictReturningFields)(meta, data[0], uniqueFields, options);
363
363
  qb.insert(data)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "5.9.4-dev.1",
3
+ "version": "5.9.4",
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",
@@ -63,12 +63,12 @@
63
63
  "sqlstring": "2.3.3"
64
64
  },
65
65
  "devDependencies": {
66
- "@mikro-orm/core": "^5.9.3"
66
+ "@mikro-orm/core": "^5.9.4"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "5.9.4-dev.1",
70
- "@mikro-orm/entity-generator": "5.9.4-dev.1",
71
- "@mikro-orm/migrations": "5.9.4-dev.1",
69
+ "@mikro-orm/core": "^5.0.0",
70
+ "@mikro-orm/entity-generator": "^5.0.0",
71
+ "@mikro-orm/migrations": "^5.0.0",
72
72
  "better-sqlite3": "*",
73
73
  "mssql": "*",
74
74
  "mysql": "*",
@@ -57,15 +57,25 @@ class CriteriaNodeFactory {
57
57
  }, {});
58
58
  return this.createNode(metadata, entityName, map, node, item);
59
59
  }
60
- const operator = Object.keys(payload[item]).some(f => core_1.Utils.isOperator(f));
60
+ // array operators can be used on embedded properties
61
+ const allowedOperators = ['$contains', '$contained', '$overlap'];
62
+ const operator = Object.keys(payload[item]).some(f => core_1.Utils.isOperator(f) && !allowedOperators.includes(f));
61
63
  if (operator) {
62
64
  throw core_1.ValidationError.cannotUseOperatorsInsideEmbeddables(entityName, prop.name, payload);
63
65
  }
64
66
  const map = Object.keys(payload[item]).reduce((oo, k) => {
65
- if (!prop.embeddedProps[k]) {
67
+ if (!prop.embeddedProps[k] && !allowedOperators.includes(k)) {
66
68
  throw core_1.ValidationError.invalidEmbeddableQuery(entityName, k, prop.type);
67
69
  }
68
- oo[prop.embeddedProps[k].name] = payload[item][k];
70
+ if (prop.embeddedProps[k]) {
71
+ oo[prop.embeddedProps[k].name] = payload[item][k];
72
+ }
73
+ else if (typeof payload[item][k] === 'object') {
74
+ oo[k] = JSON.stringify(payload[item][k]);
75
+ }
76
+ else {
77
+ oo[k] = payload[item][k];
78
+ }
69
79
  return oo;
70
80
  }, {});
71
81
  return this.createNode(metadata, entityName, map, node, item);
@@ -72,7 +72,7 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
72
72
  }
73
73
  else if (this.isPrefixed(k) || core_1.Utils.isOperator(k) || !childAlias) {
74
74
  const idx = prop.referencedPKs.indexOf(k);
75
- const key = idx !== -1 && !childAlias ? prop.joinColumns[idx] : k;
75
+ const key = idx !== -1 && !childAlias && ![core_1.ReferenceType.ONE_TO_MANY, core_1.ReferenceType.MANY_TO_MANY].includes(prop.reference) ? prop.joinColumns[idx] : k;
76
76
  if (key in o) {
77
77
  const $and = o.$and ?? [];
78
78
  $and.push({ [key]: o[key] }, { [key]: payload[k] });
@@ -55,7 +55,7 @@ class DatabaseSchema {
55
55
  table.comment = meta.comment;
56
56
  meta.props
57
57
  .filter(prop => this.shouldHaveColumn(meta, prop))
58
- .forEach(prop => table.addColumnFromProperty(prop, meta));
58
+ .forEach(prop => table.addColumnFromProperty(prop, meta, config));
59
59
  meta.indexes.forEach(index => table.addIndex(meta, index, 'index'));
60
60
  meta.uniques.forEach(index => table.addIndex(meta, index, 'unique'));
61
61
  table.addIndex(meta, { properties: meta.props.filter(prop => prop.primary).map(prop => prop.name) }, 'primary');
@@ -1,4 +1,4 @@
1
- import { type Dictionary, type EntityMetadata, type EntityProperty, type NamingStrategy } from '@mikro-orm/core';
1
+ import { type Dictionary, type EntityMetadata, type EntityProperty, type NamingStrategy, type Configuration } from '@mikro-orm/core';
2
2
  import type { SchemaHelper } from './SchemaHelper';
3
3
  import type { Check, Column, ForeignKey, Index } from '../typings';
4
4
  import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
@@ -22,7 +22,7 @@ export declare class DatabaseTable {
22
22
  getChecks(): Check[];
23
23
  init(cols: Column[], indexes: Index[] | undefined, checks: Check<unknown>[] | undefined, pks: string[], fks?: Dictionary<ForeignKey>, enums?: Dictionary<string[]>): void;
24
24
  addColumn(column: Column): void;
25
- addColumnFromProperty(prop: EntityProperty, meta: EntityMetadata): void;
25
+ addColumnFromProperty(prop: EntityProperty, meta: EntityMetadata, config: Configuration): void;
26
26
  private getIndexName;
27
27
  getEntityDeclaration(namingStrategy: NamingStrategy, schemaHelper: SchemaHelper): EntityMetadata;
28
28
  /**
@@ -52,7 +52,7 @@ class DatabaseTable {
52
52
  addColumn(column) {
53
53
  this.columns[column.name] = column;
54
54
  }
55
- addColumnFromProperty(prop, meta) {
55
+ addColumnFromProperty(prop, meta, config) {
56
56
  prop.fieldNames.forEach((field, idx) => {
57
57
  var _a;
58
58
  const type = prop.enum ? 'enum' : prop.columnTypes[idx];
@@ -99,7 +99,7 @@ class DatabaseTable {
99
99
  });
100
100
  if ([core_1.ReferenceType.MANY_TO_ONE, core_1.ReferenceType.ONE_TO_ONE].includes(prop.reference)) {
101
101
  const constraintName = this.getIndexName(true, prop.fieldNames, 'foreign');
102
- let schema = prop.targetMeta.schema === '*' ? this.schema : prop.targetMeta.schema ?? this.schema;
102
+ let schema = prop.targetMeta.root.schema === '*' ? this.schema : (prop.targetMeta.root.schema ?? config.get('schema', this.platform.getDefaultSchemaName()));
103
103
  if (prop.referencedTableName.includes('.')) {
104
104
  schema = undefined;
105
105
  }
@@ -17,12 +17,12 @@ export declare class SchemaComparator {
17
17
  * operations to change the schema stored in fromSchema to the schema that is
18
18
  * stored in toSchema.
19
19
  */
20
- compare(fromSchema: DatabaseSchema, toSchema: DatabaseSchema): SchemaDifference;
20
+ compare(fromSchema: DatabaseSchema, toSchema: DatabaseSchema, inverseDiff?: SchemaDifference): SchemaDifference;
21
21
  /**
22
22
  * Returns the difference between the tables fromTable and toTable.
23
23
  * If there are no differences this method returns the boolean false.
24
24
  */
25
- diffTable(fromTable: DatabaseTable, toTable: DatabaseTable): TableDifference | false;
25
+ diffTable(fromTable: DatabaseTable, toTable: DatabaseTable, inverseTableDiff?: TableDifference): TableDifference | false;
26
26
  /**
27
27
  * Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
28
28
  * however ambiguities between different possibilities should not lead to renaming at all.
@@ -19,7 +19,7 @@ class SchemaComparator {
19
19
  * operations to change the schema stored in fromSchema to the schema that is
20
20
  * stored in toSchema.
21
21
  */
22
- compare(fromSchema, toSchema) {
22
+ compare(fromSchema, toSchema, inverseDiff) {
23
23
  const diff = { newTables: {}, removedTables: {}, changedTables: {}, orphanedForeignKeys: [], newNamespaces: new Set(), removedNamespaces: new Set(), fromSchema };
24
24
  const foreignKeysToTable = {};
25
25
  for (const namespace of toSchema.getNamespaces()) {
@@ -40,7 +40,7 @@ class SchemaComparator {
40
40
  diff.newTables[tableName] = toSchema.getTable(tableName);
41
41
  }
42
42
  else {
43
- const tableDifferences = this.diffTable(fromSchema.getTable(tableName), toSchema.getTable(tableName));
43
+ const tableDifferences = this.diffTable(fromSchema.getTable(tableName), toSchema.getTable(tableName), inverseDiff?.changedTables[tableName]);
44
44
  if (tableDifferences !== false) {
45
45
  diff.changedTables[tableName] = tableDifferences;
46
46
  }
@@ -88,7 +88,7 @@ class SchemaComparator {
88
88
  * Returns the difference between the tables fromTable and toTable.
89
89
  * If there are no differences this method returns the boolean false.
90
90
  */
91
- diffTable(fromTable, toTable) {
91
+ diffTable(fromTable, toTable, inverseTableDiff) {
92
92
  let changes = 0;
93
93
  const tableDifferences = {
94
94
  name: fromTable.getShortestName(),
@@ -148,7 +148,7 @@ class SchemaComparator {
148
148
  this.log(`column ${tableDifferences.name}.${column.name} changed`, { changedProperties });
149
149
  changes++;
150
150
  }
151
- this.detectColumnRenamings(tableDifferences);
151
+ this.detectColumnRenamings(tableDifferences, inverseTableDiff);
152
152
  const fromTableIndexes = fromTable.getIndexes();
153
153
  const toTableIndexes = toTable.getIndexes();
154
154
  // See if all the indexes in "from" table exist in "to" table
@@ -240,7 +240,7 @@ class SchemaComparator {
240
240
  * Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
241
241
  * however ambiguities between different possibilities should not lead to renaming at all.
242
242
  */
243
- detectColumnRenamings(tableDifferences) {
243
+ detectColumnRenamings(tableDifferences, inverseTableDiff) {
244
244
  const renameCandidates = {};
245
245
  for (const addedColumn of Object.values(tableDifferences.addedColumns)) {
246
246
  for (const removedColumn of Object.values(tableDifferences.removedColumns)) {
@@ -248,6 +248,10 @@ class SchemaComparator {
248
248
  if (diff.size !== 0) {
249
249
  continue;
250
250
  }
251
+ const renamedColumn = inverseTableDiff?.renamedColumns[addedColumn.name];
252
+ if (renamedColumn && renamedColumn?.name !== removedColumn.name) {
253
+ continue;
254
+ }
251
255
  renameCandidates[addedColumn.name] = renameCandidates[addedColumn.name] ?? [];
252
256
  renameCandidates[addedColumn.name].push([removedColumn, addedColumn]);
253
257
  }
@@ -442,8 +446,8 @@ class SchemaComparator {
442
446
  return defaultValueFrom === defaultValueTo;
443
447
  }
444
448
  if (to.mappedType instanceof core_1.JsonType) {
445
- const defaultValueFrom = (0, core_1.parseJsonSafe)(from.default.replace(/^'(.*)'$/, '$1'));
446
- const defaultValueTo = (0, core_1.parseJsonSafe)(to.default?.replace(/^'(.*)'$/, '$1'));
449
+ const defaultValueFrom = (0, core_1.parseJsonSafe)(from.default.replace(/^(_\w+\\)?'(.*?)\\?'$/, '$2'));
450
+ const defaultValueTo = (0, core_1.parseJsonSafe)(to.default?.replace(/^\(?'(.*?)'\)?$/, '$1'));
447
451
  return core_1.Utils.equals(defaultValueFrom, defaultValueTo);
448
452
  }
449
453
  if (to.mappedType instanceof core_1.DateTimeType && from.default && to.default) {
@@ -149,7 +149,7 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
149
149
  const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
150
150
  const comparator = new SchemaComparator_1.SchemaComparator(this.platform);
151
151
  const diffUp = comparator.compare(fromSchema, toSchema);
152
- const diffDown = comparator.compare(toSchema, fromSchema);
152
+ const diffDown = comparator.compare(toSchema, fromSchema, diffUp);
153
153
  return {
154
154
  up: await this.diffToSQL(diffUp, options),
155
155
  down: this.platform.supportsDownMigrations() ? await this.diffToSQL(diffDown, options) : '',
@@ -219,7 +219,7 @@ class SchemaGenerator extends core_1.AbstractSchemaGenerator {
219
219
  if (schema && schemaName === '*') {
220
220
  return `${schema}.${referencedTableName.replace(/^\*\./, '')}`;
221
221
  }
222
- if (schemaName === this.platform.getDefaultSchemaName()) {
222
+ if (!schemaName || schemaName === this.platform.getDefaultSchemaName()) {
223
223
  return tableName;
224
224
  }
225
225
  return `${schemaName}.${tableName}`;