@mikro-orm/sql 7.2.8 → 7.2.9-dev.1

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.
@@ -130,17 +130,18 @@ export class MsSqlNativeQueryBuilder extends NativeQueryBuilder {
130
130
  }
131
131
  this.parts.push('then update set');
132
132
  if (!clause.merge || Array.isArray(clause.merge)) {
133
- const parts = (clause.merge || keys)
133
+ const merge = clause.merge?.length ? clause.merge : keys;
134
+ const parts = merge
134
135
  .filter(field => !Array.isArray(clause.fields) || !clause.fields.includes(field))
135
136
  .map((column) => `${this.quote(column)} = tsource.${this.quote(column)}`);
136
- this.parts.push(parts.join(', '));
137
+ this.parts.push(this.joinVersionMerge(clause, parts, merge));
137
138
  }
138
139
  else if (typeof clause.merge === 'object') {
139
140
  const parts = Object.entries(clause.merge).map(([key, value]) => {
140
141
  this.params.push(value);
141
142
  return `${this.getTableName()}.${this.quote(key)} = ?`;
142
143
  });
143
- this.parts.push(parts.join(', '));
144
+ this.parts.push(this.joinVersionMerge(clause, parts, Object.keys(clause.merge)));
144
145
  }
145
146
  }
146
147
  this.addOutputClause('inserted');
@@ -58,16 +58,17 @@ export class MySqlNativeQueryBuilder extends NativeQueryBuilder {
58
58
  this.params.push(clause.merge[field]);
59
59
  return `${this.quote(field)} = ?`;
60
60
  });
61
- this.parts.push(fields.join(', '));
61
+ this.parts.push(this.joinVersionMerge(clause, fields, Object.keys(clause.merge)));
62
62
  }
63
63
  else if (clause.merge.length === 0) {
64
64
  const dataAsArray = Utils.asArray(this.options.data);
65
65
  const keys = Object.keys(dataAsArray[0]);
66
- this.parts.push(keys.map(key => `${this.quote(key)} = values(${this.quote(key)})`).join(', '));
66
+ const fields = keys.map(key => `${this.quote(key)} = values(${this.quote(key)})`);
67
+ this.parts.push(this.joinVersionMerge(clause, fields, keys));
67
68
  }
68
69
  else {
69
70
  const fields = clause.merge.map(key => `${this.quote(key)} = values(${this.quote(key)})`);
70
- this.parts.push(fields.join(', '));
71
+ this.parts.push(this.joinVersionMerge(clause, fields, clause.merge));
71
72
  }
72
73
  if (clause.where) {
73
74
  this.parts.push(`where ${clause.where.sql}`);
@@ -181,7 +181,8 @@ export class OracleNativeQueryBuilder extends NativeQueryBuilder {
181
181
  if (!clause.ignore) {
182
182
  /* v8 ignore next: merge type branch */
183
183
  if (!clause.merge || Array.isArray(clause.merge)) {
184
- const mergeParts = (clause.merge || keys)
184
+ const merge = clause.merge?.length ? clause.merge : keys;
185
+ const mergeParts = merge
185
186
  .filter(field => !Array.isArray(clause.fields) || !clause.fields.includes(field))
186
187
  .filter((field) => keys.includes(field)) // only reference columns present in the source data
187
188
  .map((column) => `${this.quote(column)} = tsource.${this.quote(column)}`);
@@ -193,7 +194,7 @@ export class OracleNativeQueryBuilder extends NativeQueryBuilder {
193
194
  this.params.push(...clause.where.params);
194
195
  }
195
196
  this.parts.push('then update set');
196
- this.parts.push(mergeParts.join(', '));
197
+ this.parts.push(this.joinVersionMerge(clause, mergeParts, merge));
197
198
  }
198
199
  } /* v8 ignore start: object-form merge branch */
199
200
  else if (typeof clause.merge === 'object') {
@@ -207,7 +208,7 @@ export class OracleNativeQueryBuilder extends NativeQueryBuilder {
207
208
  this.params.push(value);
208
209
  return `${this.getTableName()}.${this.quote(key)} = ?`;
209
210
  });
210
- this.parts.push(parts.join(', '));
211
+ this.parts.push(this.joinVersionMerge(clause, parts, Object.keys(clause.merge)));
211
212
  }
212
213
  /* v8 ignore stop */
213
214
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.2.8",
3
+ "version": "7.2.9-dev.1",
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
  "keywords": [
6
6
  "data-mapper",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.2.8"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.8"
56
+ "@mikro-orm/core": "7.2.9-dev.1"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -66,9 +66,14 @@ interface OnConflictClause {
66
66
  sql: string;
67
67
  params: unknown[];
68
68
  };
69
+ version?: {
70
+ field: string;
71
+ timestamp?: string;
72
+ };
69
73
  }
70
74
  /** @internal */
71
75
  export declare class NativeQueryBuilder implements Subquery {
76
+ #private;
72
77
  protected readonly platform: AbstractSqlPlatform;
73
78
  readonly __subquery: true;
74
79
  protected type?: QueryType;
@@ -103,6 +108,7 @@ export declare class NativeQueryBuilder implements Subquery {
103
108
  };
104
109
  protected addLockClause(): void;
105
110
  protected addOnConflictClause(): void;
111
+ protected joinVersionMerge(clause: OnConflictClause, parts: string[], merged: unknown[]): string;
106
112
  protected combineParts(): {
107
113
  sql: string;
108
114
  params: unknown[];
@@ -7,6 +7,8 @@ export class NativeQueryBuilder {
7
7
  parts = [];
8
8
  params = [];
9
9
  options = {};
10
+ /** the table name or alias usable as a column qualifier (without the alias definition or index hint) */
11
+ #tableQualifier;
10
12
  constructor(platform) {
11
13
  this.platform = platform;
12
14
  }
@@ -33,8 +35,14 @@ export class NativeQueryBuilder {
33
35
  const asKeyword = this.platform.usesAsKeyword() ? ' as ' : ' ';
34
36
  const alias = options?.alias ? `${asKeyword}${this.platform.quoteIdentifier(options.alias)}` : '';
35
37
  const schema = options?.schema && options.schema !== this.platform.getDefaultSchemaName() ? `${options.schema}.` : '';
38
+ this.#tableQualifier = options?.alias
39
+ ? this.platform.quoteIdentifier(options.alias)
40
+ : this.quote(schema + tableName);
36
41
  tableName = this.quote(schema + tableName) + alias;
37
42
  }
43
+ else {
44
+ this.#tableQualifier = undefined;
45
+ }
38
46
  this.options.tableName = tableName;
39
47
  this.options.indexHint = options?.indexHint;
40
48
  return this;
@@ -174,19 +182,19 @@ export class NativeQueryBuilder {
174
182
  this.params.push(clause.merge[field]);
175
183
  return `${this.quote(field)} = ?`;
176
184
  });
177
- this.parts.push(fields.join(', '));
185
+ this.parts.push(this.joinVersionMerge(clause, fields, Object.keys(clause.merge)));
178
186
  }
179
187
  else if (clause.merge) {
180
188
  this.parts.push('do update set');
181
189
  if (clause.merge.length) {
182
190
  const fields = clause.merge.map(field => `${this.quote(field)} = excluded.${this.quote(field)}`);
183
- this.parts.push(fields.join(', '));
191
+ this.parts.push(this.joinVersionMerge(clause, fields, clause.merge));
184
192
  }
185
193
  else {
186
194
  const dataAsArray = Utils.asArray(this.options.data);
187
195
  const keys = Object.keys(dataAsArray[0]);
188
196
  const fields = keys.map(field => `${this.quote(field)} = excluded.${this.quote(field)}`);
189
- this.parts.push(fields.join(', '));
197
+ this.parts.push(this.joinVersionMerge(clause, fields, keys));
190
198
  }
191
199
  }
192
200
  if (clause.where) {
@@ -194,6 +202,17 @@ export class NativeQueryBuilder {
194
202
  this.params.push(...clause.where.params);
195
203
  }
196
204
  }
205
+ joinVersionMerge(clause, parts, merged) {
206
+ if (clause.version && !merged.includes(clause.version.field)) {
207
+ const field = this.quote(clause.version.field);
208
+ // the existing row's version, as the `version + 1` of an update (a bare column is ambiguous with `excluded`)
209
+ parts = [
210
+ ...parts,
211
+ `${field} = ${clause.version.timestamp ?? `${this.#tableQualifier ?? this.getTableName()}.${field} + 1`}`,
212
+ ];
213
+ }
214
+ return parts.join(', ');
215
+ }
197
216
  combineParts() {
198
217
  let sql = this.parts.join(' ');
199
218
  if (this.options.wrap) {
@@ -408,6 +408,7 @@ export class QueryBuilderHelper {
408
408
  return `%${value}%`;
409
409
  }
410
410
  appendOnConflictClause(type, onConflict, qb) {
411
+ const meta = this.#metadata.find(this.#entityName);
411
412
  onConflict.forEach(item => {
412
413
  const { fields, ignore } = item;
413
414
  const sub = qb.onConflict({ fields, ignore });
@@ -424,6 +425,12 @@ export class QueryBuilderHelper {
424
425
  mergeParam = item.merge.map(key => this.mapper(key, type));
425
426
  }
426
427
  sub.merge = mergeParam ?? [];
428
+ // a merged row is updated, so its version is bumped as `updateVersionProperty()` does
429
+ if (meta?.ownsVersionProperty()) {
430
+ const prop = meta.properties[meta.versionProperty];
431
+ const timestamp = prop.runtimeType === 'Date' ? this.#platform.getCurrentTimestampSQL(prop.length) : undefined;
432
+ sub.version = { field: prop.fieldNames[0], timestamp };
433
+ }
427
434
  if (item.where) {
428
435
  sub.where = this._appendQueryCondition(type, item.where);
429
436
  }