@mikro-orm/mysql 7.3.0-dev.2 → 7.3.0-dev.20

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.
@@ -6,6 +6,12 @@ import { AbstractSqlConnection, type TransactionEventBroadcaster } from '@mikro-
6
6
  export declare class MySqlConnection extends AbstractSqlConnection {
7
7
  #private;
8
8
  createKyselyDialect(overrides: PoolOptions): Promise<MysqlDialect>;
9
+ /**
10
+ * `mysql2` uses the `timezone` option only to convert `Date` values on the client, so the session
11
+ * time zone (and with it `now()` or `current_timestamp` defaults) would still follow the server.
12
+ * Pin it on every new connection, before the user-provided `onCreateConnection` hook runs.
13
+ */
14
+ private getOnCreateConnection;
9
15
  /** Returns the `mysql2` connection pool backing this connection. */
10
16
  getNativeClient(): Promise<Pool>;
11
17
  mapOptions(overrides: PoolOptions): PoolOptions;
@@ -1,4 +1,4 @@
1
- import { MysqlDialect, } from 'kysely';
1
+ import { CompiledQuery, MysqlDialect, } from 'kysely';
2
2
  import { createConnection, createPool } from 'mysql2';
3
3
  import { Utils, AbstractSqlConnection, } from '@mikro-orm/sql';
4
4
  /** MySQL database connection using the `mysql2` driver. */
@@ -11,6 +11,7 @@ export class MySqlConnection extends AbstractSqlConnection {
11
11
  async createKyselyDialect(overrides) {
12
12
  const options = this.mapOptions(overrides);
13
13
  const password = options.password;
14
+ const onCreateConnection = this.getOnCreateConnection(options.timezone);
14
15
  if (typeof password === 'function') {
15
16
  const initialPassword = await password();
16
17
  const innerPool = createPool({ ...options, password: initialPassword });
@@ -41,7 +42,7 @@ export class MySqlConnection extends AbstractSqlConnection {
41
42
  return new MysqlDialect({
42
43
  pool,
43
44
  controlConnection: MySqlConnection.#controlConnection,
44
- onCreateConnection: this.options.onCreateConnection ?? this.config.get('onCreateConnection'),
45
+ onCreateConnection,
45
46
  onReserveConnection: this.options.onReserveConnection ?? this.config.get('onReserveConnection'),
46
47
  });
47
48
  }
@@ -49,10 +50,26 @@ export class MySqlConnection extends AbstractSqlConnection {
49
50
  return new MysqlDialect({
50
51
  pool: this.#pool,
51
52
  controlConnection: MySqlConnection.#controlConnection,
52
- onCreateConnection: this.options.onCreateConnection ?? this.config.get('onCreateConnection'),
53
+ onCreateConnection,
53
54
  onReserveConnection: this.options.onReserveConnection ?? this.config.get('onReserveConnection'),
54
55
  });
55
56
  }
57
+ /**
58
+ * `mysql2` uses the `timezone` option only to convert `Date` values on the client, so the session
59
+ * time zone (and with it `now()` or `current_timestamp` defaults) would still follow the server.
60
+ * Pin it on every new connection, before the user-provided `onCreateConnection` hook runs.
61
+ */
62
+ getOnCreateConnection(timezone) {
63
+ const hook = this.options.onCreateConnection ?? this.config.get('onCreateConnection');
64
+ const sessionTimezone = timezone === 'Z' ? '+00:00' : timezone;
65
+ if (!sessionTimezone || sessionTimezone === 'local') {
66
+ return hook;
67
+ }
68
+ return async (connection) => {
69
+ await connection.executeQuery(CompiledQuery.raw('set time_zone = ?', [sessionTimezone]));
70
+ await hook?.(connection);
71
+ };
72
+ }
56
73
  /** Returns the `mysql2` connection pool backing this connection. */
57
74
  async getNativeClient() {
58
75
  await this.ensureConnection();
@@ -138,6 +155,9 @@ export class MySqlConnection extends AbstractSqlConnection {
138
155
  }
139
156
  async commit(ctx, eventBroadcaster) {
140
157
  if (!ctx.isRolledBack && 'savepointName' in ctx) {
158
+ if ('failedSavepointRollback' in ctx) {
159
+ throw ctx.failedSavepointRollback;
160
+ }
141
161
  try {
142
162
  await ctx.releaseSavepoint(ctx.savepointName).execute();
143
163
  }
package/MySqlDriver.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Configuration, Constructor, EntityDictionary, EntityName, FilterQuery, NativeInsertUpdateManyOptions, QueryResult, UpsertManyOptions } from '@mikro-orm/core';
1
+ import { type Configuration, type Constructor, type EntityDictionary, type EntityName, type FilterQuery, type NativeInsertUpdateManyOptions, type QueryResult, type UpsertManyOptions } from '@mikro-orm/core';
2
2
  import { AbstractSqlDriver } from '@mikro-orm/sql';
3
3
  import { MySqlConnection } from './MySqlConnection.js';
4
4
  import { MySqlMikroORM } from './MySqlMikroORM.js';
package/MySqlDriver.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { isRaw, } from '@mikro-orm/core';
1
2
  import { AbstractSqlDriver, Utils } from '@mikro-orm/sql';
2
3
  import { MySqlConnection } from './MySqlConnection.js';
3
4
  import { MySqlMikroORM } from './MySqlMikroORM.js';
@@ -19,13 +20,29 @@ export class MySqlDriver extends AbstractSqlDriver {
19
20
  }
20
21
  async nativeInsertMany(entityName, data, options = {}) {
21
22
  options.processCollections ??= true;
22
- const res = await super.nativeInsertMany(entityName, data, options);
23
23
  const meta = this.metadata.get(entityName);
24
+ const pk = meta.primaryKeys[0];
25
+ // mysql continues auto_increment from an explicit id, so a generated row after one starts a new insert
26
+ const split = data.findIndex((item, idx) => idx > 0 && item[pk] == null && data[idx - 1][pk] != null);
27
+ if (split > 0) {
28
+ const res = await this.nativeInsertMany(entityName, data.slice(0, split), options);
29
+ const rest = await this.nativeInsertMany(entityName, data.slice(split), options);
30
+ res.rows.push(...rest.rows);
31
+ res.affectedRows += rest.affectedRows;
32
+ res.insertedIds = res.insertedIds && rest.insertedIds ? [...res.insertedIds, ...rest.insertedIds] : undefined;
33
+ return res;
34
+ }
35
+ const res = await super.nativeInsertMany(entityName, data, options);
24
36
  const pks = this.getPrimaryKeyFields(meta);
25
37
  const ctx = options.ctx;
26
38
  const autoIncrementIncrement = await this.getAutoIncrementIncrement(ctx);
27
- data.forEach((item, idx) => (res.rows[idx] = { [pks[0]]: item[pks[0]] ?? res.insertId + idx * autoIncrementIncrement }));
39
+ data.forEach((item, idx) => (res.rows[idx] = { [pks[0]]: item[pk] ?? res.insertId + idx * autoIncrementIncrement }));
28
40
  res.row = res.rows[0];
41
+ const ids = res.rows.map(row => row[pks[0]]);
42
+ // without `returning` the value of a raw pk expression is never known
43
+ if (pks.length === 1 && !ids.some(isRaw)) {
44
+ res.insertedIds = ids;
45
+ }
29
46
  return res;
30
47
  }
31
48
  async nativeUpdateMany(entityName, where, data, options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mysql",
3
- "version": "7.3.0-dev.2",
3
+ "version": "7.3.0-dev.20",
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.3.0-dev.2",
51
- "kysely": "0.29.6",
52
- "mysql2": "3.24.4",
50
+ "@mikro-orm/sql": "7.3.0-dev.20",
51
+ "kysely": "0.29.8",
52
+ "mysql2": "3.24.6",
53
53
  "sqlstring": "2.3.3"
54
54
  },
55
55
  "devDependencies": {
56
- "@mikro-orm/core": "^7.2.1"
56
+ "@mikro-orm/core": "^7.2.7"
57
57
  },
58
58
  "peerDependencies": {
59
- "@mikro-orm/core": "7.3.0-dev.2"
59
+ "@mikro-orm/core": "7.3.0-dev.20"
60
60
  },
61
61
  "engines": {
62
62
  "node": ">= 22.17.0"