@mikro-orm/mssql 7.1.9-dev.8 → 7.1.9

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.
@@ -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
  }
@@ -67,6 +67,10 @@ export declare class MsSqlSchemaHelper extends SchemaHelper {
67
67
  dropViewIfExists(name: string, schema?: string): string;
68
68
  getAddColumnsSQL(table: DatabaseTable, columns: Column[]): string[];
69
69
  appendComments(table: DatabaseTable): string[];
70
+ alterTableComment(table: DatabaseTable, comment?: string): string;
71
+ getChangeColumnCommentSQL(tableName: string, to: Column, schemaName?: string): string;
72
+ /** Comments are stored as `MS_Description` extended properties, which have separate add/update/drop procedures. */
73
+ private getCommentSQL;
70
74
  inferLengthFromColumnType(type: string): number | undefined;
71
75
  protected wrap(val: string | undefined, type: Type<unknown>): string | undefined;
72
76
  }
@@ -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) {
@@ -794,31 +794,49 @@ export class MsSqlSchemaHelper extends SchemaHelper {
794
794
  return this.createTableColumn(column, table);
795
795
  })
796
796
  .join(', ');
797
- return [`alter table ${table.getQuotedName()} add ${adds}`];
797
+ const sql = [`alter table ${table.getQuotedName()} add ${adds}`];
798
+ for (const column of columns) {
799
+ if (column.comment) {
800
+ sql.push(this.getCommentSQL(table.schema, table.name, column.comment, column.name));
801
+ }
802
+ }
803
+ return sql;
798
804
  }
799
805
  appendComments(table) {
800
806
  const sql = [];
801
- const schema = this.platform.quoteValue(table.schema);
802
- const tableName = this.platform.quoteValue(table.name);
803
807
  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}`);
808
+ sql.push(this.getCommentSQL(table.schema, table.name, table.comment));
809
809
  }
810
810
  for (const column of table.getColumns()) {
811
811
  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}`);
812
+ sql.push(this.getCommentSQL(table.schema, table.name, column.comment, column.name));
818
813
  }
819
814
  }
820
815
  return sql;
821
816
  }
817
+ alterTableComment(table, comment) {
818
+ return this.getCommentSQL(table.schema, table.name, comment);
819
+ }
820
+ getChangeColumnCommentSQL(tableName, to, schemaName) {
821
+ return this.getCommentSQL(schemaName, tableName, to.comment, to.name);
822
+ }
823
+ /** Comments are stored as `MS_Description` extended properties, which have separate add/update/drop procedures. */
824
+ getCommentSQL(schemaName, tableName, comment, columnName) {
825
+ const schema = this.platform.quoteValue(schemaName ?? this.platform.getDefaultSchemaName());
826
+ const table = this.platform.quoteValue(tableName);
827
+ const level1 = `N'Schema', N${schema}, N'Table', N${table}`;
828
+ const level2 = columnName ? `N'Column', N${this.platform.quoteValue(columnName)}` : '';
829
+ const exists = `if exists(select * from sys.fn_listextendedproperty(N'MS_Description', ${level1}, ${level2 || 'null, null'}))`;
830
+ const target = level2 ? `${level1}, ${level2}` : level1;
831
+ if (!comment) {
832
+ return `${exists}\n exec sys.sp_dropextendedproperty N'MS_Description', ${target}`;
833
+ }
834
+ const value = this.platform.quoteValue(comment);
835
+ return `${exists}
836
+ exec sys.sp_updateextendedproperty N'MS_Description', N${value}, ${target}
837
+ else
838
+ exec sys.sp_addextendedproperty N'MS_Description', N${value}, ${target}`;
839
+ }
822
840
  inferLengthFromColumnType(type) {
823
841
  const match = /^(\w+)\s*\(\s*(-?\d+|max)\s*\)/.exec(type);
824
842
  if (!match) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mssql",
3
- "version": "7.1.9-dev.8",
3
+ "version": "7.1.9",
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.1.9-dev.8",
50
+ "@mikro-orm/sql": "7.1.9",
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.8"
57
+ "@mikro-orm/core": "^7.1.9"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.1.9-dev.8"
60
+ "@mikro-orm/core": "7.1.9"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">= 22.17.0"