@mikro-orm/mssql 6.4.16-dev.1 → 6.4.16-dev.10

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.
package/MsSqlDriver.d.ts CHANGED
@@ -6,4 +6,5 @@ export declare class MsSqlDriver extends AbstractSqlDriver<MsSqlConnection> {
6
6
  constructor(config: Configuration);
7
7
  nativeInsertMany<T extends AnyEntity<T>>(entityName: string, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
8
8
  createQueryBuilder<T extends AnyEntity<T>>(entityName: string, ctx?: Transaction<Knex.Transaction>, preferredConnectionType?: ConnectionType, convertCustomTypes?: boolean, loggerContext?: LoggingOptions, alias?: string, em?: SqlEntityManager): MsSqlQueryBuilder<T, any, any, any>;
9
+ private appendOutputTable;
9
10
  }
package/MsSqlDriver.js CHANGED
@@ -46,7 +46,7 @@ class MsSqlDriver extends knex_1.AbstractSqlDriver {
46
46
  return `set identity_insert ${tableName} on; ${sql}; set identity_insert ${tableName} off`;
47
47
  });
48
48
  }
49
- return super.nativeInsertMany(entityName, data, options);
49
+ return super.nativeInsertMany(entityName, data, options, sql => meta.hasTriggers ? this.appendOutputTable(entityName, sql) : sql);
50
50
  }
51
51
  createQueryBuilder(entityName, ctx, preferredConnectionType, convertCustomTypes, loggerContext, alias, em) {
52
52
  // do not compute the connectionType if EM is provided as it will be computed from it in the QB later on
@@ -57,5 +57,22 @@ class MsSqlDriver extends knex_1.AbstractSqlDriver {
57
57
  }
58
58
  return qb;
59
59
  }
60
+ appendOutputTable(entityName, sql) {
61
+ const meta = this.metadata.get(entityName);
62
+ const returningProps = meta.props.filter(prop => prop.primary || prop.defaultRaw);
63
+ const returningFields = core_1.Utils.flatten(returningProps.map(prop => prop.fieldNames));
64
+ const tableName = this.getTableName(meta, {}, true);
65
+ const selections = returningFields
66
+ .map((field) => `[t].${this.platform.quoteIdentifier(field)}`)
67
+ .join(',');
68
+ const position = sql.indexOf(' values ');
69
+ const sqlBeforeValues = sql.substring(0, position);
70
+ const sqlAfterValues = sql.substring(position + 1);
71
+ let outputSql = `select top(0) ${selections} into #out from ${tableName} as t left join ${tableName} on 0 = 1; `;
72
+ outputSql += `${sqlBeforeValues} into #out ${sqlAfterValues}; `;
73
+ outputSql += `select ${selections} from #out as t; `;
74
+ outputSql += `drop table #out`;
75
+ return outputSql;
76
+ }
60
77
  }
61
78
  exports.MsSqlDriver = MsSqlDriver;
@@ -6,4 +6,5 @@ export declare class MsSqlQueryBuilder<Entity extends object = AnyEntity, RootAl
6
6
  getKnexQuery(processVirtualEntity?: boolean): Knex.QueryBuilder;
7
7
  private appendIdentityInsert;
8
8
  private checkIdentityInsert;
9
+ private appendOutputTable;
9
10
  }
@@ -13,6 +13,12 @@ class MsSqlQueryBuilder extends knex_1.QueryBuilder {
13
13
  if (this.flags.has(core_1.QueryFlag.IDENTITY_INSERT)) {
14
14
  this.appendIdentityInsert(qb);
15
15
  }
16
+ if (!this.flags.has(core_1.QueryFlag.IDENTITY_INSERT) && this.type === knex_1.QueryType.INSERT && this.metadata.has(this.mainAlias.entityName)) {
17
+ const meta = this.metadata.get(this.mainAlias.entityName);
18
+ if (meta.hasTriggers) {
19
+ this.appendOutputTable(qb);
20
+ }
21
+ }
16
22
  return qb;
17
23
  }
18
24
  getKnexQuery(processVirtualEntity = true) {
@@ -49,5 +55,30 @@ class MsSqlQueryBuilder extends knex_1.QueryBuilder {
49
55
  this.setFlag(core_1.QueryFlag.IDENTITY_INSERT);
50
56
  }
51
57
  }
58
+ appendOutputTable(qb) {
59
+ const meta = this.metadata.find(this.mainAlias.entityName);
60
+ const returningProps = meta.props.filter(prop => prop.primary || prop.defaultRaw);
61
+ const returningFields = core_1.Utils.flatten(returningProps.map(prop => prop.fieldNames));
62
+ const table = this.driver.getTableName(meta, { schema: this._schema });
63
+ const selections = returningFields
64
+ .map((field) => `[t].${this.platform.quoteIdentifier(field)}`)
65
+ .join(',');
66
+ const originalToSQL = qb.toSQL;
67
+ qb.toSQL = () => {
68
+ const res = originalToSQL.apply(qb);
69
+ const position = res.sql.indexOf(' values ');
70
+ const sqlBeforeValues = res.sql.substring(0, position);
71
+ const sqlAfterValues = res.sql.substring(position + 1);
72
+ let outputSql = `select top(0) ${selections} into #out from ${table} as t left join ${table} on 0 = 1; `;
73
+ outputSql += `${sqlBeforeValues} into #out ${sqlAfterValues}; `;
74
+ outputSql += `select ${selections} from #out as t; `;
75
+ outputSql += `drop table #out`;
76
+ return {
77
+ ...res,
78
+ sql: outputSql,
79
+ toNative: () => res.toNative(),
80
+ };
81
+ };
82
+ }
52
83
  }
53
84
  exports.MsSqlQueryBuilder = MsSqlQueryBuilder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mssql",
3
- "version": "6.4.16-dev.1",
3
+ "version": "6.4.16-dev.10",
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",
@@ -58,7 +58,7 @@
58
58
  "access": "public"
59
59
  },
60
60
  "dependencies": {
61
- "@mikro-orm/knex": "6.4.16-dev.1",
61
+ "@mikro-orm/knex": "6.4.16-dev.10",
62
62
  "tedious": "19.0.0",
63
63
  "tsqlstring": "1.0.1"
64
64
  },
@@ -66,6 +66,6 @@
66
66
  "@mikro-orm/core": "^6.4.15"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.4.16-dev.1"
69
+ "@mikro-orm/core": "6.4.16-dev.10"
70
70
  }
71
71
  }