@mikro-orm/mssql 7.1.0-dev.0 → 7.1.0-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.
@@ -2,17 +2,17 @@ import { type AnyEntity, type EntityClass, type EntitySchema, MikroORM, type Opt
2
2
  import type { SqlEntityManager } from '@mikro-orm/sql';
3
3
  import { MsSqlDriver } from './MsSqlDriver.js';
4
4
  /** Configuration options for the MSSQL driver. */
5
- export type MsSqlOptions<EM extends SqlEntityManager<MsSqlDriver> = SqlEntityManager<MsSqlDriver>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> = Partial<Options<MsSqlDriver, EM, Entities>>;
5
+ export type MsSqlOptions<EM extends SqlEntityManager<MsSqlDriver> = SqlEntityManager<MsSqlDriver>, Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> = Partial<Options<MsSqlDriver, EM, Entities>>;
6
6
  /** Creates a type-safe configuration object for the MSSQL driver. */
7
- export declare function defineMsSqlConfig<EM extends SqlEntityManager<MsSqlDriver> = SqlEntityManager<MsSqlDriver>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: Partial<Options<MsSqlDriver, EM, Entities>>): Partial<Options<MsSqlDriver, EM, Entities>>;
7
+ export declare function defineMsSqlConfig<EM extends SqlEntityManager<MsSqlDriver> = SqlEntityManager<MsSqlDriver>, Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: Partial<Options<MsSqlDriver, EM, Entities>>): Partial<Options<MsSqlDriver, EM, Entities>>;
8
8
  /**
9
9
  * @inheritDoc
10
10
  */
11
- 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> {
11
+ export declare class MsSqlMikroORM<EM extends SqlEntityManager<MsSqlDriver> = SqlEntityManager<MsSqlDriver>, Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> extends MikroORM<MsSqlDriver, EM, Entities> {
12
12
  /**
13
13
  * @inheritDoc
14
14
  */
15
- 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: Partial<Options<D, EM, Entities>>): Promise<MikroORM<D, EM, Entities>>;
15
+ static init<D extends IDatabaseDriver = MsSqlDriver, EM extends EntityManager<D> = D[typeof EntityManagerType] & EntityManager<D>, Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: Partial<Options<D, EM, Entities>>): Promise<MikroORM<D, EM, Entities>>;
16
16
  /**
17
17
  * @inheritDoc
18
18
  */
@@ -8,6 +8,7 @@ export declare class MsSqlPlatform extends AbstractSqlPlatform {
8
8
  #private;
9
9
  protected readonly schemaHelper: MsSqlSchemaHelper;
10
10
  protected readonly exceptionConverter: MsSqlExceptionConverter;
11
+ formatIndexHint(indexNames: string[]): string;
11
12
  /** @inheritDoc */
12
13
  lookupExtensions(orm: MikroORM<MsSqlDriver>): void;
13
14
  /** @inheritDoc */
package/MsSqlPlatform.js CHANGED
@@ -16,6 +16,9 @@ export class MsSqlPlatform extends AbstractSqlPlatform {
16
16
  bigint: 'bigint',
17
17
  boolean: 'bit',
18
18
  };
19
+ formatIndexHint(indexNames) {
20
+ return `with (index(${indexNames.join(', ')}))`;
21
+ }
19
22
  /** @inheritDoc */
20
23
  lookupExtensions(orm) {
21
24
  MsSqlSchemaGenerator.register(orm);
@@ -1,4 +1,4 @@
1
- import { type AbstractSqlConnection, type CheckDef, type Column, type DatabaseSchema, type DatabaseTable, type Dictionary, type ForeignKey, type IndexDef, SchemaHelper, type Table, type TableDifference, type Transaction, type Type } from '@mikro-orm/sql';
1
+ import { type AbstractSqlConnection, type CheckDef, type Column, type DatabaseSchema, type DatabaseTable, type Dictionary, type ForeignKey, type IndexDef, SchemaHelper, type Table, type TableDifference, type SqlTriggerDef, type Transaction, type Type } from '@mikro-orm/sql';
2
2
  /** Schema introspection helper for Microsoft SQL Server. */
3
3
  export declare class MsSqlSchemaHelper extends SchemaHelper {
4
4
  static readonly DEFAULT_VALUES: {
@@ -23,6 +23,12 @@ export declare class MsSqlSchemaHelper extends SchemaHelper {
23
23
  private getEnumDefinitions;
24
24
  private getChecksSQL;
25
25
  getAllChecks(connection: AbstractSqlConnection, tablesBySchemas: Map<string | undefined, Table[]>, ctx?: Transaction): Promise<Dictionary<CheckDef[]>>;
26
+ /** Generates SQL to create an MSSQL trigger. MSSQL supports AFTER and INSTEAD OF only. */
27
+ createTrigger(table: DatabaseTable, trigger: SqlTriggerDef): string;
28
+ /** Generates SQL to drop an MSSQL trigger. */
29
+ dropTrigger(table: DatabaseTable, trigger: SqlTriggerDef): string;
30
+ private getSchemaQualifiedName;
31
+ getAllTriggers(connection: AbstractSqlConnection, tablesBySchemas: Map<string | undefined, Table[]>): Promise<Dictionary<SqlTriggerDef[]>>;
26
32
  loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[], ctx?: Transaction): Promise<void>;
27
33
  getPreAlterTable(tableDiff: TableDifference, safe: boolean): string[];
28
34
  getPostAlterTable(tableDiff: TableDifference, safe: boolean): string[];
@@ -285,6 +285,82 @@ export class MsSqlSchemaHelper extends SchemaHelper {
285
285
  }
286
286
  return ret;
287
287
  }
288
+ /** Generates SQL to create an MSSQL trigger. MSSQL supports AFTER and INSTEAD OF only. */
289
+ createTrigger(table, trigger) {
290
+ if (trigger.expression) {
291
+ return trigger.expression;
292
+ }
293
+ /* v8 ignore next 3 */
294
+ if (trigger.timing === 'before') {
295
+ throw new Error(`MSSQL does not support BEFORE triggers. Use AFTER or INSTEAD OF for trigger "${trigger.name}".`);
296
+ }
297
+ const timing = trigger.timing.toUpperCase();
298
+ const events = trigger.events.map(e => e.toUpperCase()).join(', ');
299
+ const qualifiedName = this.getSchemaQualifiedName(table, trigger.name);
300
+ return `create trigger ${qualifiedName} on ${table.getQuotedName()} ${timing} ${events} as begin ${trigger.body}; end`;
301
+ }
302
+ /** Generates SQL to drop an MSSQL trigger. */
303
+ dropTrigger(table, trigger) {
304
+ return `drop trigger if exists ${this.getSchemaQualifiedName(table, trigger.name)}`;
305
+ }
306
+ getSchemaQualifiedName(table, name) {
307
+ const defaultSchema = this.platform.getDefaultSchemaName();
308
+ if (table.schema && table.schema !== defaultSchema) {
309
+ return `${this.quote(table.schema)}.${this.quote(name)}`;
310
+ }
311
+ return this.quote(name);
312
+ }
313
+ async getAllTriggers(connection, tablesBySchemas) {
314
+ const conditions = [];
315
+ for (const [schema, tables] of tablesBySchemas) {
316
+ const names = tables.map(t => this.platform.quoteValue(t.table_name)).join(', ');
317
+ const schemaName = this.platform.quoteValue(schema ?? this.platform.getDefaultSchemaName());
318
+ conditions.push(`(schema_name(p.schema_id) = ${schemaName} and p.name in (${names}))`);
319
+ }
320
+ const sql = `select t.name as trigger_name, schema_name(p.schema_id) as schema_name,
321
+ p.name as table_name, te.type_desc as event,
322
+ case when t.is_instead_of_trigger = 1 then 'INSTEAD OF' else 'AFTER' end as timing,
323
+ object_definition(t.object_id) as definition
324
+ from sys.triggers t
325
+ join sys.trigger_events te on t.object_id = te.object_id
326
+ join sys.objects p on t.parent_id = p.object_id
327
+ where (${conditions.join(' or ')})
328
+ order by t.name, te.type_desc`;
329
+ const allTriggers = await connection.execute(sql);
330
+ const ret = {};
331
+ const triggerMap = new Map();
332
+ for (const row of allTriggers) {
333
+ const key = this.getTableKey(row);
334
+ const dedupeKey = `${key}:${row.trigger_name}`;
335
+ const event = row.event.toLowerCase();
336
+ if (triggerMap.has(dedupeKey)) {
337
+ const existing = triggerMap.get(dedupeKey);
338
+ if (!existing.events.includes(event)) {
339
+ existing.events.push(event);
340
+ }
341
+ continue;
342
+ }
343
+ // Parse body from full trigger definition
344
+ let body = '';
345
+ if (row.definition) {
346
+ const bodyMatch = /\bas\s+begin\s+([\s\S]*)\s*end\s*;?\s*$/i.exec(row.definition);
347
+ if (bodyMatch) {
348
+ body = bodyMatch[1].trim().replace(/;\s*$/, '');
349
+ }
350
+ }
351
+ ret[key] ??= [];
352
+ const trigger = {
353
+ name: row.trigger_name,
354
+ timing: row.timing.toLowerCase(),
355
+ events: [event],
356
+ forEach: 'row', // MSSQL has no FOR EACH ROW/STATEMENT syntax; match the metadata default to avoid false diffs
357
+ body,
358
+ };
359
+ ret[key].push(trigger);
360
+ triggerMap.set(dedupeKey, trigger);
361
+ }
362
+ return ret;
363
+ }
288
364
  async loadInformationSchema(schema, connection, tables, schemas, ctx) {
289
365
  if (tables.length === 0) {
290
366
  return;
@@ -294,12 +370,16 @@ export class MsSqlSchemaHelper extends SchemaHelper {
294
370
  const indexes = await this.getAllIndexes(connection, tablesBySchema, ctx);
295
371
  const checks = await this.getAllChecks(connection, tablesBySchema, ctx);
296
372
  const fks = await this.getAllForeignKeys(connection, tablesBySchema, ctx);
373
+ const triggers = await this.getAllTriggers(connection, tablesBySchema);
297
374
  for (const t of tables) {
298
375
  const key = this.getTableKey(t);
299
376
  const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
300
377
  const pks = await this.getPrimaryKeys(connection, indexes[key], table.name, table.schema);
301
378
  const enums = this.getEnumDefinitions(checks[key] ?? []);
302
379
  table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums);
380
+ if (triggers[key]) {
381
+ table.setTriggers(triggers[key]);
382
+ }
303
383
  }
304
384
  }
305
385
  getPreAlterTable(tableDiff, safe) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mssql",
3
- "version": "7.1.0-dev.0",
3
+ "version": "7.1.0-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
  "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.0-dev.0",
50
+ "@mikro-orm/sql": "7.1.0-dev.10",
51
51
  "kysely": "0.28.16",
52
52
  "tarn": "3.0.2",
53
53
  "tedious": "19.2.1",
@@ -57,7 +57,7 @@
57
57
  "@mikro-orm/core": "^7.0.11"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.1.0-dev.0"
60
+ "@mikro-orm/core": "7.1.0-dev.10"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">= 22.17.0"