@mikro-orm/sql 7.0.0-dev.98 → 7.0.0-dev.99

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,14 +1,15 @@
1
1
  import { type ControlledTransaction, type Dialect, Kysely } from 'kysely';
2
- import { type AnyEntity, Connection, type Dictionary, type EntityData, type IsolationLevel, type LogContext, type LoggingOptions, type MaybePromise, type QueryResult, RawQueryFragment, type Transaction, type TransactionEventBroadcaster } from '@mikro-orm/core';
2
+ import { type AnyEntity, Connection, type Dictionary, type EntityData, type IsolationLevel, type LogContext, type LoggingOptions, type QueryResult, RawQueryFragment, type Transaction, type TransactionEventBroadcaster } from '@mikro-orm/core';
3
3
  import type { AbstractSqlPlatform } from './AbstractSqlPlatform.js';
4
4
  import { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
5
5
  export declare abstract class AbstractSqlConnection extends Connection {
6
+ #private;
6
7
  protected platform: AbstractSqlPlatform;
7
- protected client: Kysely<any>;
8
- abstract createKyselyDialect(overrides: Dictionary): MaybePromise<Dialect>;
8
+ abstract createKyselyDialect(overrides: Dictionary): Dialect;
9
9
  connect(options?: {
10
10
  skipOnConnect?: boolean;
11
11
  }): Promise<void>;
12
+ createKysely(): void;
12
13
  /**
13
14
  * @inheritDoc
14
15
  */
@@ -2,37 +2,41 @@ import { CompiledQuery, Kysely } from 'kysely';
2
2
  import { Connection, EventType, RawQueryFragment, Utils, } from '@mikro-orm/core';
3
3
  import { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
4
4
  export class AbstractSqlConnection extends Connection {
5
- client;
5
+ #client;
6
6
  async connect(options) {
7
+ this.getClient();
8
+ this.connected = true;
9
+ if (options?.skipOnConnect !== true) {
10
+ await this.onConnect();
11
+ }
12
+ }
13
+ createKysely() {
7
14
  let driverOptions = this.options.driverOptions ?? this.config.get('driverOptions');
8
15
  if (typeof driverOptions === 'function') {
9
- driverOptions = await driverOptions();
16
+ driverOptions = driverOptions();
10
17
  }
11
18
  if (driverOptions instanceof Kysely) {
12
19
  this.logger.log('info', 'Reusing Kysely client provided via `driverOptions`');
13
- this.client = driverOptions;
20
+ this.#client = driverOptions;
14
21
  }
15
22
  else if ('createDriver' in driverOptions) {
16
23
  this.logger.log('info', 'Reusing Kysely dialect provided via `driverOptions`');
17
- this.client = new Kysely({ dialect: driverOptions });
24
+ this.#client = new Kysely({ dialect: driverOptions });
18
25
  }
19
26
  else {
20
- this.client = new Kysely({
21
- dialect: await this.createKyselyDialect(driverOptions),
27
+ this.#client = new Kysely({
28
+ dialect: this.createKyselyDialect(driverOptions),
22
29
  });
23
30
  }
24
- this.connected = true;
25
- if (options?.skipOnConnect !== true) {
26
- await this.onConnect();
27
- }
28
31
  }
29
32
  /**
30
33
  * @inheritDoc
31
34
  */
32
35
  async close(force) {
33
36
  await super.close(force);
34
- await this.client?.destroy();
37
+ await this.#client?.destroy();
35
38
  this.connected = false;
39
+ this.#client = undefined;
36
40
  }
37
41
  /**
38
42
  * @inheritDoc
@@ -49,7 +53,7 @@ export class AbstractSqlConnection extends Connection {
49
53
  return { ok: false, reason: 'Connection not established' };
50
54
  }
51
55
  try {
52
- await this.client.executeQuery(CompiledQuery.raw('select 1'));
56
+ await this.getClient().executeQuery(CompiledQuery.raw('select 1'));
53
57
  return { ok: true };
54
58
  }
55
59
  catch (error) {
@@ -57,7 +61,10 @@ export class AbstractSqlConnection extends Connection {
57
61
  }
58
62
  }
59
63
  getClient() {
60
- return this.client;
64
+ if (!this.#client) {
65
+ this.createKysely();
66
+ }
67
+ return this.#client;
61
68
  }
62
69
  async transactional(cb, options = {}) {
63
70
  const trx = await this.begin(options);
@@ -86,7 +93,7 @@ export class AbstractSqlConnection extends Connection {
86
93
  }
87
94
  await this.ensureConnection();
88
95
  await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
89
- let trxBuilder = this.client.startTransaction();
96
+ let trxBuilder = this.getClient().startTransaction();
90
97
  if (options.isolationLevel) {
91
98
  trxBuilder = trxBuilder.setIsolationLevel(options.isolationLevel);
92
99
  }
@@ -155,7 +162,7 @@ export class AbstractSqlConnection extends Connection {
155
162
  const sql = this.getSql(q.query, q.formatted, loggerContext);
156
163
  return this.executeQuery(sql, async () => {
157
164
  const compiled = CompiledQuery.raw(q.formatted);
158
- const res = await (ctx ?? this.client).executeQuery(compiled);
165
+ const res = await (ctx ?? this.#client).executeQuery(compiled);
159
166
  return this.transformRawResult(res, method);
160
167
  }, { ...q, ...loggerContext });
161
168
  }
@@ -172,7 +179,7 @@ export class AbstractSqlConnection extends Connection {
172
179
  parameters: [],
173
180
  };
174
181
  try {
175
- const res = (ctx ?? this.client).getExecutor().stream(compiled, 1);
182
+ const res = (ctx ?? this.getClient()).getExecutor().stream(compiled, 1);
176
183
  this.logQuery(sql, {
177
184
  sql, params,
178
185
  ...loggerContext,
@@ -198,7 +205,7 @@ export class AbstractSqlConnection extends Connection {
198
205
  const buf = await readFile(path);
199
206
  try {
200
207
  const raw = CompiledQuery.raw(buf.toString());
201
- await this.client.executeQuery(raw);
208
+ await this.getClient().executeQuery(raw);
202
209
  }
203
210
  catch (e) {
204
211
  /* v8 ignore next */
@@ -3,6 +3,6 @@ import { AbstractSqlConnection } from '../../AbstractSqlConnection.js';
3
3
  export class BaseSqliteConnection extends AbstractSqlConnection {
4
4
  async connect(options) {
5
5
  await super.connect(options);
6
- await this.client.executeQuery(CompiledQuery.raw('pragma foreign_keys = on'));
6
+ await this.getClient().executeQuery(CompiledQuery.raw('pragma foreign_keys = on'));
7
7
  }
8
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.0.0-dev.98",
3
+ "version": "7.0.0-dev.99",
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
  "type": "module",
6
6
  "exports": {
@@ -57,6 +57,6 @@
57
57
  "@mikro-orm/core": "^6.6.2"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.0.0-dev.98"
60
+ "@mikro-orm/core": "7.0.0-dev.99"
61
61
  }
62
62
  }