@mikro-orm/knex 7.0.0-dev.3 → 7.0.0-dev.5

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.
@@ -252,6 +252,9 @@ export class AbstractSqlDriver extends DatabaseDriver {
252
252
  : meta2.props.filter(prop => this.platform.shouldHaveColumn(prop, hint.children || []));
253
253
  const tz = this.platform.getTimezone();
254
254
  for (const prop of targetProps) {
255
+ if (prop.fieldNames.every(name => typeof root[`${relationAlias}__${name}`] === 'undefined')) {
256
+ continue;
257
+ }
255
258
  if (prop.fieldNames.length > 1) { // composite keys
256
259
  const fk = prop.fieldNames.map(name => root[`${relationAlias}__${name}`]);
257
260
  const pk = Utils.mapFlatCompositePrimaryKey(fk, prop);
@@ -927,11 +930,11 @@ export class AbstractSqlDriver extends DatabaseDriver {
927
930
  const fields = [];
928
931
  const populate = options.populate ?? [];
929
932
  const joinedProps = this.joinedProps(meta, populate, options);
930
- const shouldHaveColumn = (prop, populate, fields) => {
933
+ const shouldHaveColumn = (meta, prop, populate, fields) => {
931
934
  if (!this.platform.shouldHaveColumn(prop, populate, options.exclude)) {
932
935
  return false;
933
936
  }
934
- if (!fields || fields.includes('*') || prop.primary) {
937
+ if (!fields || fields.includes('*') || prop.primary || meta.root.discriminatorColumn === prop.name) {
935
938
  return true;
936
939
  }
937
940
  return fields.some(f => f === prop.name || f.toString().startsWith(prop.name + '.'));
@@ -941,7 +944,7 @@ export class AbstractSqlDriver extends DatabaseDriver {
941
944
  if (options.parentJoinPath) {
942
945
  // alias all fields in the primary table
943
946
  meta.props
944
- .filter(prop => shouldHaveColumn(prop, populate, options.explicitFields))
947
+ .filter(prop => shouldHaveColumn(meta, prop, populate, options.explicitFields))
945
948
  .forEach(prop => fields.push(...this.mapPropToFieldNames(qb, prop, options.parentTableAlias)));
946
949
  }
947
950
  for (const hint of joinedProps) {
@@ -1289,6 +1292,9 @@ export class AbstractSqlDriver extends DatabaseDriver {
1289
1292
  if (!options.fields.includes('*') && !options.fields.includes(`${qb.alias}.*`)) {
1290
1293
  ret.unshift(...meta.primaryKeys.filter(pk => !options.fields.includes(pk)));
1291
1294
  }
1295
+ if (meta.root.discriminatorColumn && !options.fields.includes(`${qb.alias}.${meta.root.discriminatorColumn}`)) {
1296
+ ret.push(meta.root.discriminatorColumn);
1297
+ }
1292
1298
  }
1293
1299
  else if (!Utils.isEmpty(options.exclude) || lazyProps.some(p => !p.formula && (p.kind !== '1:1' || p.owner))) {
1294
1300
  const props = meta.props.filter(prop => this.platform.shouldHaveColumn(prop, populate, options.exclude, false));
@@ -65,9 +65,11 @@ export class MsSqlNativeQueryBuilder extends NativeQueryBuilder {
65
65
  this.params.push(...clause.fields.params);
66
66
  }
67
67
  else if (clause.fields.length > 0) {
68
- // const fields = clause.fields.map(field => this.quote(field));
69
- const conflictColumn = this.quote(clause.fields[0]);
70
- this.parts.push(`on ${this.getTableName()}.${conflictColumn} = tsource.${conflictColumn}`);
68
+ const fields = clause.fields.map(field => {
69
+ const col = this.quote(field);
70
+ return `${this.getTableName()}.${col} = tsource.${col}`;
71
+ });
72
+ this.parts.push(`on ${fields.join(' and ')}`);
71
73
  }
72
74
  const sourceColumns = keys.map(field => `tsource.${this.quote(field)}`).join(', ');
73
75
  const destinationColumns = keys.map(field => this.quote(field)).join(', ');
@@ -63,7 +63,6 @@ export declare abstract class BaseSqlitePlatform extends AbstractSqlPlatform {
63
63
  */
64
64
  processDateProperty(value: unknown): string | number | Date;
65
65
  getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
66
- supportsDownMigrations(): boolean;
67
66
  supportsDeferredUniqueConstraints(): boolean;
68
67
  getFullTextWhereClause(): string;
69
68
  quoteVersionValue(value: Date | number, prop: EntityProperty): Date | string | number;
@@ -84,10 +84,6 @@ export class BaseSqlitePlatform extends AbstractSqlPlatform {
84
84
  }
85
85
  return super.getIndexName(tableName, columns, type);
86
86
  }
87
- // TODO enable once tests are green
88
- supportsDownMigrations() {
89
- return false;
90
- }
91
87
  supportsDeferredUniqueConstraints() {
92
88
  return false;
93
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "7.0.0-dev.3",
3
+ "version": "7.0.0-dev.5",
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
  "type": "module",
6
6
  "exports": {
@@ -54,9 +54,9 @@
54
54
  "sqlstring": "2.3.3"
55
55
  },
56
56
  "devDependencies": {
57
- "@mikro-orm/core": "^6.4.5"
57
+ "@mikro-orm/core": "^6.4.7"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.0.0-dev.3"
60
+ "@mikro-orm/core": "7.0.0-dev.5"
61
61
  }
62
62
  }
@@ -69,16 +69,12 @@ export class CriteriaNode {
69
69
  let joinAlias = qb.getAliasForJoinPath(this.getPath());
70
70
  if (!joinAlias && this.parent && [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(this.prop.kind) && this.prop.owner) {
71
71
  joinAlias = qb.getAliasForJoinPath(this.parent.getPath());
72
- return Utils.getPrimaryKeyHash(this.prop.joinColumns.map(col => `${joinAlias ?? qb.alias}.${col}`));
72
+ return Utils.getPrimaryKeyHash(this.prop.ownColumns.map(col => `${joinAlias ?? qb.alias}.${col}`));
73
73
  }
74
74
  const alias = joinAlias ?? qb.alias;
75
75
  if (this.prop.kind === ReferenceKind.MANY_TO_MANY) {
76
76
  return Utils.getPrimaryKeyHash(this.prop.inverseJoinColumns.map(col => `${alias}.${col}`));
77
77
  }
78
- // if we found a matching join, we need to use the target table column names, as we use that alias instead of the root
79
- if (!joinAlias && this.prop.owner && this.prop.joinColumns.length > 1) {
80
- return Utils.getPrimaryKeyHash(this.prop.joinColumns.map(col => `${alias}.${col}`));
81
- }
82
78
  return Utils.getPrimaryKeyHash(this.prop.referencedColumnNames.map(col => `${alias}.${col}`));
83
79
  }
84
80
  getPath(addIndex = false) {
@@ -39,9 +39,6 @@ export class QueryBuilderHelper {
39
39
  const prop = this.getProperty(f, a);
40
40
  const fkIdx2 = prop?.fieldNames.findIndex(name => name === f) ?? -1;
41
41
  if (fkIdx2 !== -1) {
42
- if (prop?.ownColumns && !prop.ownColumns.includes(f)) {
43
- continue;
44
- }
45
42
  parts.push(this.mapper(a !== this.alias ? `${a}.${prop.fieldNames[fkIdx2]}` : prop.fieldNames[fkIdx2], type, value, alias));
46
43
  }
47
44
  else if (prop) {
@@ -62,9 +59,6 @@ export class QueryBuilderHelper {
62
59
  }
63
60
  });
64
61
  }
65
- if (parts.length === 1) {
66
- return parts[0];
67
- }
68
62
  return raw('(' + parts.map(part => this.platform.quoteIdentifier(part)).join(', ') + ')');
69
63
  }
70
64
  const rawField = RawQueryFragment.getKnownFragment(field);
@@ -473,8 +467,8 @@ export class QueryBuilderHelper {
473
467
  parts.push(sql);
474
468
  params.push(...params2);
475
469
  }
476
- else if (op === '$in' && Array.isArray(value[op]) && value[op].length === 0) {
477
- parts.push(`1 = 0`);
470
+ else if (['$in', '$nin'].includes(op) && Array.isArray(value[op]) && value[op].length === 0) {
471
+ parts.push(`1 = ${op === '$in' ? 0 : 1}`);
478
472
  }
479
473
  else if (value[op] instanceof RawQueryFragment || value[op] instanceof NativeQueryBuilder) {
480
474
  const query = value[op] instanceof NativeQueryBuilder ? value[op].toRaw() : value[op];
@@ -103,9 +103,10 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
103
103
  return super.clearDatabase(options);
104
104
  }
105
105
  await this.execute(this.helper.disableForeignKeysSQL());
106
- for (const meta of this.getOrderedMetadata(options?.schema).reverse()) {
106
+ const schema = options?.schema ?? this.config.get('schema', this.platform.getDefaultSchemaName());
107
+ for (const meta of this.getOrderedMetadata(schema).reverse()) {
107
108
  await this.driver.createQueryBuilder(meta.className, this.em?.getTransactionContext(), 'write', false)
108
- .withSchema(options?.schema)
109
+ .withSchema(schema)
109
110
  .truncate()
110
111
  .execute();
111
112
  }