@mikro-orm/mssql 7.2.0-dev.3 → 7.2.0-dev.4

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.
@@ -52,6 +52,8 @@ export declare class MsSqlPlatform extends AbstractSqlPlatform {
52
52
  }): string;
53
53
  getDefaultMappedType(type: string): Type<unknown>;
54
54
  getDefaultSchemaName(): string | undefined;
55
+ getDefaultPrimaryName(tableName: string, columns: string[]): string;
56
+ supportsCustomPrimaryKeyNames(): boolean;
55
57
  getUuidTypeDeclarationSQL(column: {
56
58
  length?: number;
57
59
  }): string;
package/MsSqlPlatform.js CHANGED
@@ -139,6 +139,12 @@ export class MsSqlPlatform extends AbstractSqlPlatform {
139
139
  getDefaultSchemaName() {
140
140
  return 'dbo';
141
141
  }
142
+ getDefaultPrimaryName(tableName, columns) {
143
+ return this.getIndexName(tableName, columns, 'primary');
144
+ }
145
+ supportsCustomPrimaryKeyNames() {
146
+ return true;
147
+ }
142
148
  getUuidTypeDeclarationSQL(column) {
143
149
  return 'uniqueidentifier';
144
150
  }
@@ -8,4 +8,5 @@ export declare class MsSqlSchemaGenerator extends SchemaGenerator {
8
8
  }): Promise<void>;
9
9
  clear(options?: ClearDatabaseOptions): Promise<void>;
10
10
  getDropSchemaSQL(options?: Omit<DropSchemaOptions, 'dropDb'>): Promise<string>;
11
+ protected startsBatch(statement: string): boolean;
11
12
  }
@@ -1,4 +1,6 @@
1
1
  import { SchemaGenerator } from '@mikro-orm/sql';
2
+ /** MSSQL rejects these unless they are the first statement in a query batch. */
3
+ const BATCH_FIRST_STATEMENT = /^create\s+(or\s+alter\s+)?(trigger|view|proc(edure)?|function|schema)\b/i;
2
4
  /** Schema generator with MSSQL-specific behavior for clearing and dropping schemas. */
3
5
  export class MsSqlSchemaGenerator extends SchemaGenerator {
4
6
  static register(orm) {
@@ -40,4 +42,7 @@ export class MsSqlSchemaGenerator extends SchemaGenerator {
40
42
  async getDropSchemaSQL(options = {}) {
41
43
  return super.getDropSchemaSQL({ dropForeignKeys: true, ...options });
42
44
  }
45
+ startsBatch(statement) {
46
+ return BATCH_FIRST_STATEMENT.test(statement);
47
+ }
43
48
  }
@@ -51,6 +51,7 @@ export declare class MsSqlSchemaHelper extends SchemaHelper {
51
51
  private getDropDefaultsSQL;
52
52
  getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column, schemaName?: string): string;
53
53
  createTableColumn(column: Column, table: DatabaseTable, changedProperties?: Set<string>): string | undefined;
54
+ protected getPrimaryKeyConstraintPrefix(table: DatabaseTable, index: IndexDef): string;
54
55
  alterTableColumn(column: Column, table: DatabaseTable, changedProperties: Set<string>): string[];
55
56
  getCreateIndexSQL(tableName: string, index: IndexDef, partialExpression?: boolean): string;
56
57
  /**
@@ -67,6 +68,10 @@ export declare class MsSqlSchemaHelper extends SchemaHelper {
67
68
  dropViewIfExists(name: string, schema?: string): string;
68
69
  getAddColumnsSQL(table: DatabaseTable, columns: Column[]): string[];
69
70
  appendComments(table: DatabaseTable): string[];
71
+ alterTableComment(table: DatabaseTable, comment?: string): string;
72
+ getChangeColumnCommentSQL(tableName: string, to: Column, schemaName?: string): string;
73
+ /** Comments are stored as `MS_Description` extended properties, which have separate add/update/drop procedures. */
74
+ private getCommentSQL;
70
75
  inferLengthFromColumnType(type: string): number | undefined;
71
76
  protected wrap(val: string | undefined, type: Type<unknown>): string | undefined;
72
77
  }
@@ -347,7 +347,7 @@ export class MsSqlSchemaHelper extends SchemaHelper {
347
347
  const timing = trigger.timing.toUpperCase();
348
348
  const events = trigger.events.map(e => e.toUpperCase()).join(', ');
349
349
  const qualifiedName = this.getSchemaQualifiedName(table, trigger.name);
350
- return `create trigger ${qualifiedName} on ${table.getQuotedName()} ${timing} ${events} as begin ${trigger.body}; end`;
350
+ return `create trigger ${qualifiedName} on ${table.getQuotedName()} ${timing} ${events} as begin ${this.normalizeTriggerBody(trigger.body)} end`;
351
351
  }
352
352
  /** Generates SQL to drop an MSSQL trigger. */
353
353
  dropTrigger(table, trigger) {
@@ -670,7 +670,8 @@ export class MsSqlSchemaHelper extends SchemaHelper {
670
670
  !column.generated &&
671
671
  !compositePK &&
672
672
  (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
673
- Utils.runIfNotEmpty(() => col.push('primary key'), primaryKey && column.primary);
673
+ const primaryKeyName = this.platform.getDefaultPrimaryName(table.name, [column.name]);
674
+ Utils.runIfNotEmpty(() => col.push(`constraint ${this.quote(primaryKeyName)} primary key`), primaryKey && column.primary);
674
675
  }
675
676
  const useDefault = changedProperties
676
677
  ? false
@@ -679,6 +680,10 @@ export class MsSqlSchemaHelper extends SchemaHelper {
679
680
  Utils.runIfNotEmpty(() => col.push(`constraint ${this.quote(defaultName)} default ${column.default}`), useDefault);
680
681
  return col.join(' ');
681
682
  }
683
+ // SQL Server generates a random `PK__…` name when the constraint is unnamed, so always name it
684
+ getPrimaryKeyConstraintPrefix(table, index) {
685
+ return `constraint ${this.quote(index.keyName)} `;
686
+ }
682
687
  alterTableColumn(column, table, changedProperties) {
683
688
  const parts = [];
684
689
  if (changedProperties.has('default')) {
@@ -794,31 +799,49 @@ export class MsSqlSchemaHelper extends SchemaHelper {
794
799
  return this.createTableColumn(column, table);
795
800
  })
796
801
  .join(', ');
797
- return [`alter table ${table.getQuotedName()} add ${adds}`];
802
+ const sql = [`alter table ${table.getQuotedName()} add ${adds}`];
803
+ for (const column of columns) {
804
+ if (column.comment) {
805
+ sql.push(this.getCommentSQL(table.schema, table.name, column.comment, column.name));
806
+ }
807
+ }
808
+ return sql;
798
809
  }
799
810
  appendComments(table) {
800
811
  const sql = [];
801
- const schema = this.platform.quoteValue(table.schema);
802
- const tableName = this.platform.quoteValue(table.name);
803
812
  if (table.comment) {
804
- const comment = this.platform.quoteValue(table.comment);
805
- sql.push(`if exists(select * from sys.fn_listextendedproperty(N'MS_Description', N'Schema', N${schema}, N'Table', N${tableName}, null, null))
806
- exec sys.sp_updateextendedproperty N'MS_Description', N${comment}, N'Schema', N${schema}, N'Table', N${tableName}
807
- else
808
- exec sys.sp_addextendedproperty N'MS_Description', N${comment}, N'Schema', N${schema}, N'Table', N${tableName}`);
813
+ sql.push(this.getCommentSQL(table.schema, table.name, table.comment));
809
814
  }
810
815
  for (const column of table.getColumns()) {
811
816
  if (column.comment) {
812
- const comment = this.platform.quoteValue(column.comment);
813
- const columnName = this.platform.quoteValue(column.name);
814
- sql.push(`if exists(select * from sys.fn_listextendedproperty(N'MS_Description', N'Schema', N${schema}, N'Table', N${tableName}, N'Column', N${columnName}))
815
- exec sys.sp_updateextendedproperty N'MS_Description', N${comment}, N'Schema', N${schema}, N'Table', N${tableName}, N'Column', N${columnName}
816
- else
817
- exec sys.sp_addextendedproperty N'MS_Description', N${comment}, N'Schema', N${schema}, N'Table', N${tableName}, N'Column', N${columnName}`);
817
+ sql.push(this.getCommentSQL(table.schema, table.name, column.comment, column.name));
818
818
  }
819
819
  }
820
820
  return sql;
821
821
  }
822
+ alterTableComment(table, comment) {
823
+ return this.getCommentSQL(table.schema, table.name, comment);
824
+ }
825
+ getChangeColumnCommentSQL(tableName, to, schemaName) {
826
+ return this.getCommentSQL(schemaName, tableName, to.comment, to.name);
827
+ }
828
+ /** Comments are stored as `MS_Description` extended properties, which have separate add/update/drop procedures. */
829
+ getCommentSQL(schemaName, tableName, comment, columnName) {
830
+ const schema = this.platform.quoteValue(schemaName ?? this.platform.getDefaultSchemaName());
831
+ const table = this.platform.quoteValue(tableName);
832
+ const level1 = `N'Schema', N${schema}, N'Table', N${table}`;
833
+ const level2 = columnName ? `N'Column', N${this.platform.quoteValue(columnName)}` : '';
834
+ const exists = `if exists(select * from sys.fn_listextendedproperty(N'MS_Description', ${level1}, ${level2 || 'null, null'}))`;
835
+ const target = level2 ? `${level1}, ${level2}` : level1;
836
+ if (!comment) {
837
+ return `${exists}\n exec sys.sp_dropextendedproperty N'MS_Description', ${target}`;
838
+ }
839
+ const value = this.platform.quoteValue(comment);
840
+ return `${exists}
841
+ exec sys.sp_updateextendedproperty N'MS_Description', N${value}, ${target}
842
+ else
843
+ exec sys.sp_addextendedproperty N'MS_Description', N${value}, ${target}`;
844
+ }
822
845
  inferLengthFromColumnType(type) {
823
846
  const match = /^(\w+)\s*\(\s*(-?\d+|max)\s*\)/.exec(type);
824
847
  if (!match) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mssql",
3
- "version": "7.2.0-dev.3",
3
+ "version": "7.2.0-dev.4",
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,17 +47,17 @@
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
49
  "dependencies": {
50
- "@mikro-orm/sql": "7.2.0-dev.3",
50
+ "@mikro-orm/sql": "7.2.0-dev.4",
51
51
  "kysely": "0.29.4",
52
52
  "tarn": "3.1.2",
53
53
  "tedious": "20.0.0",
54
54
  "tsqlstring": "1.0.1"
55
55
  },
56
56
  "devDependencies": {
57
- "@mikro-orm/core": "^7.1.7"
57
+ "@mikro-orm/core": "^7.1.11"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.2.0-dev.3"
60
+ "@mikro-orm/core": "7.2.0-dev.4"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">= 22.17.0"