@mikro-orm/mysql 7.1.0-dev.8 → 7.1.0

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
  }
@@ -11,12 +11,18 @@ export class MySqlConnection extends AbstractSqlConnection {
11
11
  const innerPool = createPool({ ...options, password: initialPassword });
12
12
  // mysql2 reads pool.config.connectionConfig.password when creating new physical
13
13
  // connections, so updating it before getConnection() ensures fresh tokens are used.
14
- // Existing idle connections are already authenticated and unaffected.
14
+ // Existing idle connections are already authenticated and unaffected, so we skip
15
+ // the callback when the pool has a free connection to reuse.
15
16
  const pool = {
16
17
  getConnection(cb) {
18
+ const inner = innerPool;
19
+ if ((inner._freeConnections?.length ?? 0) > 0) {
20
+ innerPool.getConnection(cb);
21
+ return;
22
+ }
17
23
  Promise.resolve(password())
18
24
  .then(pw => {
19
- innerPool.config.connectionConfig.password = pw;
25
+ inner.config.connectionConfig.password = pw;
20
26
  innerPool.getConnection(cb);
21
27
  })
22
28
  .catch(err => cb(err, undefined));
@@ -40,6 +46,17 @@ export class MySqlConnection extends AbstractSqlConnection {
40
46
  const pool = this.config.get('pool');
41
47
  ret.connectionLimit = pool?.max;
42
48
  ret.idleTimeout = pool?.idleTimeoutMillis;
49
+ // mysql2 only runs idle cleanup when `maxIdle < connectionLimit`; its default has
50
+ // them equal, so `idleTimeout` alone is a no-op. When the user opts into idle
51
+ // cleanup via `pool.idleTimeoutMillis`, default `maxIdle` to `pool.min ?? 0` so
52
+ // idle connections actually drain. A misconfigured `pool.min > pool.max` is
53
+ // clamped below `pool.max` so cleanup still runs. Explicit `driverOptions.maxIdle`
54
+ // still wins because `overrides` is merged in last.
55
+ if (pool?.idleTimeoutMillis != null) {
56
+ const min = pool.min ?? 0;
57
+ const max = pool.max;
58
+ ret.maxIdle = max != null && min > max ? Math.max(0, max - 1) : min;
59
+ }
43
60
  if (this.config.get('multipleStatements')) {
44
61
  ret.multipleStatements = this.config.get('multipleStatements');
45
62
  }
@@ -53,6 +70,52 @@ export class MySqlConnection extends AbstractSqlConnection {
53
70
  ret.dateStrings = true;
54
71
  return Utils.mergeConfig(ret, overrides);
55
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
+ }
56
119
  async commit(ctx, eventBroadcaster) {
57
120
  if (!ctx.isRolledBack && 'savepointName' in ctx) {
58
121
  try {
@@ -1,5 +1,5 @@
1
- import { type AnyEntity, type EntityClass, type EntitySchema, MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from '@mikro-orm/core';
2
- import type { SqlEntityManager } from '@mikro-orm/sql';
1
+ import { type AnyEntity, type EntityClass, type EntitySchema, type MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from '@mikro-orm/core';
2
+ import { SqlMikroORM, type SqlEntityManager } from '@mikro-orm/sql';
3
3
  import { MySqlDriver } from './MySqlDriver.js';
4
4
  /** Configuration options for the MySQL driver. */
5
5
  export type MySqlOptions<EM extends SqlEntityManager<MySqlDriver> = SqlEntityManager<MySqlDriver>, Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> = Partial<Options<MySqlDriver, EM, Entities>>;
@@ -8,7 +8,7 @@ export declare function defineMySqlConfig<EM extends SqlEntityManager<MySqlDrive
8
8
  /**
9
9
  * @inheritDoc
10
10
  */
11
- export declare class MySqlMikroORM<EM extends SqlEntityManager<MySqlDriver> = SqlEntityManager<MySqlDriver>, Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> extends MikroORM<MySqlDriver, EM, Entities> {
11
+ export declare class MySqlMikroORM<EM extends SqlEntityManager<MySqlDriver> = SqlEntityManager<MySqlDriver>, Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> extends SqlMikroORM<MySqlDriver, EM, Entities> {
12
12
  /**
13
13
  * @inheritDoc
14
14
  */
package/MySqlMikroORM.js CHANGED
@@ -1,4 +1,5 @@
1
- import { defineConfig, MikroORM, } from '@mikro-orm/core';
1
+ import { defineConfig, } from '@mikro-orm/core';
2
+ import { SqlMikroORM } from '@mikro-orm/sql';
2
3
  import { MySqlDriver } from './MySqlDriver.js';
3
4
  /** Creates a type-safe configuration object for the MySQL driver. */
4
5
  export function defineMySqlConfig(options) {
@@ -7,7 +8,7 @@ export function defineMySqlConfig(options) {
7
8
  /**
8
9
  * @inheritDoc
9
10
  */
10
- export class MySqlMikroORM extends MikroORM {
11
+ export class MySqlMikroORM extends SqlMikroORM {
11
12
  /**
12
13
  * @inheritDoc
13
14
  */
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  <a href="https://mikro-orm.io"><img src="https://raw.githubusercontent.com/mikro-orm/mikro-orm/master/docs/static/img/logo-readme.svg?sanitize=true" alt="MikroORM" /></a>
3
3
  </h1>
4
4
 
5
- TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL, SQLite (including libSQL), MSSQL and Oracle databases.
5
+ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL (including CockroachDB and PGlite), SQLite (including libSQL), MSSQL and Oracle databases.
6
6
 
7
7
  > Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [Hibernate](https://hibernate.org/).
8
8
 
@@ -19,6 +19,7 @@ Install a driver package for your database:
19
19
 
20
20
  ```sh
21
21
  npm install @mikro-orm/postgresql # PostgreSQL
22
+ npm install @mikro-orm/pglite # PGlite (embedded PostgreSQL in WASM)
22
23
  npm install @mikro-orm/mysql # MySQL
23
24
  npm install @mikro-orm/mariadb # MariaDB
24
25
  npm install @mikro-orm/sqlite # SQLite
package/index.d.ts CHANGED
@@ -3,3 +3,7 @@ export * from './MySqlDriver.js';
3
3
  export * from './MySqlPlatform.js';
4
4
  export * from './MySqlConnection.js';
5
5
  export { MySqlMikroORM as MikroORM, type MySqlOptions as Options, defineMySqlConfig as defineConfig, } from './MySqlMikroORM.js';
6
+ import { type AbstractSqlDriver, SqlEntityManager } from '@mikro-orm/sql';
7
+ import type { MySqlDriver } from './MySqlDriver.js';
8
+ export type EntityManager<Driver extends AbstractSqlDriver = MySqlDriver> = SqlEntityManager<Driver>;
9
+ export declare const EntityManager: typeof SqlEntityManager;
package/index.js CHANGED
@@ -3,3 +3,5 @@ export * from './MySqlDriver.js';
3
3
  export * from './MySqlPlatform.js';
4
4
  export * from './MySqlConnection.js';
5
5
  export { MySqlMikroORM as MikroORM, defineMySqlConfig as defineConfig, } from './MySqlMikroORM.js';
6
+ import { SqlEntityManager } from '@mikro-orm/sql';
7
+ export const EntityManager = SqlEntityManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mysql",
3
- "version": "7.1.0-dev.8",
3
+ "version": "7.1.0",
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,16 +47,16 @@
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
49
  "dependencies": {
50
- "@mikro-orm/sql": "7.1.0-dev.8",
51
- "kysely": "0.28.16",
52
- "mysql2": "3.22.1",
50
+ "@mikro-orm/sql": "7.1.0",
51
+ "kysely": "0.29.2",
52
+ "mysql2": "3.22.3",
53
53
  "sqlstring": "2.3.3"
54
54
  },
55
55
  "devDependencies": {
56
- "@mikro-orm/core": "^7.0.11"
56
+ "@mikro-orm/core": "^7.1.0"
57
57
  },
58
58
  "peerDependencies": {
59
- "@mikro-orm/core": "7.1.0-dev.8"
59
+ "@mikro-orm/core": "7.1.0"
60
60
  },
61
61
  "engines": {
62
62
  "node": ">= 22.17.0"