@mikro-orm/mssql 7.0.0-dev.7 → 7.0.0-dev.71

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,19 +1,18 @@
1
- import { MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from '@mikro-orm/core';
2
- import { MsSqlDriver } from './MsSqlDriver.js';
1
+ import { type AnyEntity, type EntityClass, type EntitySchema, MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from '@mikro-orm/core';
3
2
  import type { SqlEntityManager } from '@mikro-orm/knex';
3
+ import { MsSqlDriver } from './MsSqlDriver.js';
4
+ export type MsSqlOptions<EM extends SqlEntityManager<MsSqlDriver> = SqlEntityManager<MsSqlDriver>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> = Options<MsSqlDriver, EM, Entities>;
5
+ export declare function defineMsSqlConfig<EM extends SqlEntityManager<MsSqlDriver> = SqlEntityManager<MsSqlDriver>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: Options<MsSqlDriver, EM, Entities>): Options<MsSqlDriver, EM, Entities>;
4
6
  /**
5
7
  * @inheritDoc
6
8
  */
7
- export declare class MsSqlMikroORM<EM extends EntityManager = SqlEntityManager> extends MikroORM<MsSqlDriver, EM> {
8
- private static DRIVER;
9
+ export declare class MsSqlMikroORM<EM extends SqlEntityManager<MsSqlDriver> = SqlEntityManager<MsSqlDriver>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> extends MikroORM<MsSqlDriver, EM, Entities> {
9
10
  /**
10
11
  * @inheritDoc
11
12
  */
12
- static init<D extends IDatabaseDriver = MsSqlDriver, EM extends EntityManager = D[typeof EntityManagerType] & EntityManager>(options?: Options<D, EM>): Promise<MikroORM<D, EM>>;
13
+ static init<D extends IDatabaseDriver = MsSqlDriver, EM extends EntityManager<D> = D[typeof EntityManagerType] & EntityManager<D>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: Options<D, EM, Entities>): Promise<MikroORM<D, EM, Entities>>;
13
14
  /**
14
15
  * @inheritDoc
15
16
  */
16
- static initSync<D extends IDatabaseDriver = MsSqlDriver, EM extends EntityManager = D[typeof EntityManagerType] & EntityManager>(options: Options<D, EM>): MikroORM<D, EM>;
17
+ constructor(options: Options<MsSqlDriver, EM, Entities>);
17
18
  }
18
- export type MsSqlOptions = Options<MsSqlDriver>;
19
- export declare function defineMsSqlConfig(options: MsSqlOptions): Options<MsSqlDriver, SqlEntityManager<MsSqlDriver> & EntityManager<IDatabaseDriver<import("@mikro-orm/core").Connection>>>;
package/MsSqlMikroORM.js CHANGED
@@ -1,24 +1,22 @@
1
1
  import { defineConfig, MikroORM, } from '@mikro-orm/core';
2
2
  import { MsSqlDriver } from './MsSqlDriver.js';
3
+ export function defineMsSqlConfig(options) {
4
+ return defineConfig({ driver: MsSqlDriver, ...options });
5
+ }
3
6
  /**
4
7
  * @inheritDoc
5
8
  */
6
9
  export class MsSqlMikroORM extends MikroORM {
7
- static DRIVER = MsSqlDriver;
8
10
  /**
9
11
  * @inheritDoc
10
12
  */
11
13
  static async init(options) {
12
- return super.init(options);
14
+ return super.init(defineMsSqlConfig(options));
13
15
  }
14
16
  /**
15
17
  * @inheritDoc
16
18
  */
17
- static initSync(options) {
18
- return super.initSync(options);
19
+ constructor(options) {
20
+ super(defineMsSqlConfig(options));
19
21
  }
20
22
  }
21
- /* v8 ignore next 3 */
22
- export function defineMsSqlConfig(options) {
23
- return defineConfig({ driver: MsSqlDriver, ...options });
24
- }
@@ -1,12 +1,13 @@
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 EntityManager, type EntityMetadata, type IDatabaseDriver, type IPrimaryKey, type MikroORM, MsSqlNativeQueryBuilder, type Primary, QueryOrder, Type } 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';
5
+ import type { MsSqlDriver } from './MsSqlDriver.js';
5
6
  export declare class MsSqlPlatform extends AbstractSqlPlatform {
6
7
  protected readonly schemaHelper: MsSqlSchemaHelper;
7
8
  protected readonly exceptionConverter: MsSqlExceptionConverter;
8
9
  /** @inheritDoc */
9
- lookupExtensions(orm: MikroORM): void;
10
+ lookupExtensions(orm: MikroORM<MsSqlDriver>): void;
10
11
  /** @inheritDoc */
11
12
  init(orm: MikroORM): void;
12
13
  getRollbackToSavepointSQL(savepointName: string): string;
@@ -55,9 +56,12 @@ export declare class MsSqlPlatform extends AbstractSqlPlatform {
55
56
  usesEnumCheckConstraints(): boolean;
56
57
  supportsMultipleCascadePaths(): boolean;
57
58
  supportsMultipleStatements(): boolean;
58
- quoteIdentifier(id: string): string;
59
+ quoteIdentifier(id: string | {
60
+ toString: () => string;
61
+ }): string;
59
62
  escape(value: any): string;
60
63
  getSchemaGenerator(driver: IDatabaseDriver, em?: EntityManager): MsSqlSchemaGenerator;
61
64
  allowsComparingTuples(): boolean;
65
+ getOrderByExpression(column: string, direction: QueryOrder): string[];
62
66
  getDefaultClientUrl(): string;
63
67
  }
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, ALIAS_REPLACEMENT, DoubleType, FloatType, MsSqlNativeQueryBuilder, QueryOrder, raw, RawQueryFragment, Type, } 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';
@@ -83,7 +83,7 @@ export class MsSqlPlatform extends AbstractSqlPlatform {
83
83
  return super.getVarcharTypeDeclarationSQL(column);
84
84
  }
85
85
  getEnumTypeDeclarationSQL(column) {
86
- if (column.items?.every(item => Utils.isString(item))) {
86
+ if (column.items?.every(item => typeof item === 'string')) {
87
87
  return Type.getType(UnicodeStringType).getColumnType({ length: 100, ...column }, this);
88
88
  }
89
89
  /* v8 ignore next */
@@ -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) {
@@ -1,6 +1,7 @@
1
1
  import { type ClearDatabaseOptions, type DropSchemaOptions, type MikroORM, SchemaGenerator } from '@mikro-orm/knex';
2
+ import type { MsSqlDriver } from './MsSqlDriver.js';
2
3
  export declare class MsSqlSchemaGenerator extends SchemaGenerator {
3
- static register(orm: MikroORM): void;
4
- clearDatabase(options?: ClearDatabaseOptions): Promise<void>;
4
+ static register(orm: MikroORM<MsSqlDriver>): void;
5
+ clear(options?: ClearDatabaseOptions): Promise<void>;
5
6
  getDropSchemaSQL(options?: Omit<DropSchemaOptions, 'dropDb'>): Promise<string>;
6
7
  }
@@ -3,11 +3,11 @@ export class MsSqlSchemaGenerator extends SchemaGenerator {
3
3
  static register(orm) {
4
4
  orm.config.registerExtension('@mikro-orm/schema-generator', () => new MsSqlSchemaGenerator(orm.em));
5
5
  }
6
- async clearDatabase(options) {
6
+ async clear(options) {
7
7
  // truncate by default, so no value is considered as true
8
8
  /* v8 ignore next 3 */
9
9
  if (options?.truncate === false) {
10
- return super.clearDatabase(options);
10
+ return super.clear(options);
11
11
  }
12
12
  // https://stackoverflow.com/questions/253849/cannot-truncate-table-because-it-is-being-referenced-by-a-foreign-key-constraint
13
13
  for (const meta of this.getOrderedMetadata(options?.schema).reverse()) {
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)
@@ -382,6 +381,8 @@ See also the list of contributors who [participated](https://github.com/mikro-or
382
381
 
383
382
  Please ⭐️ this repository if this project helped you!
384
383
 
384
+ > If you'd like to support my open-source work, consider sponsoring me directly at [github.com/sponsors/b4nan](https://github.com/sponsors/b4nan).
385
+
385
386
  ## 📝 License
386
387
 
387
388
  Copyright © 2018 [Martin Adámek](https://github.com/b4nan).
package/index.d.ts CHANGED
@@ -5,4 +5,4 @@ export * from './MsSqlPlatform.js';
5
5
  export * from './MsSqlSchemaHelper.js';
6
6
  export * from './MsSqlExceptionConverter.js';
7
7
  export * from './UnicodeStringType.js';
8
- export { MsSqlMikroORM as MikroORM, MsSqlOptions as Options, defineMsSqlConfig as defineConfig, } from './MsSqlMikroORM.js';
8
+ export { MsSqlMikroORM as MikroORM, type MsSqlOptions as Options, defineMsSqlConfig as defineConfig, } from './MsSqlMikroORM.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.7",
4
+ "version": "7.0.0-dev.71",
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",
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "homepage": "https://mikro-orm.io",
40
40
  "engines": {
41
- "node": ">= 22.11.0"
41
+ "node": ">= 22.17.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "yarn clean && yarn compile && yarn copy",
@@ -50,17 +50,17 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "@mikro-orm/knex": "7.0.0-dev.7",
53
+ "@mikro-orm/knex": "7.0.0-dev.71",
54
54
  "tarn": "3.0.2",
55
- "tedious": "19.0.0",
55
+ "tedious": "19.1.0",
56
56
  "tsqlstring": "1.0.1"
57
57
  },
58
58
  "devDependencies": {
59
- "@mikro-orm/core": "^6.4.9",
60
- "kysely": "https://pkg.pr.new/kysely-org/kysely/kysely@2b7007e"
59
+ "@mikro-orm/core": "^6.6.1",
60
+ "kysely": "0.28.8"
61
61
  },
62
62
  "peerDependencies": {
63
- "@mikro-orm/core": "7.0.0-dev.7",
63
+ "@mikro-orm/core": "7.0.0-dev.71",
64
64
  "kysely": "*"
65
65
  }
66
66
  }