@mikro-orm/knex 5.7.15-dev.4 → 5.7.15-dev.40

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.
@@ -12,11 +12,13 @@ export declare abstract class AbstractSqlConnection extends Connection {
12
12
  isConnected(): Promise<boolean>;
13
13
  transactional<T>(cb: (trx: Transaction<Knex.Transaction>) => Promise<T>, options?: {
14
14
  isolationLevel?: IsolationLevel;
15
+ readOnly?: boolean;
15
16
  ctx?: Knex.Transaction;
16
17
  eventBroadcaster?: TransactionEventBroadcaster;
17
18
  }): Promise<T>;
18
19
  begin(options?: {
19
20
  isolationLevel?: IsolationLevel;
21
+ readOnly?: boolean;
20
22
  ctx?: Knex.Transaction;
21
23
  eventBroadcaster?: TransactionEventBroadcaster;
22
24
  }): Promise<Knex.Transaction>;
@@ -46,7 +46,10 @@ class AbstractSqlConnection extends core_1.Connection {
46
46
  if (!options.ctx) {
47
47
  await options.eventBroadcaster?.dispatchEvent(core_1.EventType.beforeTransactionStart);
48
48
  }
49
- const trx = await (options.ctx || this.client).transaction(null, { isolationLevel: options.isolationLevel });
49
+ const trx = await (options.ctx || this.client).transaction(null, {
50
+ isolationLevel: options.isolationLevel,
51
+ readOnly: options.readOnly,
52
+ });
50
53
  if (!options.ctx) {
51
54
  await options.eventBroadcaster?.dispatchEvent(core_1.EventType.afterTransactionStart, trx);
52
55
  }
@@ -335,7 +335,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
335
335
  const uniqueFields = core_1.Utils.isPlainObject(where) ? Object.keys(where) : meta.primaryKeys;
336
336
  /* istanbul ignore next */
337
337
  qb.insert(data)
338
- .onConflict(uniqueFields.map(p => meta?.properties[p]?.fieldNames[0] ?? p))
338
+ .onConflict(uniqueFields)
339
339
  .merge(Object.keys(data).filter(f => !uniqueFields.includes(f)))
340
340
  .returning(meta?.comparableProps.filter(p => !p.lazy && !p.embeddable && !(p.name in data)).map(p => p.name) ?? '*');
341
341
  }
@@ -357,7 +357,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
357
357
  const uniqueFields = core_1.Utils.isPlainObject(where[0]) ? Object.keys(where[0]) : meta.primaryKeys;
358
358
  const qb = this.createQueryBuilder(entityName, options.ctx, 'write', options.convertCustomTypes).withSchema(this.getSchemaName(meta, options));
359
359
  qb.insert(data)
360
- .onConflict(uniqueFields.map(p => meta.properties[p]?.fieldNames[0] ?? p))
360
+ .onConflict(uniqueFields)
361
361
  .merge(Object.keys(data[0]).filter(f => !uniqueFields.includes(f)))
362
362
  .returning(meta.comparableProps.filter(p => !p.lazy && !p.embeddable && !(p.name in data[0])).map(p => p.name) ?? '*');
363
363
  return qb.execute('run', false);
@@ -461,11 +461,23 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
461
461
  if (coll.property.reference === core_1.ReferenceType.ONE_TO_MANY) {
462
462
  const cols = coll.property.referencedColumnNames;
463
463
  const qb = this.createQueryBuilder(coll.property.type, ctx, 'write')
464
- .withSchema(this.getSchemaName(meta, options))
465
- .update({ [coll.property.mappedBy]: pks })
464
+ .withSchema(this.getSchemaName(meta, options));
465
+ if (coll.getSnapshot() === undefined) {
466
+ if (coll.property.orphanRemoval) {
467
+ const kqb = qb.delete({ [coll.property.mappedBy]: pks })
468
+ .getKnexQuery()
469
+ .whereNotIn(cols, insertDiff);
470
+ return this.rethrow(this.execute(kqb));
471
+ }
472
+ const kqb = qb.update({ [coll.property.mappedBy]: null })
473
+ .getKnexQuery()
474
+ .whereNotIn(cols, insertDiff);
475
+ return this.rethrow(this.execute(kqb));
476
+ }
477
+ const kqb = qb.update({ [coll.property.mappedBy]: pks })
466
478
  .getKnexQuery()
467
479
  .whereIn(cols, insertDiff);
468
- return this.rethrow(this.execute(qb));
480
+ return this.rethrow(this.execute(kqb));
469
481
  }
470
482
  /* istanbul ignore next */
471
483
  const ownerSchema = wrapped.getSchema() === '*' ? this.config.get('schema') : wrapped.getSchema();
@@ -672,9 +684,10 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
672
684
  if (!deleteDiff) {
673
685
  deleteDiff = [];
674
686
  }
687
+ const pivotMeta = this.metadata.find(prop.pivotEntity);
675
688
  if (deleteDiff === true || deleteDiff.length > 0) {
676
689
  const qb1 = this.createQueryBuilder(prop.pivotEntity, options?.ctx, 'write')
677
- .withSchema(this.getSchemaName(meta, options))
690
+ .withSchema(this.getSchemaName(pivotMeta, options))
678
691
  .unsetFlag(core_1.QueryFlag.CONVERT_CUSTOM_TYPES);
679
692
  const knex = qb1.getKnex();
680
693
  if (Array.isArray(deleteDiff)) {
@@ -700,7 +713,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
700
713
  await core_1.Utils.runSerial(items, item => {
701
714
  return this.createQueryBuilder(prop.pivotEntity, options?.ctx, 'write')
702
715
  .unsetFlag(core_1.QueryFlag.CONVERT_CUSTOM_TYPES)
703
- .withSchema(this.getSchemaName(meta, options))
716
+ .withSchema(this.getSchemaName(pivotMeta, options))
704
717
  .insert(item)
705
718
  .execute('run', false);
706
719
  });
@@ -754,7 +767,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
754
767
  }
755
768
  if (prop.reference === core_1.ReferenceType.EMBEDDED) {
756
769
  if (prop.object) {
757
- ret.push(prop.fieldNames[0]);
770
+ ret.push(prop.name);
758
771
  return;
759
772
  }
760
773
  const parts = field.split('.');
@@ -766,13 +779,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
766
779
  }
767
780
  return;
768
781
  }
769
- if (prop.formula) {
770
- ret.push(prop.name);
771
- return;
772
- }
773
- if (prop.fieldNames) {
774
- ret.push(...prop.fieldNames);
775
- }
782
+ ret.push(prop.name);
776
783
  }
777
784
  buildFields(meta, populate, joinedProps, qb, fields) {
778
785
  const lazyProps = meta.props.filter(prop => prop.lazy && !populate.some(p => p.field === prop.name || p.all));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "5.7.15-dev.4",
3
+ "version": "5.7.15-dev.40",
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,9 +66,9 @@
66
66
  "@mikro-orm/core": "^5.7.14"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~5.7.15-dev.4",
70
- "@mikro-orm/entity-generator": "~5.7.15-dev.4",
71
- "@mikro-orm/migrations": "~5.7.15-dev.4",
69
+ "@mikro-orm/core": "~5.7.15-dev.40",
70
+ "@mikro-orm/entity-generator": "~5.7.15-dev.40",
71
+ "@mikro-orm/migrations": "~5.7.15-dev.40",
72
72
  "better-sqlite3": "*",
73
73
  "mssql": "*",
74
74
  "mysql": "*",
@@ -34,7 +34,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
34
34
  private readonly context?;
35
35
  private connectionType?;
36
36
  private readonly em?;
37
- get mainAlias(): Alias;
37
+ get mainAlias(): Alias<T>;
38
38
  get alias(): string;
39
39
  get helper(): QueryBuilderHelper;
40
40
  /** @internal */
@@ -244,9 +244,16 @@ class QueryBuilder {
244
244
  return this;
245
245
  }
246
246
  onConflict(fields = []) {
247
+ const meta = this.mainAlias.metadata;
247
248
  this.ensureNotFinalized();
248
- this._onConflict = this._onConflict || [];
249
- this._onConflict.push({ fields: core_1.Utils.asArray(fields).map(f => f.toString()) });
249
+ this._onConflict ?? (this._onConflict = []);
250
+ this._onConflict.push({
251
+ fields: core_1.Utils.asArray(fields).flatMap(f => {
252
+ const key = f.toString();
253
+ /* istanbul ignore next */
254
+ return meta.properties[key]?.fieldNames ?? [key];
255
+ }),
256
+ });
250
257
  return this;
251
258
  }
252
259
  ignore() {
@@ -686,6 +693,10 @@ class QueryBuilder {
686
693
  }
687
694
  return;
688
695
  }
696
+ if (prop && prop.fieldNames.length > 1) {
697
+ ret.push(...prop.fieldNames.map(f => this.helper.mapper(f, this.type)));
698
+ return;
699
+ }
689
700
  ret.push(this.helper.mapper(field, this.type));
690
701
  });
691
702
  const meta = this.mainAlias.metadata;
@@ -16,7 +16,7 @@ export declare class QueryBuilderHelper {
16
16
  private readonly driver;
17
17
  private readonly platform;
18
18
  private readonly metadata;
19
- constructor(entityName: string, alias: string, aliasMap: Dictionary<Alias>, subQueries: Dictionary<string>, knex: Knex, driver: AbstractSqlDriver);
19
+ constructor(entityName: string, alias: string, aliasMap: Dictionary<Alias<any>>, subQueries: Dictionary<string>, knex: Knex, driver: AbstractSqlDriver);
20
20
  mapper(field: string | Knex.Raw, type?: QueryType): string;
21
21
  mapper(field: string | Knex.Raw, type?: QueryType, value?: any, alias?: string | null): string;
22
22
  processData(data: Dictionary, convertCustomTypes: boolean, multi?: boolean): any;
@@ -61,9 +61,9 @@ export declare class QueryBuilderHelper {
61
61
  isTableNameAliasRequired(type?: QueryType): boolean;
62
62
  private mapData;
63
63
  }
64
- export interface Alias {
64
+ export interface Alias<T> {
65
65
  aliasName: string;
66
66
  entityName: string;
67
- metadata?: EntityMetadata;
67
+ metadata?: EntityMetadata<T>;
68
68
  subQuery?: Knex.QueryBuilder;
69
69
  }
@@ -128,12 +128,13 @@ class QueryBuilderHelper {
128
128
  const pivotMeta = this.metadata.find(prop.pivotEntity);
129
129
  const ret = {
130
130
  [`${ownerAlias}.${prop.name}#${pivotAlias}`]: {
131
- prop, type, cond, ownerAlias,
131
+ prop, type, ownerAlias,
132
132
  alias: pivotAlias,
133
133
  inverseAlias: alias,
134
134
  joinColumns: prop.joinColumns,
135
135
  inverseJoinColumns: prop.inverseJoinColumns,
136
136
  primaryKeys: prop.referencedColumnNames,
137
+ cond: {},
137
138
  table: pivotMeta.tableName,
138
139
  schema: this.driver.getSchemaName(pivotMeta),
139
140
  path: path.endsWith('[pivot]') ? path : `${path}[pivot]`,
@@ -143,7 +144,7 @@ class QueryBuilderHelper {
143
144
  return ret;
144
145
  }
145
146
  const prop2 = prop.owner ? pivotMeta.relations[1] : pivotMeta.relations[0];
146
- ret[`${pivotAlias}.${prop2.name}#${alias}`] = this.joinManyToOneReference(prop2, pivotAlias, alias, type);
147
+ ret[`${pivotAlias}.${prop2.name}#${alias}`] = this.joinManyToOneReference(prop2, pivotAlias, alias, type, cond);
147
148
  ret[`${pivotAlias}.${prop2.name}#${alias}`].path = path;
148
149
  return ret;
149
150
  }
@@ -17,6 +17,7 @@ export declare class DatabaseTable {
17
17
  constructor(platform: AbstractSqlPlatform, name: string, schema?: string | undefined);
18
18
  getColumns(): Column[];
19
19
  getColumn(name: string): Column | undefined;
20
+ removeColumn(name: string): void;
20
21
  getIndexes(): Index[];
21
22
  getChecks(): Check[];
22
23
  init(cols: Column[], indexes: Index[] | undefined, checks: Check<unknown>[] | undefined, pks: string[], fks?: Dictionary<ForeignKey>, enums?: Dictionary<string[]>): void;
@@ -24,6 +24,9 @@ class DatabaseTable {
24
24
  getColumn(name) {
25
25
  return this.columns[name];
26
26
  }
27
+ removeColumn(name) {
28
+ delete this.columns[name];
29
+ }
27
30
  getIndexes() {
28
31
  return this.indexes;
29
32
  }
@@ -227,6 +230,8 @@ class DatabaseTable {
227
230
  primary: column.primary,
228
231
  fieldName: column.name,
229
232
  length: column.length,
233
+ precision: column.precision,
234
+ scale: column.scale,
230
235
  index: index ? index.keyName : undefined,
231
236
  unique: unique ? unique.keyName : undefined,
232
237
  enum: !!column.enumItems?.length,