@mikro-orm/sql 7.2.1-dev.7 → 7.2.1-dev.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.
@@ -100,7 +100,7 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
100
100
  */
101
101
  private convertOwnerPksForPivotQuery;
102
102
  private getPivotOrderBy;
103
- execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(query: string | NativeQueryBuilder | RawQueryFragment, params?: any[] | Dictionary, method?: 'all' | 'get' | 'run', ctx?: Transaction, loggerContext?: LoggingOptions): Promise<T>;
103
+ execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(query: string | NativeQueryBuilder | RawQueryFragment, params?: any[] | Dictionary, method?: 'all' | 'get' | 'run', ctx?: Transaction, loggerContext?: LoggingOptions, connection?: AbstractSqlConnection): Promise<T>;
104
104
  stream<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: StreamOptions<T, any, any, any>): AsyncIterableIterator<T>;
105
105
  /**
106
106
  * 1:1 owner side needs to be marked for population so QB auto-joins the owner id
@@ -1636,8 +1636,8 @@ export class AbstractSqlDriver extends DatabaseDriver {
1636
1636
  }
1637
1637
  return [];
1638
1638
  }
1639
- async execute(query, params = [], method = 'all', ctx, loggerContext) {
1640
- return this.rethrow(this.connection.execute(query, params, method, ctx, loggerContext));
1639
+ async execute(query, params = [], method = 'all', ctx, loggerContext, connection = this.connection) {
1640
+ return this.rethrow(connection.execute(query, params, method, ctx, loggerContext));
1641
1641
  }
1642
1642
  async *stream(entityName, where, options) {
1643
1643
  options = { populate: [], orderBy: [], ...options };
@@ -8,6 +8,8 @@ import type { InferClassEntityDB, InferKyselyDB } from './typings.js';
8
8
  import { type MikroKyselyPluginOptions } from './plugin/index.js';
9
9
  /** Options for the modern signature of `SqlEntityManager.execute()`. */
10
10
  export interface EmExecuteOptions extends AbortQueryOptions {
11
+ /** Connection to use outside a transaction. Defaults to 'write'; an active transaction always takes precedence. */
12
+ connectionType?: ConnectionType;
11
13
  /** Result shape — `'all'` for rows, `'get'` for a single row, `'run'` for affected count. Defaults to `'all'`. */
12
14
  method?: 'all' | 'get' | 'run';
13
15
  /** Logger context payload forwarded to `Logger.logQuery`. */
@@ -62,8 +64,8 @@ export declare class SqlEntityManager<Driver extends AbstractSqlDriver = Abstrac
62
64
  */
63
65
  execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(query: string | NativeQueryBuilder | RawQueryFragment, params?: any[] | Dictionary, method?: 'all' | 'get' | 'run', loggerContext?: LoggingOptions): Promise<T>;
64
66
  /**
65
- * Executes a raw SQL query with an options bag carrying `method`, `loggerContext`, `signal`
66
- * and `inflightQueryAbortStrategy`. Per-call `signal` / `inflightQueryAbortStrategy` override
67
+ * Executes a raw SQL query with an options bag carrying `method`, `loggerContext`, `signal`,
68
+ * `connectionType` and `inflightQueryAbortStrategy`. Per-call `signal` / `inflightQueryAbortStrategy` override
67
69
  * the fork-level defaults set via `em.fork({ signal })`. The current transaction context is
68
70
  * applied automatically.
69
71
  */
@@ -68,7 +68,10 @@ export class SqlEntityManager extends EntityManager {
68
68
  merged.signal = opts.signal ?? fork?.signal;
69
69
  merged.inflightQueryAbortStrategy = opts.inflightQueryAbortStrategy ?? fork?.inflightQueryAbortStrategy;
70
70
  }
71
- return context.withSessionContext(context.getTransactionContext(), ctx => this.getDriver().execute(query, params, opts.method ?? 'all', ctx, merged));
71
+ const transaction = context.getTransactionContext();
72
+ // Resolve once so implicit RLS transactions and query execution use the same replica.
73
+ const connection = context.getConnection(transaction ? 'write' : (opts.connectionType ?? 'write'));
74
+ return context.withSessionContext(transaction, ctx => this.getDriver().execute(query, params, opts.method ?? 'all', ctx, merged, connection), connection);
72
75
  }
73
76
  /**
74
77
  * @inheritDoc
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.2.1-dev.7",
3
+ "version": "7.2.1-dev.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",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.2.0"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.1-dev.7"
56
+ "@mikro-orm/core": "7.2.1-dev.9"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"