@mikro-orm/mysql 7.1.0-dev.43 → 7.1.0-dev.44

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.
@@ -1,9 +1,11 @@
1
1
  import { type ControlledTransaction, MysqlDialect } from 'kysely';
2
2
  import { type PoolOptions } from 'mysql2';
3
+ import { type Routine, type Transaction } from '@mikro-orm/core';
3
4
  import { AbstractSqlConnection, type TransactionEventBroadcaster } from '@mikro-orm/sql';
4
5
  /** MySQL database connection using the `mysql2` driver. */
5
6
  export declare class MySqlConnection extends AbstractSqlConnection {
6
7
  createKyselyDialect(overrides: PoolOptions): Promise<MysqlDialect>;
7
8
  mapOptions(overrides: PoolOptions): PoolOptions;
9
+ callRoutine<T>(routine: Routine, args?: Record<string, unknown>, ctx?: Transaction): Promise<T>;
8
10
  commit(ctx: ControlledTransaction<any, any>, eventBroadcaster?: TransactionEventBroadcaster): Promise<void>;
9
11
  }
@@ -70,6 +70,52 @@ export class MySqlConnection extends AbstractSqlConnection {
70
70
  ret.dateStrings = true;
71
71
  return Utils.mergeConfig(ret, overrides);
72
72
  }
73
+ async callRoutine(routine, args = {}, ctx) {
74
+ if (routine.type === 'function') {
75
+ return this.callRoutineFunction(routine, args, ctx);
76
+ }
77
+ const name = this.platform.quoteIdentifier(routine.name);
78
+ const callPlaceholders = [];
79
+ const callValues = [];
80
+ const outVarParams = [];
81
+ routine.params.forEach((p, i) => {
82
+ if (p.direction === 'in') {
83
+ callPlaceholders.push('?');
84
+ callValues.push(this.convertRoutineInbound(args[p.name], p));
85
+ return;
86
+ }
87
+ const varName = `@_mikro_orm_routine_${i}`;
88
+ outVarParams.push({ name: p.name, varName, param: p });
89
+ callPlaceholders.push(varName);
90
+ });
91
+ // MySQL `@var`s are connection-scoped, so SET + CALL + SELECT must share one physical
92
+ // connection — wrap in an implicit transaction when the caller didn't supply one.
93
+ const needsConnectionAffinity = outVarParams.length > 0 && !ctx;
94
+ const runSteps = async (sharedCtx) => {
95
+ for (let i = 0; i < routine.params.length; i++) {
96
+ const p = routine.params[i];
97
+ if (p.direction === 'inout') {
98
+ const varName = `@_mikro_orm_routine_${i}`;
99
+ await this.execute(`set ${varName} := ?`, [this.convertRoutineInbound(args[p.name], p)], 'run', sharedCtx);
100
+ }
101
+ }
102
+ // mysql2 trails the result sets with an OK packet (non-array); filter to row arrays.
103
+ const callResult = (await this.execute(`call ${name}(${callPlaceholders.join(', ')})`, callValues, 'all', sharedCtx));
104
+ const resultSets = callResult.filter(Array.isArray);
105
+ if (outVarParams.length > 0) {
106
+ const selectClause = outVarParams
107
+ .map(o => `${o.varName} as ${this.platform.quoteIdentifier(o.name)}`)
108
+ .join(', ');
109
+ const rows = (await this.execute(`select ${selectClause}`, [], 'all', sharedCtx));
110
+ this.applyRoutineOutParams(rows[0] ?? {}, outVarParams.map(o => o.param), args);
111
+ }
112
+ return (resultSets.length > 0 ? resultSets : undefined);
113
+ };
114
+ if (needsConnectionAffinity) {
115
+ return this.transactional(trx => runSteps(trx));
116
+ }
117
+ return runSteps(ctx);
118
+ }
73
119
  async commit(ctx, eventBroadcaster) {
74
120
  if (!ctx.isRolledBack && 'savepointName' in ctx) {
75
121
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mysql",
3
- "version": "7.1.0-dev.43",
3
+ "version": "7.1.0-dev.44",
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.1.0-dev.43",
50
+ "@mikro-orm/sql": "7.1.0-dev.44",
51
51
  "kysely": "0.29.2",
52
52
  "mysql2": "3.22.3",
53
53
  "sqlstring": "2.3.3"
@@ -56,7 +56,7 @@
56
56
  "@mikro-orm/core": "^7.0.17"
57
57
  },
58
58
  "peerDependencies": {
59
- "@mikro-orm/core": "7.1.0-dev.43"
59
+ "@mikro-orm/core": "7.1.0-dev.44"
60
60
  },
61
61
  "engines": {
62
62
  "node": ">= 22.17.0"