@mikro-orm/knex 7.0.0-dev.7 → 7.0.0-dev.9

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.
@@ -81,6 +81,7 @@ export class AbstractSqlConnection extends Connection {
81
81
  this.logQuery(this.platform.getSavepointSQL(savepointName));
82
82
  return trx;
83
83
  }
84
+ await this.ensureConnection();
84
85
  await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
85
86
  let trxBuilder = this.client.startTransaction();
86
87
  if (options.isolationLevel) {
@@ -731,8 +731,14 @@ export class AbstractSqlDriver extends DatabaseDriver {
731
731
  const pivotMeta = this.metadata.find(coll.property.pivotEntity);
732
732
  let schema = pivotMeta.schema;
733
733
  if (schema === '*') {
734
- const ownerSchema = wrapped.getSchema() === '*' ? this.config.get('schema') : wrapped.getSchema();
735
- schema = coll.property.owner ? ownerSchema : this.config.get('schema');
734
+ if (coll.property.owner) {
735
+ schema = wrapped.getSchema() === '*' ? options?.schema ?? this.config.get('schema') : wrapped.getSchema();
736
+ }
737
+ else {
738
+ const targetMeta = coll.property.targetMeta;
739
+ const targetSchema = (coll[0] ?? snap?.[0]) && helper(coll[0] ?? snap?.[0]).getSchema();
740
+ schema = targetMeta.schema === '*' ? options?.schema ?? targetSchema ?? this.config.get('schema') : targetMeta.schema;
741
+ }
736
742
  }
737
743
  else if (schema == null) {
738
744
  schema = this.config.get('schema');
@@ -783,13 +789,15 @@ export class AbstractSqlDriver extends DatabaseDriver {
783
789
  const targetSchema = this.getSchemaName(prop.targetMeta, options) ?? this.platform.getDefaultSchemaName();
784
790
  qb.innerJoin(pivotProp1.name, targetAlias, {}, targetSchema);
785
791
  const targetFields = this.buildFields(prop.targetMeta, (options.populate ?? []), [], qb, targetAlias, options);
792
+ const additionalFields = [];
786
793
  for (const field of targetFields) {
787
794
  const f = field.toString();
788
- fields.unshift(f.includes('.') ? field : `${targetAlias}.${f}`);
795
+ additionalFields.push(f.includes('.') ? field : `${targetAlias}.${f}`);
789
796
  if (RawQueryFragment.isKnownFragment(field)) {
790
797
  qb.rawFragments.add(f);
791
798
  }
792
799
  }
800
+ fields.unshift(...additionalFields);
793
801
  // we need to handle 1:1 owner auto-joins explicitly, as the QB type is the pivot table, not the target
794
802
  populate.forEach(hint => {
795
803
  const alias = qb.getNextAlias(prop.targetMeta.tableName);
@@ -82,7 +82,9 @@ export class MsSqlNativeQueryBuilder extends NativeQueryBuilder {
82
82
  }
83
83
  this.parts.push('then update set');
84
84
  if (!clause.merge || Array.isArray(clause.merge)) {
85
- const parts = keys.map((column) => `${this.quote(column)} = tsource.${this.quote(column)}`);
85
+ const parts = keys
86
+ .filter(field => !Array.isArray(clause.fields) || !clause.fields.includes(field))
87
+ .map((column) => `${this.quote(column)} = tsource.${this.quote(column)}`);
86
88
  this.parts.push(parts.join(', '));
87
89
  }
88
90
  else if (typeof clause.merge === 'object') {
@@ -1,5 +1,5 @@
1
1
  import { AbstractSqlConnection } from '../../AbstractSqlConnection.js';
2
2
  export declare abstract class BaseSqliteConnection extends AbstractSqlConnection {
3
- connect(): Promise<void>;
3
+ connect(simple?: boolean): Promise<void>;
4
4
  getClientUrl(): string;
5
5
  }
@@ -3,9 +3,15 @@ import { CompiledQuery } from 'kysely';
3
3
  import { Utils } from '@mikro-orm/core';
4
4
  import { AbstractSqlConnection } from '../../AbstractSqlConnection.js';
5
5
  export class BaseSqliteConnection extends AbstractSqlConnection {
6
- async connect() {
6
+ async connect(simple = false) {
7
7
  await super.connect();
8
- Utils.ensureDir(dirname(this.config.get('dbName')));
8
+ if (simple) {
9
+ return;
10
+ }
11
+ const dbName = this.config.get('dbName');
12
+ if (dbName && dbName !== ':memory:') {
13
+ Utils.ensureDir(dirname(this.config.get('dbName')));
14
+ }
9
15
  await this.client.executeQuery(CompiledQuery.raw('pragma foreign_keys = on'));
10
16
  }
11
17
  getClientUrl() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "7.0.0-dev.7",
3
+ "version": "7.0.0-dev.9",
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": {
@@ -50,13 +50,13 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "kysely": "https://pkg.pr.new/kysely-org/kysely/kysely@2b7007e",
53
+ "kysely": "0.28.0",
54
54
  "sqlstring": "2.3.3"
55
55
  },
56
56
  "devDependencies": {
57
- "@mikro-orm/core": "^6.4.9"
57
+ "@mikro-orm/core": "^6.4.13"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.0.0-dev.7"
60
+ "@mikro-orm/core": "7.0.0-dev.9"
61
61
  }
62
62
  }
@@ -55,7 +55,7 @@ export class ObjectCriteriaNode extends CriteriaNode {
55
55
  }
56
56
  return { $and };
57
57
  }
58
- alias = this.autoJoin(qb, ownerAlias);
58
+ alias = this.autoJoin(qb, ownerAlias, options);
59
59
  }
60
60
  return keys.reduce((o, field) => {
61
61
  const childNode = this.payload[field];
@@ -193,7 +193,7 @@ export class ObjectCriteriaNode extends CriteriaNode {
193
193
  });
194
194
  return !primaryKeys && !nestedAlias && !operatorKeys && !embeddable;
195
195
  }
196
- autoJoin(qb, alias) {
196
+ autoJoin(qb, alias, options) {
197
197
  const nestedAlias = qb.getNextAlias(this.prop?.pivotTable ?? this.entityName);
198
198
  const customExpression = RawQueryFragment.isKnownFragment(this.key);
199
199
  const scalar = Utils.isPrimaryKey(this.payload) || this.payload instanceof RegExp || this.payload instanceof Date || customExpression;
@@ -211,7 +211,9 @@ export class ObjectCriteriaNode extends CriteriaNode {
211
211
  qb._fields = prev;
212
212
  }
213
213
  }
214
- qb.scheduleFilterCheck(path);
214
+ if (!options || options.type !== 'orderBy') {
215
+ qb.scheduleFilterCheck(path);
216
+ }
215
217
  return nestedAlias;
216
218
  }
217
219
  isPrefixed(field) {
@@ -240,9 +240,12 @@ export class QueryBuilder {
240
240
  }
241
241
  }
242
242
  prop.targetMeta.props
243
- .filter(prop => explicitFields
244
- ? explicitFields.includes(prop.name) || explicitFields.includes(`${alias}.${prop.name}`) || prop.primary
245
- : this.platform.shouldHaveColumn(prop, populate))
243
+ .filter(prop => {
244
+ if (!explicitFields) {
245
+ return this.platform.shouldHaveColumn(prop, populate);
246
+ }
247
+ return prop.primary && !explicitFields.includes(prop.name) && !explicitFields.includes(`${alias}.${prop.name}`);
248
+ })
246
249
  .forEach(prop => fields.push(...this.driver.mapPropToFieldNames(this, prop, alias)));
247
250
  return fields;
248
251
  }
@@ -275,6 +278,12 @@ export class QueryBuilder {
275
278
  }
276
279
  const cond = await em.applyFilters(join.prop.type, join.cond, filterOptions, 'read');
277
280
  if (Utils.hasObjectKeys(cond)) {
281
+ // remove nested filters, we only care about scalars here, nesting would require another join branch
282
+ for (const key of Object.keys(cond)) {
283
+ if (Utils.isPlainObject(cond[key]) && Object.keys(cond[key]).every(k => !(Utils.isOperator(k) && !['$some', '$none', '$every'].includes(k)))) {
284
+ delete cond[key];
285
+ }
286
+ }
278
287
  if (Utils.hasObjectKeys(join.cond)) {
279
288
  /* istanbul ignore next */
280
289
  join.cond = { $and: [join.cond, cond] };
@@ -363,7 +372,7 @@ export class QueryBuilder {
363
372
  convertCustomTypes: false,
364
373
  type: 'orderBy',
365
374
  });
366
- this._orderBy.push(CriteriaNodeFactory.createNode(this.metadata, this.mainAlias.entityName, processed).process(this, { matchPopulateJoins: true }));
375
+ this._orderBy.push(CriteriaNodeFactory.createNode(this.metadata, this.mainAlias.entityName, processed).process(this, { matchPopulateJoins: true, type: 'orderBy' }));
367
376
  });
368
377
  return this;
369
378
  }
@@ -1227,9 +1236,7 @@ export class QueryBuilder {
1227
1236
  }
1228
1237
  }
1229
1238
  hasToManyJoins() {
1230
- // console.log(this._joins);
1231
1239
  return Object.values(this._joins).some(join => {
1232
- // console.log(join.prop.name, join.prop.kind, [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(join.prop.kind));
1233
1240
  return [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(join.prop.kind);
1234
1241
  });
1235
1242
  }
@@ -97,6 +97,9 @@ export class DatabaseTable {
97
97
  ignoreSchemaChanges: prop.ignoreSchemaChanges,
98
98
  };
99
99
  this.columns[field].unsigned ??= this.columns[field].autoincrement;
100
+ if (this.nativeEnums[type]) {
101
+ this.columns[field].enumItems ??= this.nativeEnums[type].items;
102
+ }
100
103
  const defaultValue = this.platform.getSchemaHelper().normalizeDefaultValue(prop.defaultRaw, prop.length);
101
104
  this.columns[field].default = defaultValue;
102
105
  });
package/typings.d.ts CHANGED
@@ -170,6 +170,7 @@ export interface ICriteriaNodeProcessOptions {
170
170
  matchPopulateJoins?: boolean;
171
171
  ignoreBranching?: boolean;
172
172
  preferNoBranch?: boolean;
173
+ type?: 'orderBy';
173
174
  }
174
175
  export interface ICriteriaNode<T extends object> {
175
176
  readonly entityName: string;