@mikro-orm/mssql 7.2.10-dev.20 → 7.2.10-dev.21

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.
@@ -45,18 +45,32 @@ export class MsSqlConnection extends AbstractSqlConnection {
45
45
  const password = options.authentication?.options?.password;
46
46
  const onCreateConnection = this.options.onCreateConnection ?? this.config.get('onCreateConnection');
47
47
  const onReserveConnection = this.options.onReserveConnection ?? this.config.get('onReserveConnection');
48
+ const connections = new WeakMap();
49
+ let pending;
50
+ // Kysely validates with a `select 1` on every checkout, a state check is enough.
51
+ class Pool extends Tarn.Pool {
52
+ constructor(poolOptions) {
53
+ const create = poolOptions.create;
54
+ super({
55
+ ...poolOptions,
56
+ create: async () => {
57
+ options.authentication.options.password = typeof password === 'function' ? await password() : password;
58
+ const connection = new Tedious.Connection(options);
59
+ /* v8 ignore next */
60
+ await onCreateConnection?.(connection);
61
+ // kysely calls the `connectionFactory` synchronously
62
+ pending = connection;
63
+ const resource = await create();
64
+ connections.set(resource, connection);
65
+ return resource;
66
+ },
67
+ validate: resource => !connections.get(resource).closed,
68
+ });
69
+ }
70
+ }
48
71
  return new MsSqlReserveDialect({
49
- tarn: { ...Tarn, options: poolOptions },
50
- tedious: {
51
- ...Tedious,
52
- connectionFactory: async () => {
53
- options.authentication.options.password = typeof password === 'function' ? await password() : password;
54
- const connection = new Tedious.Connection(options);
55
- /* v8 ignore next */
56
- await onCreateConnection?.(connection);
57
- return connection;
58
- },
59
- },
72
+ tarn: { ...Tarn, Pool, options: poolOptions },
73
+ tedious: { ...Tedious, connectionFactory: () => pending },
60
74
  }, onReserveConnection);
61
75
  }
62
76
  mapOptions(overrides) {
@@ -61,6 +61,7 @@ export declare class MsSqlPlatform extends AbstractSqlPlatform {
61
61
  length?: number;
62
62
  }): string;
63
63
  validateMetadata(meta: EntityMetadata): void;
64
+ private initUnicodeType;
64
65
  getSearchJsonPropertyKey(path: string[], type: string, aliased: boolean | string, value?: unknown): string | RawQueryFragment;
65
66
  getJsonArrayFromSQL(column: string, alias: string, properties: {
66
67
  name: string;
package/MsSqlPlatform.js CHANGED
@@ -1,4 +1,4 @@
1
- import { AbstractSqlPlatform, ALIAS_REPLACEMENT, DoubleType, FloatType, MsSqlNativeQueryBuilder, QueryOrder, raw, RawQueryFragment, Type, } from '@mikro-orm/sql';
1
+ import { AbstractSqlPlatform, ALIAS_REPLACEMENT, DoubleType, FloatType, MsSqlNativeQueryBuilder, QueryOrder, raw, ReferenceKind, RawQueryFragment, Type, } from '@mikro-orm/sql';
2
2
  // @ts-expect-error no types available
3
3
  import SqlString from 'tsqlstring';
4
4
  import { MsSqlSchemaHelper } from './MsSqlSchemaHelper.js';
@@ -161,13 +161,29 @@ export class MsSqlPlatform extends AbstractSqlPlatform {
161
161
  validateMetadata(meta) {
162
162
  super.validateMetadata(meta);
163
163
  for (const prop of meta.props) {
164
- if ((prop.runtimeType === 'string' || ['string', 'nvarchar'].includes(prop.type)) &&
165
- !['uuid'].includes(prop.type) &&
166
- !prop.columnTypes[0].startsWith('varchar')) {
167
- prop.customType ??= new UnicodeStringType();
168
- prop.customType.prop = prop;
169
- prop.customType.platform = this;
170
- prop.customType.meta = meta;
164
+ this.initUnicodeType(meta, prop);
165
+ if (!prop.customType &&
166
+ [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
167
+ prop.fieldNames?.length === 1 &&
168
+ !prop.polymorphic) {
169
+ // discovery copies the custom type of the target column to the relation before it is set here
170
+ const target = prop.targetKey ? prop.targetMeta.properties[prop.targetKey] : prop.targetMeta.getPrimaryProp();
171
+ this.initUnicodeType(prop.targetMeta, target);
172
+ prop.customType = target.customType;
173
+ }
174
+ }
175
+ }
176
+ initUnicodeType(meta, prop) {
177
+ if ((prop.runtimeType === 'string' || ['string', 'nvarchar'].includes(prop.type)) &&
178
+ !['uuid'].includes(prop.type) &&
179
+ !prop.columnTypes[0].startsWith('varchar')) {
180
+ prop.customType ??= new UnicodeStringType();
181
+ prop.customType.prop = prop;
182
+ prop.customType.platform = this;
183
+ prop.customType.meta = meta;
184
+ // the comparator skips the custom type for plain `string` props, while the hydrator snapshots its converted value
185
+ if (prop.type === 'string') {
186
+ prop.type = prop.customType.name;
171
187
  }
172
188
  }
173
189
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mssql",
3
- "version": "7.2.10-dev.20",
3
+ "version": "7.2.10-dev.21",
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.2.10-dev.20",
50
+ "@mikro-orm/sql": "7.2.10-dev.21",
51
51
  "kysely": "0.29.9",
52
52
  "tarn": "3.1.2",
53
53
  "tedious": "20.3.9",
@@ -57,7 +57,7 @@
57
57
  "@mikro-orm/core": "^7.2.9"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.2.10-dev.20"
60
+ "@mikro-orm/core": "7.2.10-dev.21"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">= 22.17.0"