@mikro-orm/sql 7.2.2-dev.2 → 7.2.2-dev.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.
@@ -17,7 +17,7 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
17
17
  /** Evaluates a formula callback, handling both string and Raw return values. */
18
18
  evaluateFormula(formula: (...args: any[]) => string | Raw, columns: any, table: FormulaTable): string;
19
19
  /** For TPT entities, returns ownProps (columns in this table); otherwise returns all props. */
20
- private getTableProps;
20
+ protected getTableProps<T extends object>(meta: EntityMetadata<T>): EntityProperty<T>[];
21
21
  /** Creates a FormulaTable object for use in formula callbacks. */
22
22
  private createFormulaTable;
23
23
  private validateSqlOptions;
@@ -6,6 +6,7 @@ export declare class MsSqlNativeQueryBuilder extends NativeQueryBuilder {
6
6
  params: unknown[];
7
7
  };
8
8
  protected compileInsert(): void;
9
+ protected addOutputClause(type: 'inserted' | 'deleted'): void;
9
10
  private appendOutputTable;
10
11
  private compileUpsert;
11
12
  protected compileSelect(): void;
@@ -22,6 +22,9 @@ export class MsSqlNativeQueryBuilder extends NativeQueryBuilder {
22
22
  this.compileCtes();
23
23
  if (this.options.onConflict && !Utils.isEmpty(Utils.asArray(this.options.data)[0])) {
24
24
  this.compileUpsert();
25
+ if (suffix) {
26
+ this.parts.push(suffix);
27
+ }
25
28
  }
26
29
  else {
27
30
  switch (this.type) {
@@ -72,31 +75,34 @@ export class MsSqlNativeQueryBuilder extends NativeQueryBuilder {
72
75
  return;
73
76
  }
74
77
  const parts = this.processInsertData();
75
- if (this.options.flags?.has(QueryFlag.OUTPUT_TABLE)) {
76
- this.parts[this.parts.length - 2] += ' into #out ';
77
- }
78
78
  this.parts.push(parts.join(', '));
79
79
  }
80
+ addOutputClause(type) {
81
+ super.addOutputClause(type);
82
+ if (this.options.returning?.length && this.options.flags?.has(QueryFlag.OUTPUT_TABLE)) {
83
+ this.parts[this.parts.length - 1] += ' into #out';
84
+ }
85
+ }
80
86
  appendOutputTable() {
81
- if (!this.options.flags?.has(QueryFlag.OUTPUT_TABLE)) {
87
+ if (!this.options.flags?.has(QueryFlag.OUTPUT_TABLE) || !this.options.returning?.length) {
82
88
  return { prefix: '', suffix: '' };
83
89
  }
84
90
  const returningFields = this.options.returning;
85
91
  const selections = returningFields.map(field => `[t].${this.platform.quoteIdentifier(field)}`).join(',');
86
92
  return {
87
93
  prefix: `select top(0) ${selections} into #out from ${this.getTableName()} as t left join ${this.getTableName()} on 0 = 1;`,
88
- suffix: `select ${selections} from #out as t; drop table #out`,
94
+ suffix: `select ${selections} from #out as t; select @@rowcount as [__mikro_orm_row_count__]; drop table #out`,
89
95
  };
90
96
  }
91
97
  compileUpsert() {
92
98
  const clause = this.options.onConflict;
93
99
  const dataAsArray = Utils.asArray(this.options.data);
94
- const keys = Object.keys(dataAsArray[0]);
100
+ const keys = [...new Set(dataAsArray.flatMap(data => Object.keys(data)))];
95
101
  const values = keys.map(() => '?');
96
102
  const parts = [];
97
103
  for (const data of dataAsArray) {
98
104
  for (const key of keys) {
99
- this.params.push(data[key]);
105
+ this.params.push(data[key] ?? null);
100
106
  }
101
107
  parts.push(`(${values.join(', ')})`);
102
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.2.2-dev.2",
3
+ "version": "7.2.2-dev.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
  "keywords": [
6
6
  "data-mapper",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.2.1"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.2-dev.2"
56
+ "@mikro-orm/core": "7.2.2-dev.4"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"