@mikro-orm/sql 7.1.0-dev.33 → 7.1.0-dev.35

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 ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.1.0-dev.33",
3
+ "version": "7.1.0-dev.35",
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.33"
56
+ "@mikro-orm/core": "7.1.0-dev.35"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"