@mikro-orm/mssql 7.1.6 → 7.1.7-dev.0

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.
@@ -2,8 +2,8 @@ import { ExceptionConverter, type Dictionary, type DriverException } from '@mikr
2
2
  /** Converts MSSQL native errors into typed MikroORM driver exceptions. */
3
3
  export declare class MsSqlExceptionConverter extends ExceptionConverter {
4
4
  /**
5
- * @see https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/mssqlserver-511-database-engine-error?view=sql-server-ver15
6
- * @see https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractPostgreSQLDriver.php
5
+ * @see https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors
6
+ * @see https://github.com/doctrine/dbal/blob/4.4.x/src/Driver/API/SQLSrv/ExceptionConverter.php
7
7
  */
8
8
  convertException(exception: Error & Dictionary): DriverException;
9
9
  }
@@ -1,11 +1,17 @@
1
- import { ExceptionConverter, InvalidFieldNameException, NonUniqueFieldNameException, NotNullConstraintViolationException, SyntaxErrorException, TableExistsException, TableNotFoundException, UniqueConstraintViolationException, } from '@mikro-orm/core';
1
+ import { CheckConstraintViolationException, DeadlockException, ExceptionConverter, ForeignKeyConstraintViolationException, InvalidFieldNameException, LockWaitTimeoutException, NonUniqueFieldNameException, NotNullConstraintViolationException, SyntaxErrorException, TableExistsException, TableNotFoundException, UniqueConstraintViolationException, } from '@mikro-orm/core';
2
2
  /** Converts MSSQL native errors into typed MikroORM driver exceptions. */
3
3
  export class MsSqlExceptionConverter extends ExceptionConverter {
4
4
  /**
5
- * @see https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/mssqlserver-511-database-engine-error?view=sql-server-ver15
6
- * @see https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractPostgreSQLDriver.php
5
+ * @see https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors
6
+ * @see https://github.com/doctrine/dbal/blob/4.4.x/src/Driver/API/SQLSrv/ExceptionConverter.php
7
7
  */
8
8
  convertException(exception) {
9
+ // the kysely mssql driver rejects with a plain array when a request produces multiple errors
10
+ if (Array.isArray(exception) && exception.length > 0) {
11
+ const [first, ...rest] = exception;
12
+ first.message += rest.map(e => '\n' + e.message).join('');
13
+ exception = first;
14
+ }
9
15
  let errno = exception.number;
10
16
  /* v8 ignore next */
11
17
  if ('errors' in exception &&
@@ -27,7 +33,25 @@ export class MsSqlExceptionConverter extends ExceptionConverter {
27
33
  return new TableNotFoundException(exception);
28
34
  case 209:
29
35
  return new NonUniqueFieldNameException(exception);
36
+ case 547:
37
+ // 547 covers both referential (FOREIGN KEY/REFERENCE) and CHECK constraint
38
+ // violations; only the message distinguishes them. The surrounding text is
39
+ // localized (e.g. "CHECK-Einschränkung" on a German server), but the
40
+ // constraint-type keyword itself is an untranslated SQL keyword, so match on
41
+ // the bare `CHECK` token.
42
+ if (exception.message.includes('CHECK')) {
43
+ return new CheckConstraintViolationException(exception);
44
+ }
45
+ return new ForeignKeyConstraintViolationException(exception);
46
+ case 4712:
47
+ // TRUNCATE blocked because the table is referenced by a FOREIGN KEY.
48
+ return new ForeignKeyConstraintViolationException(exception);
49
+ case 1205:
50
+ return new DeadlockException(exception);
51
+ case 1222:
52
+ return new LockWaitTimeoutException(exception);
30
53
  case 2601:
54
+ case 2627:
31
55
  return new UniqueConstraintViolationException(exception);
32
56
  case 2714:
33
57
  return new TableExistsException(exception);
@@ -3,6 +3,9 @@ import type { MsSqlDriver } from './MsSqlDriver.js';
3
3
  /** Schema generator with MSSQL-specific behavior for clearing and dropping schemas. */
4
4
  export declare class MsSqlSchemaGenerator extends SchemaGenerator {
5
5
  static register(orm: MikroORM<MsSqlDriver>): void;
6
+ createDatabase(name?: string, options?: {
7
+ skipOnConnect?: boolean;
8
+ }): Promise<void>;
6
9
  clear(options?: ClearDatabaseOptions): Promise<void>;
7
10
  getDropSchemaSQL(options?: Omit<DropSchemaOptions, 'dropDb'>): Promise<string>;
8
11
  }
@@ -4,6 +4,21 @@ export class MsSqlSchemaGenerator extends SchemaGenerator {
4
4
  static register(orm) {
5
5
  orm.config.registerExtension('@mikro-orm/schema-generator', () => new MsSqlSchemaGenerator(orm.em));
6
6
  }
7
+ async createDatabase(name, options) {
8
+ // `create database` clones the `model` database under an exclusive lock, so concurrent
9
+ // calls (e.g. parallel test workers) can fail with error 1807 — retry a few times
10
+ for (let attempt = 1;; attempt++) {
11
+ try {
12
+ return await super.createDatabase(name, options);
13
+ }
14
+ catch (e) {
15
+ if (attempt >= 5 || e?.number !== 1807) {
16
+ throw e;
17
+ }
18
+ await new Promise(resolve => setTimeout(resolve, attempt * 200));
19
+ }
20
+ }
21
+ }
7
22
  async clear(options) {
8
23
  // truncate by default, so no value is considered as true
9
24
  /* v8 ignore next */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mssql",
3
- "version": "7.1.6",
3
+ "version": "7.1.7-dev.0",
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",
@@ -47,7 +47,7 @@
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
49
  "dependencies": {
50
- "@mikro-orm/sql": "7.1.6",
50
+ "@mikro-orm/sql": "7.1.7-dev.0",
51
51
  "kysely": "0.29.3",
52
52
  "tarn": "3.1.2",
53
53
  "tedious": "20.0.0",
@@ -57,7 +57,7 @@
57
57
  "@mikro-orm/core": "^7.1.6"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.1.6"
60
+ "@mikro-orm/core": "7.1.7-dev.0"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">= 22.17.0"