@mikro-orm/sql 7.1.0-dev.34 → 7.1.0-dev.36

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.
@@ -31,7 +31,28 @@ export declare class SqlEntityManager<Driver extends AbstractSqlDriver = Abstrac
31
31
  */
32
32
  qb<Entity extends object, RootAlias extends string = never>(entityName: EntityName<Entity>, alias?: RootAlias, type?: ConnectionType, loggerContext?: LoggingOptions): QueryBuilder<Entity, RootAlias>;
33
33
  /**
34
- * Returns configured Kysely instance.
34
+ * Returns a configured Kysely instance bound to this EntityManager.
35
+ *
36
+ * When the EntityManager is inside a transaction (e.g. within `em.transactional(...)`, or after
37
+ * `em.begin()`), the returned Kysely instance automatically uses the transaction context, so any
38
+ * queries executed via Kysely's own `.execute()` / `.executeTakeFirst*()` participate in the
39
+ * current transaction.
40
+ *
41
+ * If you need a Kysely instance that is **not** bound to the current transaction (e.g. to perform
42
+ * a side query against the pool while inside a transactional block), fork the EntityManager first:
43
+ *
44
+ * ```ts
45
+ * await em.transactional(async em => {
46
+ * // bound to the current transaction
47
+ * await em.getKysely().selectFrom('user').selectAll().execute();
48
+ *
49
+ * // bound to the pool, runs outside the transaction
50
+ * await em.fork().getKysely().selectFrom('audit_log').selectAll().execute();
51
+ * });
52
+ * ```
53
+ *
54
+ * The `options.type` (`'read'` / `'write'`) is only honored outside a transaction — inside a
55
+ * transaction the connection is already pinned.
35
56
  */
36
57
  getKysely<TDB = undefined, TOptions extends GetKyselyOptions = GetKyselyOptions>(options?: TOptions): Kysely<TDB extends undefined ? InferKyselyDB<EntitiesFromManager<this>, TOptions> & InferClassEntityDB<AllEntitiesFromManager<this>, TOptions> : TDB>;
37
58
  /**
@@ -20,10 +20,33 @@ export class SqlEntityManager extends EntityManager {
20
20
  return this.createQueryBuilder(entityName, alias, type, loggerContext);
21
21
  }
22
22
  /**
23
- * Returns configured Kysely instance.
23
+ * Returns a configured Kysely instance bound to this EntityManager.
24
+ *
25
+ * When the EntityManager is inside a transaction (e.g. within `em.transactional(...)`, or after
26
+ * `em.begin()`), the returned Kysely instance automatically uses the transaction context, so any
27
+ * queries executed via Kysely's own `.execute()` / `.executeTakeFirst*()` participate in the
28
+ * current transaction.
29
+ *
30
+ * If you need a Kysely instance that is **not** bound to the current transaction (e.g. to perform
31
+ * a side query against the pool while inside a transactional block), fork the EntityManager first:
32
+ *
33
+ * ```ts
34
+ * await em.transactional(async em => {
35
+ * // bound to the current transaction
36
+ * await em.getKysely().selectFrom('user').selectAll().execute();
37
+ *
38
+ * // bound to the pool, runs outside the transaction
39
+ * await em.fork().getKysely().selectFrom('audit_log').selectAll().execute();
40
+ * });
41
+ * ```
42
+ *
43
+ * The `options.type` (`'read'` / `'write'`) is only honored outside a transaction — inside a
44
+ * transaction the connection is already pinned.
24
45
  */
25
46
  getKysely(options = {}) {
26
- let kysely = this.getConnection(options.type).getClient();
47
+ const context = this.getContext(false);
48
+ const ctx = context.getTransactionContext();
49
+ let kysely = ctx ?? this.getConnection(options.type).getClient();
27
50
  if (options.columnNamingStrategy != null ||
28
51
  options.tableNamingStrategy != null ||
29
52
  options.processOnCreateHooks != null ||
@@ -35,11 +35,9 @@ export declare class BaseMySqlPlatform extends AbstractSqlPlatform {
35
35
  getDefaultMappedType(type: string): Type<unknown>;
36
36
  isNumericColumn(mappedType: Type<unknown>): boolean;
37
37
  supportsUnsigned(): boolean;
38
- /**
39
- * Returns the default name of index for the given columns
40
- * cannot go past 64 character length for identifiers in MySQL
41
- */
42
- getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
38
+ /** MySQL/MariaDB identifier limit. */
39
+ getMaxIdentifierLength(): number;
40
+ getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence' | 'check'): string;
43
41
  getDefaultPrimaryName(tableName: string, columns: string[]): string;
44
42
  supportsCreatingFullTextIndex(): boolean;
45
43
  getFullTextWhereClause(): string;
@@ -1,4 +1,4 @@
1
- import { Utils, QueryOrder, DecimalType, DoubleType, } from '@mikro-orm/core';
1
+ import { QueryOrder, DecimalType, DoubleType, } from '@mikro-orm/core';
2
2
  import { MySqlSchemaHelper } from './MySqlSchemaHelper.js';
3
3
  import { MySqlExceptionConverter } from './MySqlExceptionConverter.js';
4
4
  import { AbstractSqlPlatform } from '../../AbstractSqlPlatform.js';
@@ -86,19 +86,15 @@ export class BaseMySqlPlatform extends AbstractSqlPlatform {
86
86
  supportsUnsigned() {
87
87
  return true;
88
88
  }
89
- /**
90
- * Returns the default name of index for the given columns
91
- * cannot go past 64 character length for identifiers in MySQL
92
- */
89
+ /** MySQL/MariaDB identifier limit. */
90
+ getMaxIdentifierLength() {
91
+ return 64;
92
+ }
93
93
  getIndexName(tableName, columns, type) {
94
94
  if (type === 'primary') {
95
95
  return this.getDefaultPrimaryName(tableName, columns);
96
96
  }
97
- const indexName = super.getIndexName(tableName, columns, type);
98
- if (indexName.length > 64) {
99
- return `${indexName.substring(0, 56 - type.length)}_${Utils.hash(indexName, 5)}_${type}`;
100
- }
101
- return indexName;
97
+ return super.getIndexName(tableName, columns, type);
102
98
  }
103
99
  getDefaultPrimaryName(tableName, columns) {
104
100
  return 'PRIMARY'; // https://dev.mysql.com/doc/refman/8.0/en/create-table.html#create-table-indexes-keys
@@ -100,11 +100,8 @@ export declare class BasePostgreSqlPlatform extends AbstractSqlPlatform {
100
100
  getDefaultMappedType(type: string): Type<unknown>;
101
101
  supportsSchemas(): boolean;
102
102
  getDefaultSchemaName(): string | undefined;
103
- /**
104
- * Returns the default name of index for the given columns
105
- * cannot go past 63 character length for identifiers in MySQL
106
- */
107
- getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
103
+ /** Postgres identifier limit (NAMEDATALEN - 1). */
104
+ getMaxIdentifierLength(): number;
108
105
  getDefaultPrimaryName(tableName: string, columns: string[]): string;
109
106
  /**
110
107
  * @inheritDoc
@@ -383,17 +383,9 @@ export class BasePostgreSqlPlatform extends AbstractSqlPlatform {
383
383
  getDefaultSchemaName() {
384
384
  return 'public';
385
385
  }
386
- /**
387
- * Returns the default name of index for the given columns
388
- * cannot go past 63 character length for identifiers in MySQL
389
- */
390
- getIndexName(tableName, columns, type) {
391
- const indexName = super.getIndexName(tableName, columns, type);
392
- if (indexName.length > 63) {
393
- const suffix = type === 'primary' ? 'pkey' : type;
394
- return `${indexName.substring(0, 55 - type.length)}_${Utils.hash(indexName, 5)}_${suffix}`;
395
- }
396
- return indexName;
386
+ /** Postgres identifier limit (NAMEDATALEN - 1). */
387
+ getMaxIdentifierLength() {
388
+ return 63;
397
389
  }
398
390
  getDefaultPrimaryName(tableName, columns) {
399
391
  const indexName = `${tableName}_pkey`;
@@ -63,7 +63,7 @@ export declare class SqlitePlatform extends AbstractSqlPlatform {
63
63
  * including all Date properties, as we would be comparing Date object with timestamp.
64
64
  */
65
65
  processDateProperty(value: unknown): string | number | Date;
66
- getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
66
+ getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence' | 'check'): string;
67
67
  supportsDeferredUniqueConstraints(): boolean;
68
68
  /**
69
69
  * SQLite supports schemas via ATTACH DATABASE. Returns true when there are
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.1.0-dev.34",
3
+ "version": "7.1.0-dev.36",
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",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.0.15"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.1.0-dev.34"
56
+ "@mikro-orm/core": "7.1.0-dev.36"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"