@mikro-orm/mssql 7.0.0-dev.3 → 7.0.0-dev.30

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, preferredConnectionType?: ConnectionType, convertCustomTypes?: boolean, loggerContext?: LoggingOptions, alias?: string, em?: SqlEntityManager): MsSqlQueryBuilder<T, any, any, any>;
9
+ private appendOutputTable;
9
10
  }
package/MsSqlDriver.js CHANGED
@@ -1,4 +1,4 @@
1
- import { QueryFlag, Utils, } from '@mikro-orm/core';
1
+ import { QueryFlag, Utils, isRaw, } from '@mikro-orm/core';
2
2
  import { AbstractSqlDriver } from '@mikro-orm/knex';
3
3
  import { MsSqlConnection } from './MsSqlConnection.js';
4
4
  import { MsSqlPlatform } from './MsSqlPlatform.js';
@@ -37,7 +37,7 @@ export class MsSqlDriver extends AbstractSqlDriver {
37
37
  return `set identity_insert ${tableName} on; ${sql}; set identity_insert ${tableName} off`;
38
38
  });
39
39
  }
40
- return super.nativeInsertMany(entityName, data, options);
40
+ return super.nativeInsertMany(entityName, data, options, sql => meta.hasTriggers ? this.appendOutputTable(entityName, data, sql) : sql);
41
41
  }
42
42
  createQueryBuilder(entityName, ctx, preferredConnectionType, convertCustomTypes, loggerContext, alias, em) {
43
43
  // do not compute the connectionType if EM is provided as it will be computed from it in the QB later on
@@ -48,4 +48,27 @@ export class MsSqlDriver extends AbstractSqlDriver {
48
48
  }
49
49
  return qb;
50
50
  }
51
+ appendOutputTable(entityName, data, sql) {
52
+ const meta = this.metadata.get(entityName);
53
+ const returningProps = meta.props
54
+ .filter(prop => prop.persist !== false && prop.defaultRaw || prop.autoincrement || prop.generated)
55
+ .filter(prop => !(prop.name in data[0]) || isRaw(data[0][prop.name]));
56
+ const returningFields = Utils.flatten(returningProps.map(prop => prop.fieldNames));
57
+ /* v8 ignore next 3 */
58
+ if (returningFields.length === 0) {
59
+ return sql;
60
+ }
61
+ const tableName = this.getTableName(meta, {}, true);
62
+ const selections = returningFields
63
+ .map((field) => `[t].${this.platform.quoteIdentifier(field)}`)
64
+ .join(',');
65
+ const position = sql.indexOf(' values ');
66
+ const sqlBeforeValues = sql.substring(0, position);
67
+ const sqlAfterValues = sql.substring(position + 1);
68
+ let outputSql = `select top(0) ${selections} into #out from ${tableName} as t left join ${tableName} on 0 = 1; `;
69
+ outputSql += `${sqlBeforeValues} into #out ${sqlAfterValues}; `;
70
+ outputSql += `select ${selections} from #out as t; `;
71
+ outputSql += `drop table #out`;
72
+ return outputSql;
73
+ }
51
74
  }
@@ -1,4 +1,4 @@
1
- import { AbstractSqlPlatform, type EntityMetadata, type IDatabaseDriver, type EntityManager, type MikroORM, Type, type Primary, type IPrimaryKey, MsSqlNativeQueryBuilder } from '@mikro-orm/knex';
1
+ import { AbstractSqlPlatform, type EntityMetadata, type IDatabaseDriver, type EntityManager, type MikroORM, Type, type Primary, type IPrimaryKey, QueryOrder, MsSqlNativeQueryBuilder } from '@mikro-orm/knex';
2
2
  import { MsSqlSchemaHelper } from './MsSqlSchemaHelper.js';
3
3
  import { MsSqlExceptionConverter } from './MsSqlExceptionConverter.js';
4
4
  import { MsSqlSchemaGenerator } from './MsSqlSchemaGenerator.js';
@@ -55,9 +55,12 @@ export declare class MsSqlPlatform extends AbstractSqlPlatform {
55
55
  usesEnumCheckConstraints(): boolean;
56
56
  supportsMultipleCascadePaths(): boolean;
57
57
  supportsMultipleStatements(): boolean;
58
- quoteIdentifier(id: string): string;
58
+ quoteIdentifier(id: string | {
59
+ toString: () => string;
60
+ }): string;
59
61
  escape(value: any): string;
60
62
  getSchemaGenerator(driver: IDatabaseDriver, em?: EntityManager): MsSqlSchemaGenerator;
61
63
  allowsComparingTuples(): boolean;
64
+ getOrderByExpression(column: string, direction: QueryOrder): string[];
62
65
  getDefaultClientUrl(): string;
63
66
  }
package/MsSqlPlatform.js CHANGED
@@ -1,4 +1,4 @@
1
- import { AbstractSqlPlatform, raw, Type, Utils, ALIAS_REPLACEMENT, DoubleType, FloatType, RawQueryFragment, MsSqlNativeQueryBuilder, } from '@mikro-orm/knex';
1
+ import { AbstractSqlPlatform, raw, Type, Utils, ALIAS_REPLACEMENT, DoubleType, FloatType, QueryOrder, RawQueryFragment, MsSqlNativeQueryBuilder, } from '@mikro-orm/knex';
2
2
  // @ts-expect-error no types available
3
3
  import SqlString from 'tsqlstring';
4
4
  import { MsSqlSchemaHelper } from './MsSqlSchemaHelper.js';
@@ -175,7 +175,7 @@ export class MsSqlPlatform extends AbstractSqlPlatform {
175
175
  if (RawQueryFragment.isKnownFragment(id)) {
176
176
  return super.quoteIdentifier(id);
177
177
  }
178
- return `[${id.replace('.', `].[`)}]`;
178
+ return `[${id.toString().replace('.', `].[`)}]`;
179
179
  }
180
180
  escape(value) {
181
181
  if (value instanceof UnicodeString) {
@@ -196,6 +196,20 @@ export class MsSqlPlatform extends AbstractSqlPlatform {
196
196
  allowsComparingTuples() {
197
197
  return false;
198
198
  }
199
+ getOrderByExpression(column, direction) {
200
+ switch (direction.toUpperCase()) {
201
+ case QueryOrder.ASC_NULLS_FIRST:
202
+ return [`case when ${column} is null then 0 else 1 end, ${column} asc`];
203
+ case QueryOrder.ASC_NULLS_LAST:
204
+ return [`case when ${column} is null then 1 else 0 end, ${column} asc`];
205
+ case QueryOrder.DESC_NULLS_FIRST:
206
+ return [`case when ${column} is null then 0 else 1 end, ${column} desc`];
207
+ case QueryOrder.DESC_NULLS_LAST:
208
+ return [`case when ${column} is null then 1 else 0 end, ${column} desc`];
209
+ default:
210
+ return [`${column} ${direction.toLowerCase()}`];
211
+ }
212
+ }
199
213
  getDefaultClientUrl() {
200
214
  return 'mssql://sa@localhost:1433';
201
215
  }
@@ -3,6 +3,12 @@ import { QueryBuilder } from '@mikro-orm/knex';
3
3
  export class MsSqlQueryBuilder extends QueryBuilder {
4
4
  insert(data) {
5
5
  this.checkIdentityInsert(data);
6
+ if (!this.flags.has(QueryFlag.IDENTITY_INSERT) && this.metadata.has(this.mainAlias.entityName)) {
7
+ const meta = this.metadata.find(this.mainAlias.entityName);
8
+ if (meta.hasTriggers) {
9
+ this.setFlag(QueryFlag.OUTPUT_TABLE);
10
+ }
11
+ }
6
12
  return super.insert(data);
7
13
  }
8
14
  checkIdentityInsert(data) {
package/README.md CHANGED
@@ -11,7 +11,6 @@ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-or
11
11
  [![Chat on discord](https://img.shields.io/discord/1214904142443839538?label=discord&color=blue)](https://discord.gg/w8bjxFHS7X)
12
12
  [![Downloads](https://img.shields.io/npm/dm/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
13
13
  [![Coverage Status](https://img.shields.io/coveralls/mikro-orm/mikro-orm.svg)](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
14
- [![Maintainability](https://api.codeclimate.com/v1/badges/27999651d3adc47cfa40/maintainability)](https://codeclimate.com/github/mikro-orm/mikro-orm/maintainability)
15
14
  [![Build Status](https://github.com/mikro-orm/mikro-orm/workflows/tests/badge.svg?branch=master)](https://github.com/mikro-orm/mikro-orm/actions?workflow=tests)
16
15
 
17
16
  ## 🤔 Unit of What?
@@ -141,7 +140,7 @@ There is also auto-generated [CHANGELOG.md](CHANGELOG.md) file based on commit m
141
140
  - [Composite and Foreign Keys as Primary Key](https://mikro-orm.io/docs/composite-keys)
142
141
  - [Filters](https://mikro-orm.io/docs/filters)
143
142
  - [Using `QueryBuilder`](https://mikro-orm.io/docs/query-builder)
144
- - [Preloading Deeply Nested Structures via populate](https://mikro-orm.io/docs/nested-populate)
143
+ - [Populating relations](https://mikro-orm.io/docs/populating-relations)
145
144
  - [Property Validation](https://mikro-orm.io/docs/property-validation)
146
145
  - [Lifecycle Hooks](https://mikro-orm.io/docs/events#hooks)
147
146
  - [Vanilla JS Support](https://mikro-orm.io/docs/usage-with-js)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/mssql",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.3",
4
+ "version": "7.0.0-dev.30",
5
5
  "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.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -50,17 +50,17 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "@mikro-orm/knex": "7.0.0-dev.3",
53
+ "@mikro-orm/knex": "7.0.0-dev.30",
54
54
  "tarn": "3.0.2",
55
55
  "tedious": "19.0.0",
56
56
  "tsqlstring": "1.0.1"
57
57
  },
58
58
  "devDependencies": {
59
- "@mikro-orm/core": "^6.4.5",
60
- "kysely": "https://pkg.pr.new/kysely-org/kysely/kysely@2b7007e"
59
+ "@mikro-orm/core": "^6.5.7",
60
+ "kysely": "0.28.7"
61
61
  },
62
62
  "peerDependencies": {
63
- "@mikro-orm/core": "7.0.0-dev.3",
63
+ "@mikro-orm/core": "7.0.0-dev.30",
64
64
  "kysely": "*"
65
65
  }
66
66
  }