@mikro-orm/mysql 7.0.14 → 7.0.15-dev.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.
@@ -3,7 +3,7 @@ import { type PoolOptions } from 'mysql2';
3
3
  import { AbstractSqlConnection, type TransactionEventBroadcaster } from '@mikro-orm/sql';
4
4
  /** MySQL database connection using the `mysql2` driver. */
5
5
  export declare class MySqlConnection extends AbstractSqlConnection {
6
- createKyselyDialect(overrides: PoolOptions): Promise<MysqlDialect>;
7
- mapOptions(overrides: PoolOptions): PoolOptions;
8
- commit(ctx: ControlledTransaction<any, any>, eventBroadcaster?: TransactionEventBroadcaster): Promise<void>;
6
+ createKyselyDialect(overrides: PoolOptions): Promise<MysqlDialect>;
7
+ mapOptions(overrides: PoolOptions): PoolOptions;
8
+ commit(ctx: ControlledTransaction<any, any>, eventBroadcaster?: TransactionEventBroadcaster): Promise<void>;
9
9
  }
@@ -1,77 +1,78 @@
1
1
  import { MysqlDialect } from 'kysely';
2
2
  import { createPool } from 'mysql2';
3
- import { Utils, AbstractSqlConnection } from '@mikro-orm/sql';
3
+ import { Utils, AbstractSqlConnection, } from '@mikro-orm/sql';
4
4
  /** MySQL database connection using the `mysql2` driver. */
5
5
  export class MySqlConnection extends AbstractSqlConnection {
6
- async createKyselyDialect(overrides) {
7
- const options = this.mapOptions(overrides);
8
- const password = options.password;
9
- if (typeof password === 'function') {
10
- const initialPassword = await password();
11
- const innerPool = createPool({ ...options, password: initialPassword });
12
- // mysql2 reads pool.config.connectionConfig.password when creating new physical
13
- // connections, so updating it before getConnection() ensures fresh tokens are used.
14
- // Existing idle connections are already authenticated and unaffected, so we skip
15
- // the callback when the pool has a free connection to reuse.
16
- const pool = {
17
- getConnection(cb) {
18
- const inner = innerPool;
19
- if ((inner._freeConnections?.length ?? 0) > 0) {
20
- innerPool.getConnection(cb);
21
- return;
22
- }
23
- Promise.resolve(password())
24
- .then(pw => {
25
- inner.config.connectionConfig.password = pw;
26
- innerPool.getConnection(cb);
27
- })
28
- .catch(err => cb(err, undefined));
29
- },
30
- end(cb) {
31
- innerPool.end(cb);
32
- },
33
- };
34
- return new MysqlDialect({
35
- pool,
36
- onCreateConnection: this.options.onCreateConnection ?? this.config.get('onCreateConnection'),
37
- });
38
- }
39
- return new MysqlDialect({
40
- pool: createPool(options),
41
- onCreateConnection: this.options.onCreateConnection ?? this.config.get('onCreateConnection'),
42
- });
43
- }
44
- mapOptions(overrides) {
45
- const ret = { ...this.getConnectionOptions() };
46
- const pool = this.config.get('pool');
47
- ret.connectionLimit = pool?.max;
48
- ret.idleTimeout = pool?.idleTimeoutMillis;
49
- if (this.config.get('multipleStatements')) {
50
- ret.multipleStatements = this.config.get('multipleStatements');
51
- }
52
- if (this.config.get('forceUtcTimezone')) {
53
- ret.timezone = 'Z';
6
+ async createKyselyDialect(overrides) {
7
+ const options = this.mapOptions(overrides);
8
+ const password = options.password;
9
+ if (typeof password === 'function') {
10
+ const initialPassword = await password();
11
+ const innerPool = createPool({ ...options, password: initialPassword });
12
+ // mysql2 reads pool.config.connectionConfig.password when creating new physical
13
+ // connections, so updating it before getConnection() ensures fresh tokens are used.
14
+ // Existing idle connections are already authenticated and unaffected, so we skip
15
+ // the callback when the pool has a free connection to reuse.
16
+ const pool = {
17
+ getConnection(cb) {
18
+ const inner = innerPool;
19
+ if ((inner._freeConnections?.length ?? 0) > 0) {
20
+ innerPool.getConnection(cb);
21
+ return;
22
+ }
23
+ Promise.resolve(password())
24
+ .then(pw => {
25
+ inner.config.connectionConfig.password = pw;
26
+ innerPool.getConnection(cb);
27
+ })
28
+ .catch(err => cb(err, undefined));
29
+ },
30
+ end(cb) {
31
+ innerPool.end(cb);
32
+ },
33
+ };
34
+ return new MysqlDialect({
35
+ pool,
36
+ onCreateConnection: this.options.onCreateConnection ?? this.config.get('onCreateConnection'),
37
+ });
38
+ }
39
+ return new MysqlDialect({
40
+ pool: createPool(options),
41
+ onCreateConnection: this.options.onCreateConnection ?? this.config.get('onCreateConnection'),
42
+ });
54
43
  }
55
- if (this.config.get('timezone')) {
56
- ret.timezone = this.config.get('timezone');
44
+ mapOptions(overrides) {
45
+ const ret = { ...this.getConnectionOptions() };
46
+ const pool = this.config.get('pool');
47
+ ret.connectionLimit = pool?.max;
48
+ ret.idleTimeout = pool?.idleTimeoutMillis;
49
+ if (this.config.get('multipleStatements')) {
50
+ ret.multipleStatements = this.config.get('multipleStatements');
51
+ }
52
+ if (this.config.get('forceUtcTimezone')) {
53
+ ret.timezone = 'Z';
54
+ }
55
+ if (this.config.get('timezone')) {
56
+ ret.timezone = this.config.get('timezone');
57
+ }
58
+ ret.supportBigNumbers = true;
59
+ ret.dateStrings = true;
60
+ return Utils.mergeConfig(ret, overrides);
57
61
  }
58
- ret.supportBigNumbers = true;
59
- ret.dateStrings = true;
60
- return Utils.mergeConfig(ret, overrides);
61
- }
62
- async commit(ctx, eventBroadcaster) {
63
- if (!ctx.isRolledBack && 'savepointName' in ctx) {
64
- try {
65
- await ctx.releaseSavepoint(ctx.savepointName).execute();
66
- } catch (e) {
67
- /* v8 ignore next */
68
- // https://github.com/knex/knex/issues/805
69
- if (e.errno !== 1305) {
70
- throw e;
62
+ async commit(ctx, eventBroadcaster) {
63
+ if (!ctx.isRolledBack && 'savepointName' in ctx) {
64
+ try {
65
+ await ctx.releaseSavepoint(ctx.savepointName).execute();
66
+ }
67
+ catch (e) {
68
+ /* v8 ignore next */
69
+ // https://github.com/knex/knex/issues/805
70
+ if (e.errno !== 1305) {
71
+ throw e;
72
+ }
73
+ }
74
+ return this.logQuery(this.platform.getReleaseSavepointSQL(ctx.savepointName));
71
75
  }
72
- }
73
- return this.logQuery(this.platform.getReleaseSavepointSQL(ctx.savepointName));
76
+ await super.commit(ctx, eventBroadcaster);
74
77
  }
75
- await super.commit(ctx, eventBroadcaster);
76
- }
77
78
  }
package/MySqlDriver.d.ts CHANGED
@@ -1,33 +1,15 @@
1
- import type {
2
- Configuration,
3
- Constructor,
4
- EntityDictionary,
5
- EntityName,
6
- FilterQuery,
7
- NativeInsertUpdateManyOptions,
8
- QueryResult,
9
- UpsertManyOptions,
10
- } from '@mikro-orm/core';
1
+ import type { Configuration, Constructor, EntityDictionary, EntityName, FilterQuery, NativeInsertUpdateManyOptions, QueryResult, UpsertManyOptions } from '@mikro-orm/core';
11
2
  import { AbstractSqlDriver } from '@mikro-orm/sql';
12
3
  import { MySqlConnection } from './MySqlConnection.js';
13
4
  import { MySqlMikroORM } from './MySqlMikroORM.js';
14
5
  import { MySqlPlatform } from './MySqlPlatform.js';
15
6
  /** Database driver for MySQL. */
16
7
  export declare class MySqlDriver extends AbstractSqlDriver<MySqlConnection, MySqlPlatform> {
17
- private autoIncrementIncrement?;
18
- constructor(config: Configuration);
19
- private getAutoIncrementIncrement;
20
- nativeInsertMany<T extends object>(
21
- entityName: EntityName<T>,
22
- data: EntityDictionary<T>[],
23
- options?: NativeInsertUpdateManyOptions<T>,
24
- ): Promise<QueryResult<T>>;
25
- nativeUpdateMany<T extends object>(
26
- entityName: EntityName<T>,
27
- where: FilterQuery<T>[],
28
- data: EntityDictionary<T>[],
29
- options?: NativeInsertUpdateManyOptions<T> & UpsertManyOptions<T>,
30
- ): Promise<QueryResult<T>>;
31
- /** @inheritDoc */
32
- getORMClass(): Constructor<MySqlMikroORM>;
8
+ private autoIncrementIncrement?;
9
+ constructor(config: Configuration);
10
+ private getAutoIncrementIncrement;
11
+ nativeInsertMany<T extends object>(entityName: EntityName<T>, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
12
+ nativeUpdateMany<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T> & UpsertManyOptions<T>): Promise<QueryResult<T>>;
13
+ /** @inheritDoc */
14
+ getORMClass(): Constructor<MySqlMikroORM>;
33
15
  }
package/MySqlDriver.js CHANGED
@@ -4,58 +4,54 @@ import { MySqlMikroORM } from './MySqlMikroORM.js';
4
4
  import { MySqlPlatform } from './MySqlPlatform.js';
5
5
  /** Database driver for MySQL. */
6
6
  export class MySqlDriver extends AbstractSqlDriver {
7
- autoIncrementIncrement;
8
- constructor(config) {
9
- super(config, new MySqlPlatform(), MySqlConnection, ['kysely', 'mysql2']);
10
- }
11
- async getAutoIncrementIncrement(ctx) {
12
- if (this.autoIncrementIncrement == null) {
13
- // the increment step may differ when running a cluster, see https://github.com/mikro-orm/mikro-orm/issues/3828
14
- const res = await this.connection.execute(`show variables like 'auto_increment_increment'`, [], 'get', ctx, {
15
- enabled: false,
16
- });
17
- /* v8 ignore next */
18
- this.autoIncrementIncrement = res?.Value ? +res?.Value : 1;
7
+ autoIncrementIncrement;
8
+ constructor(config) {
9
+ super(config, new MySqlPlatform(), MySqlConnection, ['kysely', 'mysql2']);
19
10
  }
20
- return this.autoIncrementIncrement;
21
- }
22
- async nativeInsertMany(entityName, data, options = {}) {
23
- options.processCollections ??= true;
24
- const res = await super.nativeInsertMany(entityName, data, options);
25
- const meta = this.metadata.get(entityName);
26
- const pks = this.getPrimaryKeyFields(meta);
27
- const ctx = options.ctx;
28
- const autoIncrementIncrement = await this.getAutoIncrementIncrement(ctx);
29
- data.forEach(
30
- (item, idx) => (res.rows[idx] = { [pks[0]]: item[pks[0]] ?? res.insertId + idx * autoIncrementIncrement }),
31
- );
32
- res.row = res.rows[0];
33
- return res;
34
- }
35
- async nativeUpdateMany(entityName, where, data, options = {}) {
36
- const res = await super.nativeUpdateMany(entityName, where, data, options);
37
- const meta = this.metadata.get(entityName);
38
- const pks = this.getPrimaryKeyFields(meta);
39
- const ctx = options.ctx;
40
- const autoIncrementIncrement = await this.getAutoIncrementIncrement(ctx);
41
- let i = 0;
42
- const rows = where.map(cond => {
43
- if (res.insertId != null && Utils.isEmpty(cond)) {
44
- return { [pks[0]]: res.insertId + i++ * autoIncrementIncrement };
45
- }
46
- if (cond[pks[0]] == null) {
47
- return undefined;
48
- }
49
- return { [pks[0]]: cond[pks[0]] };
50
- });
51
- if (rows.every(i => i !== undefined)) {
52
- res.rows = rows;
11
+ async getAutoIncrementIncrement(ctx) {
12
+ if (this.autoIncrementIncrement == null) {
13
+ // the increment step may differ when running a cluster, see https://github.com/mikro-orm/mikro-orm/issues/3828
14
+ const res = await this.connection.execute(`show variables like 'auto_increment_increment'`, [], 'get', ctx, { enabled: false });
15
+ /* v8 ignore next */
16
+ this.autoIncrementIncrement = res?.Value ? +res?.Value : 1;
17
+ }
18
+ return this.autoIncrementIncrement;
19
+ }
20
+ async nativeInsertMany(entityName, data, options = {}) {
21
+ options.processCollections ??= true;
22
+ const res = await super.nativeInsertMany(entityName, data, options);
23
+ const meta = this.metadata.get(entityName);
24
+ const pks = this.getPrimaryKeyFields(meta);
25
+ const ctx = options.ctx;
26
+ const autoIncrementIncrement = await this.getAutoIncrementIncrement(ctx);
27
+ data.forEach((item, idx) => (res.rows[idx] = { [pks[0]]: item[pks[0]] ?? res.insertId + idx * autoIncrementIncrement }));
28
+ res.row = res.rows[0];
29
+ return res;
30
+ }
31
+ async nativeUpdateMany(entityName, where, data, options = {}) {
32
+ const res = await super.nativeUpdateMany(entityName, where, data, options);
33
+ const meta = this.metadata.get(entityName);
34
+ const pks = this.getPrimaryKeyFields(meta);
35
+ const ctx = options.ctx;
36
+ const autoIncrementIncrement = await this.getAutoIncrementIncrement(ctx);
37
+ let i = 0;
38
+ const rows = where.map(cond => {
39
+ if (res.insertId != null && Utils.isEmpty(cond)) {
40
+ return { [pks[0]]: res.insertId + i++ * autoIncrementIncrement };
41
+ }
42
+ if (cond[pks[0]] == null) {
43
+ return undefined;
44
+ }
45
+ return { [pks[0]]: cond[pks[0]] };
46
+ });
47
+ if (rows.every(i => i !== undefined)) {
48
+ res.rows = rows;
49
+ }
50
+ res.row = res.rows[0];
51
+ return res;
52
+ }
53
+ /** @inheritDoc */
54
+ getORMClass() {
55
+ return MySqlMikroORM;
53
56
  }
54
- res.row = res.rows[0];
55
- return res;
56
- }
57
- /** @inheritDoc */
58
- getORMClass() {
59
- return MySqlMikroORM;
60
- }
61
57
  }
@@ -1,58 +1,20 @@
1
- import {
2
- type AnyEntity,
3
- type EntityClass,
4
- type EntitySchema,
5
- type MikroORM,
6
- type Options,
7
- type IDatabaseDriver,
8
- type EntityManager,
9
- type EntityManagerType,
10
- } from '@mikro-orm/core';
1
+ import { type AnyEntity, type EntityClass, type EntitySchema, type MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from '@mikro-orm/core';
11
2
  import { SqlMikroORM, type SqlEntityManager } from '@mikro-orm/sql';
12
3
  import { MySqlDriver } from './MySqlDriver.js';
13
4
  /** Configuration options for the MySQL driver. */
14
- export type MySqlOptions<
15
- EM extends SqlEntityManager<MySqlDriver> = SqlEntityManager<MySqlDriver>,
16
- Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (
17
- | string
18
- | EntityClass<AnyEntity>
19
- | EntitySchema
20
- )[],
21
- > = Partial<Options<MySqlDriver, EM, Entities>>;
5
+ export type MySqlOptions<EM extends SqlEntityManager<MySqlDriver> = SqlEntityManager<MySqlDriver>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> = Partial<Options<MySqlDriver, EM, Entities>>;
22
6
  /** Creates a type-safe configuration object for the MySQL driver. */
23
- export declare function defineMySqlConfig<
24
- EM extends SqlEntityManager<MySqlDriver> = SqlEntityManager<MySqlDriver>,
25
- Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (
26
- | string
27
- | EntityClass<AnyEntity>
28
- | EntitySchema
29
- )[],
30
- >(options: Partial<Options<MySqlDriver, EM, Entities>>): Partial<Options<MySqlDriver, EM, Entities>>;
7
+ export declare function defineMySqlConfig<EM extends SqlEntityManager<MySqlDriver> = SqlEntityManager<MySqlDriver>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: Partial<Options<MySqlDriver, EM, Entities>>): Partial<Options<MySqlDriver, EM, Entities>>;
31
8
  /**
32
9
  * @inheritDoc
33
10
  */
34
- export declare class MySqlMikroORM<
35
- EM extends SqlEntityManager<MySqlDriver> = SqlEntityManager<MySqlDriver>,
36
- Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (
37
- | string
38
- | EntityClass<AnyEntity>
39
- | EntitySchema
40
- )[],
41
- > extends SqlMikroORM<MySqlDriver, EM, Entities> {
42
- /**
43
- * @inheritDoc
44
- */
45
- static init<
46
- D extends IDatabaseDriver = MySqlDriver,
47
- EM extends EntityManager<D> = D[typeof EntityManagerType] & EntityManager<D>,
48
- Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (
49
- | string
50
- | EntityClass<AnyEntity>
51
- | EntitySchema
52
- )[],
53
- >(options: Partial<Options<D, EM, Entities>>): Promise<MikroORM<D, EM, Entities>>;
54
- /**
55
- * @inheritDoc
56
- */
57
- constructor(options: Partial<Options<MySqlDriver, EM, Entities>>);
11
+ export declare class MySqlMikroORM<EM extends SqlEntityManager<MySqlDriver> = SqlEntityManager<MySqlDriver>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> extends SqlMikroORM<MySqlDriver, EM, Entities> {
12
+ /**
13
+ * @inheritDoc
14
+ */
15
+ static init<D extends IDatabaseDriver = MySqlDriver, EM extends EntityManager<D> = D[typeof EntityManagerType] & EntityManager<D>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: Partial<Options<D, EM, Entities>>): Promise<MikroORM<D, EM, Entities>>;
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ constructor(options: Partial<Options<MySqlDriver, EM, Entities>>);
58
20
  }
package/MySqlMikroORM.js CHANGED
@@ -1,24 +1,24 @@
1
- import { defineConfig } from '@mikro-orm/core';
1
+ import { defineConfig, } from '@mikro-orm/core';
2
2
  import { SqlMikroORM } from '@mikro-orm/sql';
3
3
  import { MySqlDriver } from './MySqlDriver.js';
4
4
  /** Creates a type-safe configuration object for the MySQL driver. */
5
5
  export function defineMySqlConfig(options) {
6
- return defineConfig({ driver: MySqlDriver, ...options });
6
+ return defineConfig({ driver: MySqlDriver, ...options });
7
7
  }
8
8
  /**
9
9
  * @inheritDoc
10
10
  */
11
11
  export class MySqlMikroORM extends SqlMikroORM {
12
- /**
13
- * @inheritDoc
14
- */
15
- static async init(options) {
16
- return super.init(defineMySqlConfig(options));
17
- }
18
- /**
19
- * @inheritDoc
20
- */
21
- constructor(options) {
22
- super(defineMySqlConfig(options));
23
- }
12
+ /**
13
+ * @inheritDoc
14
+ */
15
+ static async init(options) {
16
+ return super.init(defineMySqlConfig(options));
17
+ }
18
+ /**
19
+ * @inheritDoc
20
+ */
21
+ constructor(options) {
22
+ super(defineMySqlConfig(options));
23
+ }
24
24
  }
@@ -1,5 +1,5 @@
1
1
  import { BaseMySqlPlatform } from '@mikro-orm/sql';
2
2
  /** Platform implementation for MySQL. */
3
3
  export declare class MySqlPlatform extends BaseMySqlPlatform {
4
- escape(value: any): string;
4
+ escape(value: any): string;
5
5
  }
package/MySqlPlatform.js CHANGED
@@ -2,7 +2,7 @@ import SqlString from 'sqlstring';
2
2
  import { BaseMySqlPlatform } from '@mikro-orm/sql';
3
3
  /** Platform implementation for MySQL. */
4
4
  export class MySqlPlatform extends BaseMySqlPlatform {
5
- escape(value) {
6
- return SqlString.escape(value, true, this.timezone);
7
- }
5
+ escape(value) {
6
+ return SqlString.escape(value, true, this.timezone);
7
+ }
8
8
  }
package/README.md CHANGED
@@ -133,7 +133,7 @@ const author = await em.findOneOrFail(Author, 1, {
133
133
  populate: ['books'],
134
134
  });
135
135
  author.name = 'Jon Snow II';
136
- author.books.getItems().forEach(book => (book.title += ' (2nd ed.)'));
136
+ author.books.getItems().forEach(book => book.title += ' (2nd ed.)');
137
137
  author.books.add(orm.em.create(Book, { title: 'New Book', author }));
138
138
 
139
139
  // Flush computes change sets and executes them in a single transaction
package/index.d.ts CHANGED
@@ -2,8 +2,4 @@ export * from '@mikro-orm/sql';
2
2
  export * from './MySqlDriver.js';
3
3
  export * from './MySqlPlatform.js';
4
4
  export * from './MySqlConnection.js';
5
- export {
6
- MySqlMikroORM as MikroORM,
7
- type MySqlOptions as Options,
8
- defineMySqlConfig as defineConfig,
9
- } from './MySqlMikroORM.js';
5
+ export { MySqlMikroORM as MikroORM, type MySqlOptions as Options, defineMySqlConfig as defineConfig, } from './MySqlMikroORM.js';
package/index.js CHANGED
@@ -2,4 +2,4 @@ export * from '@mikro-orm/sql';
2
2
  export * from './MySqlDriver.js';
3
3
  export * from './MySqlPlatform.js';
4
4
  export * from './MySqlConnection.js';
5
- export { MySqlMikroORM as MikroORM, defineMySqlConfig as defineConfig } from './MySqlMikroORM.js';
5
+ export { MySqlMikroORM as MikroORM, defineMySqlConfig as defineConfig, } from './MySqlMikroORM.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mysql",
3
- "version": "7.0.14",
3
+ "version": "7.0.15-dev.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,7 +47,7 @@
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
49
  "dependencies": {
50
- "@mikro-orm/sql": "7.0.14",
50
+ "@mikro-orm/sql": "7.0.15-dev.0",
51
51
  "kysely": "0.28.17",
52
52
  "mysql2": "3.22.3",
53
53
  "sqlstring": "2.3.3"
@@ -56,7 +56,7 @@
56
56
  "@mikro-orm/core": "^7.0.14"
57
57
  },
58
58
  "peerDependencies": {
59
- "@mikro-orm/core": "7.0.14"
59
+ "@mikro-orm/core": "7.0.15-dev.0"
60
60
  },
61
61
  "engines": {
62
62
  "node": ">= 22.17.0"