@mikro-orm/mssql 7.0.0-rc.2 → 7.0.0-rc.3
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/MsSqlConnection.js +2 -2
- package/MsSqlDriver.js +8 -6
- package/MsSqlExceptionConverter.js +4 -1
- package/MsSqlPlatform.js +4 -4
- package/MsSqlSchemaHelper.js +50 -24
- package/package.json +31 -31
- package/tsconfig.build.tsbuildinfo +1 -1
package/MsSqlConnection.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbstractSqlConnection, Utils
|
|
1
|
+
import { AbstractSqlConnection, Utils } from '@mikro-orm/sql';
|
|
2
2
|
import { MssqlDialect } from 'kysely';
|
|
3
3
|
import * as Tedious from 'tedious';
|
|
4
4
|
import * as Tarn from 'tarn';
|
|
@@ -68,7 +68,7 @@ export class MsSqlConnection extends AbstractSqlConnection {
|
|
|
68
68
|
return res.rows;
|
|
69
69
|
}
|
|
70
70
|
const rowCount = res.rows.length;
|
|
71
|
-
const hasEmptyCount =
|
|
71
|
+
const hasEmptyCount = rowCount === 1 && '' in res.rows[0];
|
|
72
72
|
const emptyRow = hasEmptyCount && Number(res.rows[0]['']);
|
|
73
73
|
return {
|
|
74
74
|
affectedRows: hasEmptyCount ? emptyRow : Number(res.numAffectedRows),
|
package/MsSqlDriver.js
CHANGED
|
@@ -22,7 +22,9 @@ export class MsSqlDriver extends AbstractSqlDriver {
|
|
|
22
22
|
const returningFields = Utils.flatten(returningProps.map(prop => prop.fieldNames));
|
|
23
23
|
const using2 = `select * from (values ${data.map((x, i) => `(${i})`).join(',')}) v (id) where 1 = 1`;
|
|
24
24
|
/* v8 ignore next */
|
|
25
|
-
const output = returningFields.length > 0
|
|
25
|
+
const output = returningFields.length > 0
|
|
26
|
+
? `output ${returningFields.map(field => 'inserted.' + this.platform.quoteIdentifier(field)).join(', ')}`
|
|
27
|
+
: '';
|
|
26
28
|
const sql = `merge into ${tableName} using (${using2}) s on 1 = 0 when not matched then insert default values ${output};`;
|
|
27
29
|
const res = await this.execute(sql, [], 'run', options.ctx);
|
|
28
30
|
const pks = this.getPrimaryKeyFields(meta);
|
|
@@ -43,7 +45,9 @@ export class MsSqlDriver extends AbstractSqlDriver {
|
|
|
43
45
|
}
|
|
44
46
|
createQueryBuilder(entityName, ctx, preferredConnectionType, convertCustomTypes, loggerContext, alias, em) {
|
|
45
47
|
// do not compute the connectionType if EM is provided as it will be computed from it in the QB later on
|
|
46
|
-
const connectionType = em
|
|
48
|
+
const connectionType = em
|
|
49
|
+
? preferredConnectionType
|
|
50
|
+
: this.resolveConnectionType({ ctx, connectionType: preferredConnectionType });
|
|
47
51
|
const qb = new MsSqlQueryBuilder(entityName, this.metadata, this, ctx, alias, connectionType, em, loggerContext);
|
|
48
52
|
if (!convertCustomTypes) {
|
|
49
53
|
qb.unsetFlag(QueryFlag.CONVERT_CUSTOM_TYPES);
|
|
@@ -53,7 +57,7 @@ export class MsSqlDriver extends AbstractSqlDriver {
|
|
|
53
57
|
appendOutputTable(entityName, data, sql) {
|
|
54
58
|
const meta = this.metadata.get(entityName);
|
|
55
59
|
const returningProps = meta.props
|
|
56
|
-
.filter(prop => prop.persist !== false && prop.defaultRaw || prop.autoincrement || prop.generated)
|
|
60
|
+
.filter(prop => (prop.persist !== false && prop.defaultRaw) || prop.autoincrement || prop.generated)
|
|
57
61
|
.filter(prop => !(prop.name in data[0]) || isRaw(data[0][prop.name]));
|
|
58
62
|
const returningFields = Utils.flatten(returningProps.map(prop => prop.fieldNames));
|
|
59
63
|
/* v8 ignore next */
|
|
@@ -61,9 +65,7 @@ export class MsSqlDriver extends AbstractSqlDriver {
|
|
|
61
65
|
return sql;
|
|
62
66
|
}
|
|
63
67
|
const tableName = this.getTableName(meta, {}, true);
|
|
64
|
-
const selections = returningFields
|
|
65
|
-
.map((field) => `[t].${this.platform.quoteIdentifier(field)}`)
|
|
66
|
-
.join(',');
|
|
68
|
+
const selections = returningFields.map((field) => `[t].${this.platform.quoteIdentifier(field)}`).join(',');
|
|
67
69
|
const position = sql.indexOf(' values ');
|
|
68
70
|
const sqlBeforeValues = sql.substring(0, position);
|
|
69
71
|
const sqlAfterValues = sql.substring(position + 1);
|
|
@@ -7,7 +7,10 @@ export class MsSqlExceptionConverter extends ExceptionConverter {
|
|
|
7
7
|
convertException(exception) {
|
|
8
8
|
let errno = exception.number;
|
|
9
9
|
/* v8 ignore next */
|
|
10
|
-
if ('errors' in exception &&
|
|
10
|
+
if ('errors' in exception &&
|
|
11
|
+
Array.isArray(exception.errors) &&
|
|
12
|
+
typeof exception.errors[0] === 'object' &&
|
|
13
|
+
'message' in exception.errors[0]) {
|
|
11
14
|
exception.message += '\n' + exception.errors.map(e => e.message).join('\n');
|
|
12
15
|
errno ??= exception.errors[0].number;
|
|
13
16
|
exception.lineNumber ??= exception.errors[0].lineNumber;
|
package/MsSqlPlatform.js
CHANGED
|
@@ -130,9 +130,9 @@ export class MsSqlPlatform extends AbstractSqlPlatform {
|
|
|
130
130
|
}
|
|
131
131
|
validateMetadata(meta) {
|
|
132
132
|
for (const prop of meta.props) {
|
|
133
|
-
if ((prop.runtimeType === 'string' || ['string', 'nvarchar'].includes(prop.type))
|
|
134
|
-
|
|
135
|
-
|
|
133
|
+
if ((prop.runtimeType === 'string' || ['string', 'nvarchar'].includes(prop.type)) &&
|
|
134
|
+
!['uuid'].includes(prop.type) &&
|
|
135
|
+
!prop.columnTypes[0].startsWith('varchar')) {
|
|
136
136
|
prop.customType ??= new UnicodeStringType();
|
|
137
137
|
prop.customType.prop = prop;
|
|
138
138
|
prop.customType.platform = this;
|
|
@@ -148,7 +148,7 @@ export class MsSqlPlatform extends AbstractSqlPlatform {
|
|
|
148
148
|
boolean: 'bit',
|
|
149
149
|
};
|
|
150
150
|
const cast = (key) => raw(type in types ? `cast(${key} as ${types[type]})` : key);
|
|
151
|
-
const quoteKey = (key) => key.match(/^[a-z]\w*$/i) ? key : `"${key}"
|
|
151
|
+
const quoteKey = (key) => (key.match(/^[a-z]\w*$/i) ? key : `"${key}"`);
|
|
152
152
|
/* v8 ignore next */
|
|
153
153
|
if (path.length === 0) {
|
|
154
154
|
return cast(`json_value(${root}, '$.${b.map(quoteKey).join('.')}')`);
|
package/MsSqlSchemaHelper.js
CHANGED
|
@@ -2,8 +2,8 @@ import { EnumType, SchemaHelper, StringType, TextType, Utils, } from '@mikro-orm
|
|
|
2
2
|
import { UnicodeStringType } from './UnicodeStringType.js';
|
|
3
3
|
export class MsSqlSchemaHelper extends SchemaHelper {
|
|
4
4
|
static DEFAULT_VALUES = {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
true: ['1'],
|
|
6
|
+
false: ['0'],
|
|
7
7
|
'getdate()': ['current_timestamp'],
|
|
8
8
|
};
|
|
9
9
|
getManagementDbName() {
|
|
@@ -87,7 +87,7 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
87
87
|
where (${[...tablesBySchemas.entries()].map(([schema, tables]) => `(ic.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}) and ic.table_schema = '${schema}')`).join(' OR ')})
|
|
88
88
|
order by ordinal_position`;
|
|
89
89
|
const allColumns = await connection.execute(sql);
|
|
90
|
-
const str = (val) => val != null ? '' + val : val;
|
|
90
|
+
const str = (val) => (val != null ? '' + val : val);
|
|
91
91
|
const ret = {};
|
|
92
92
|
for (const col of allColumns) {
|
|
93
93
|
const mappedType = this.platform.getMappedType(col.data_type);
|
|
@@ -95,7 +95,9 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
95
95
|
const increments = col.is_identity === 1 && connection.getPlatform().isNumericColumn(mappedType);
|
|
96
96
|
const key = this.getTableKey(col);
|
|
97
97
|
/* v8 ignore next */
|
|
98
|
-
const generated = col.generation_expression
|
|
98
|
+
const generated = col.generation_expression
|
|
99
|
+
? `${col.generation_expression}${col.is_persisted ? ' persisted' : ''}`
|
|
100
|
+
: undefined;
|
|
99
101
|
let type = col.data_type;
|
|
100
102
|
if (['varchar', 'nvarchar', 'char', 'nchar', 'varbinary'].includes(col.data_type)) {
|
|
101
103
|
col.length = col.character_maximum_length;
|
|
@@ -115,7 +117,9 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
115
117
|
ret[key] ??= [];
|
|
116
118
|
ret[key].push({
|
|
117
119
|
name: col.column_name,
|
|
118
|
-
type: this.platform.isNumericColumn(mappedType)
|
|
120
|
+
type: this.platform.isNumericColumn(mappedType)
|
|
121
|
+
? col.data_type.replace(/ unsigned$/, '').replace(/\(\d+\)$/, '')
|
|
122
|
+
: type,
|
|
119
123
|
mappedType,
|
|
120
124
|
unsigned: col.data_type.endsWith(' unsigned'),
|
|
121
125
|
length: col.length,
|
|
@@ -237,7 +241,12 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
237
241
|
/* v8 ignore next */
|
|
238
242
|
const hasItems = (items?.length ?? 0) > 0;
|
|
239
243
|
if (item.columnName && hasItems) {
|
|
240
|
-
items = items
|
|
244
|
+
items = items
|
|
245
|
+
.map(val => val
|
|
246
|
+
.trim()
|
|
247
|
+
.replace(`[${item.columnName}]=`, '')
|
|
248
|
+
.match(/^\(?'(.*)'/)?.[1])
|
|
249
|
+
.filter(Boolean);
|
|
241
250
|
if (items.length > 0) {
|
|
242
251
|
o[item.columnName] = items.reverse();
|
|
243
252
|
}
|
|
@@ -310,7 +319,7 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
310
319
|
}
|
|
311
320
|
// convert to string first if it's not already a string or has a smaller length
|
|
312
321
|
const type = this.platform.extractSimpleType(col.fromColumn.type);
|
|
313
|
-
if (!['varchar', 'nvarchar', 'varbinary'].includes(type) ||
|
|
322
|
+
if (!['varchar', 'nvarchar', 'varbinary'].includes(type) || col.fromColumn.length < col.column.length) {
|
|
314
323
|
ret.push(`alter table ${quotedName} alter column [${col.oldColumnName}] nvarchar(max)`);
|
|
315
324
|
}
|
|
316
325
|
}
|
|
@@ -372,18 +381,21 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
372
381
|
}
|
|
373
382
|
const i = globalThis.idx;
|
|
374
383
|
globalThis.idx++;
|
|
375
|
-
constraints.push(`declare @constraint${i} varchar(100) = (select default_constraints.name from sys.all_columns`
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
384
|
+
constraints.push(`declare @constraint${i} varchar(100) = (select default_constraints.name from sys.all_columns` +
|
|
385
|
+
' join sys.tables on all_columns.object_id = tables.object_id' +
|
|
386
|
+
' join sys.schemas on tables.schema_id = schemas.schema_id' +
|
|
387
|
+
' join sys.default_constraints on all_columns.default_object_id = default_constraints.object_id' +
|
|
388
|
+
` where schemas.name = '${schemaName}' and tables.name = '${tableName}' and all_columns.name = '${column.name}')` +
|
|
389
|
+
` if @constraint${i} is not null exec('alter table ${tableNameRaw} drop constraint ' + @constraint${i})`);
|
|
381
390
|
}
|
|
382
391
|
return constraints;
|
|
383
392
|
}
|
|
384
393
|
getRenameColumnSQL(tableName, oldColumnName, to, schemaName) {
|
|
385
394
|
/* v8 ignore next */
|
|
386
|
-
const oldName = (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') +
|
|
395
|
+
const oldName = (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') +
|
|
396
|
+
tableName +
|
|
397
|
+
'.' +
|
|
398
|
+
oldColumnName;
|
|
387
399
|
const columnName = this.platform.quoteValue(to.name);
|
|
388
400
|
return `exec sp_rename ${this.platform.quoteValue(oldName)}, ${columnName}, 'COLUMN'`;
|
|
389
401
|
}
|
|
@@ -392,7 +404,10 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
392
404
|
const primaryKey = !changedProperties && !this.hasNonDefaultPrimaryKeyName(table);
|
|
393
405
|
const columnType = column.generated ? `as ${column.generated}` : column.type;
|
|
394
406
|
const col = [this.quote(column.name)];
|
|
395
|
-
if (column.autoincrement &&
|
|
407
|
+
if (column.autoincrement &&
|
|
408
|
+
!column.generated &&
|
|
409
|
+
!compositePK &&
|
|
410
|
+
(!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
|
|
396
411
|
col.push(column.mappedType.getColumnType({ autoincrement: true }, this.platform));
|
|
397
412
|
}
|
|
398
413
|
else {
|
|
@@ -401,10 +416,15 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
401
416
|
Utils.runIfNotEmpty(() => col.push('identity(1,1)'), column.autoincrement);
|
|
402
417
|
Utils.runIfNotEmpty(() => col.push('null'), column.nullable);
|
|
403
418
|
Utils.runIfNotEmpty(() => col.push('not null'), !column.nullable && !column.generated);
|
|
404
|
-
if (column.autoincrement &&
|
|
419
|
+
if (column.autoincrement &&
|
|
420
|
+
!column.generated &&
|
|
421
|
+
!compositePK &&
|
|
422
|
+
(!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
|
|
405
423
|
Utils.runIfNotEmpty(() => col.push('primary key'), primaryKey && column.primary);
|
|
406
424
|
}
|
|
407
|
-
const useDefault = changedProperties
|
|
425
|
+
const useDefault = changedProperties
|
|
426
|
+
? false
|
|
427
|
+
: column.default != null && column.default !== 'null' && !column.autoincrement;
|
|
408
428
|
const defaultName = this.platform.getConfig().getNamingStrategy().indexName(table.name, [column.name], 'default');
|
|
409
429
|
Utils.runIfNotEmpty(() => col.push(`constraint ${this.quote(defaultName)} default ${column.default}`), useDefault);
|
|
410
430
|
return col.join(' ');
|
|
@@ -459,14 +479,16 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
459
479
|
*/
|
|
460
480
|
getIndexColumns(index) {
|
|
461
481
|
if (index.columns?.length) {
|
|
462
|
-
return index.columns
|
|
482
|
+
return index.columns
|
|
483
|
+
.map(col => {
|
|
463
484
|
let colDef = this.quote(col.name);
|
|
464
485
|
// MSSQL supports sort order
|
|
465
486
|
if (col.sort) {
|
|
466
487
|
colDef += ` ${col.sort}`;
|
|
467
488
|
}
|
|
468
489
|
return colDef;
|
|
469
|
-
})
|
|
490
|
+
})
|
|
491
|
+
.join(', ');
|
|
470
492
|
}
|
|
471
493
|
return index.columnNames.map(c => this.quote(c)).join(', ');
|
|
472
494
|
}
|
|
@@ -490,8 +512,7 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
490
512
|
if (index.expression) {
|
|
491
513
|
return index.expression;
|
|
492
514
|
}
|
|
493
|
-
const needsWhereClause = index.unique
|
|
494
|
-
&& index.columnNames.some(column => table.getColumn(column)?.nullable);
|
|
515
|
+
const needsWhereClause = index.unique && index.columnNames.some(column => table.getColumn(column)?.nullable);
|
|
495
516
|
if (!needsWhereClause) {
|
|
496
517
|
return this.getCreateIndexSQL(table.getShortestName(), index);
|
|
497
518
|
}
|
|
@@ -517,9 +538,11 @@ export class MsSqlSchemaHelper extends SchemaHelper {
|
|
|
517
538
|
return `if object_id('${viewName}', 'V') is not null drop view ${viewName}`;
|
|
518
539
|
}
|
|
519
540
|
getAddColumnsSQL(table, columns) {
|
|
520
|
-
const adds = columns
|
|
541
|
+
const adds = columns
|
|
542
|
+
.map(column => {
|
|
521
543
|
return `${this.createTableColumn(column, table)}`;
|
|
522
|
-
})
|
|
544
|
+
})
|
|
545
|
+
.join(', ');
|
|
523
546
|
return [`alter table ${table.getQuotedName()} add ${adds}`];
|
|
524
547
|
}
|
|
525
548
|
appendComments(table) {
|
|
@@ -556,7 +579,10 @@ else
|
|
|
556
579
|
return +match[2];
|
|
557
580
|
}
|
|
558
581
|
wrap(val, type) {
|
|
559
|
-
const stringType = type instanceof StringType ||
|
|
582
|
+
const stringType = type instanceof StringType ||
|
|
583
|
+
type instanceof TextType ||
|
|
584
|
+
type instanceof EnumType ||
|
|
585
|
+
type instanceof UnicodeStringType;
|
|
560
586
|
return typeof val === 'string' && val.length > 0 && stringType ? this.platform.quoteValue(val) : val;
|
|
561
587
|
}
|
|
562
588
|
}
|
package/package.json
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/mssql",
|
|
3
|
-
"
|
|
4
|
-
"version": "7.0.0-rc.2",
|
|
3
|
+
"version": "7.0.0-rc.3",
|
|
5
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.",
|
|
6
|
-
"exports": {
|
|
7
|
-
"./package.json": "./package.json",
|
|
8
|
-
".": "./index.js"
|
|
9
|
-
},
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "git+ssh://git@github.com/mikro-orm/mikro-orm.git"
|
|
13
|
-
},
|
|
14
5
|
"keywords": [
|
|
15
|
-
"
|
|
6
|
+
"data-mapper",
|
|
7
|
+
"ddd",
|
|
8
|
+
"entity",
|
|
9
|
+
"identity-map",
|
|
10
|
+
"javascript",
|
|
11
|
+
"js",
|
|
12
|
+
"mariadb",
|
|
13
|
+
"mikro-orm",
|
|
16
14
|
"mongo",
|
|
17
15
|
"mongodb",
|
|
18
16
|
"mysql",
|
|
19
|
-
"
|
|
17
|
+
"orm",
|
|
20
18
|
"postgresql",
|
|
21
19
|
"sqlite",
|
|
22
20
|
"sqlite3",
|
|
23
21
|
"ts",
|
|
24
22
|
"typescript",
|
|
25
|
-
"
|
|
26
|
-
"javascript",
|
|
27
|
-
"entity",
|
|
28
|
-
"ddd",
|
|
29
|
-
"mikro-orm",
|
|
30
|
-
"unit-of-work",
|
|
31
|
-
"data-mapper",
|
|
32
|
-
"identity-map"
|
|
23
|
+
"unit-of-work"
|
|
33
24
|
],
|
|
34
|
-
"
|
|
35
|
-
"license": "MIT",
|
|
25
|
+
"homepage": "https://mikro-orm.io",
|
|
36
26
|
"bugs": {
|
|
37
27
|
"url": "https://github.com/mikro-orm/mikro-orm/issues"
|
|
38
28
|
},
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"author": "Martin Adámek",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+ssh://git@github.com/mikro-orm/mikro-orm.git"
|
|
34
|
+
},
|
|
35
|
+
"type": "module",
|
|
36
|
+
"exports": {
|
|
37
|
+
"./package.json": "./package.json",
|
|
38
|
+
".": "./index.js"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "yarn compile && yarn copy",
|
|
@@ -46,20 +46,20 @@
|
|
|
46
46
|
"compile": "yarn run -T tsc -p tsconfig.build.json",
|
|
47
47
|
"copy": "node ../../scripts/copy.mjs"
|
|
48
48
|
},
|
|
49
|
-
"publishConfig": {
|
|
50
|
-
"access": "public"
|
|
51
|
-
},
|
|
52
49
|
"dependencies": {
|
|
53
|
-
"@mikro-orm/sql": "7.0.0-rc.
|
|
50
|
+
"@mikro-orm/sql": "7.0.0-rc.3",
|
|
54
51
|
"kysely": "0.28.11",
|
|
55
52
|
"tarn": "3.0.2",
|
|
56
53
|
"tedious": "19.2.1",
|
|
57
54
|
"tsqlstring": "1.0.1"
|
|
58
55
|
},
|
|
59
56
|
"devDependencies": {
|
|
60
|
-
"@mikro-orm/core": "^6.6.
|
|
57
|
+
"@mikro-orm/core": "^6.6.8"
|
|
61
58
|
},
|
|
62
59
|
"peerDependencies": {
|
|
63
|
-
"@mikro-orm/core": "7.0.0-rc.
|
|
60
|
+
"@mikro-orm/core": "7.0.0-rc.3"
|
|
61
|
+
},
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">= 22.17.0"
|
|
64
64
|
}
|
|
65
65
|
}
|