@mikro-orm/knex 7.0.0-dev.1 → 7.0.0-dev.11

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.
Files changed (79) hide show
  1. package/AbstractSqlConnection.d.ts +2 -2
  2. package/AbstractSqlConnection.js +33 -31
  3. package/AbstractSqlDriver.d.ts +14 -12
  4. package/AbstractSqlDriver.js +198 -186
  5. package/AbstractSqlPlatform.d.ts +4 -4
  6. package/AbstractSqlPlatform.js +17 -21
  7. package/PivotCollectionPersister.d.ts +1 -1
  8. package/PivotCollectionPersister.js +5 -8
  9. package/SqlEntityManager.d.ts +4 -3
  10. package/SqlEntityManager.js +2 -6
  11. package/SqlEntityRepository.d.ts +2 -2
  12. package/SqlEntityRepository.js +2 -6
  13. package/dialects/index.d.ts +4 -4
  14. package/dialects/index.js +4 -20
  15. package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +1 -1
  16. package/dialects/mssql/MsSqlNativeQueryBuilder.js +29 -29
  17. package/dialects/mssql/index.d.ts +1 -1
  18. package/dialects/mssql/index.js +1 -17
  19. package/dialects/mysql/MySqlExceptionConverter.js +16 -19
  20. package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +1 -1
  21. package/dialects/mysql/MySqlNativeQueryBuilder.js +13 -17
  22. package/dialects/mysql/MySqlPlatform.d.ts +5 -5
  23. package/dialects/mysql/MySqlPlatform.js +16 -20
  24. package/dialects/mysql/MySqlSchemaHelper.d.ts +5 -5
  25. package/dialects/mysql/MySqlSchemaHelper.js +8 -12
  26. package/dialects/mysql/index.d.ts +4 -4
  27. package/dialects/mysql/index.js +4 -20
  28. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
  29. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +2 -6
  30. package/dialects/postgresql/index.d.ts +1 -1
  31. package/dialects/postgresql/index.js +1 -17
  32. package/dialects/sqlite/BaseSqliteConnection.d.ts +2 -2
  33. package/dialects/sqlite/BaseSqliteConnection.js +14 -12
  34. package/dialects/sqlite/BaseSqlitePlatform.d.ts +4 -5
  35. package/dialects/sqlite/BaseSqlitePlatform.js +11 -19
  36. package/dialects/sqlite/SqliteExceptionConverter.js +16 -19
  37. package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +1 -1
  38. package/dialects/sqlite/SqliteNativeQueryBuilder.js +2 -6
  39. package/dialects/sqlite/SqliteSchemaHelper.d.ts +5 -5
  40. package/dialects/sqlite/SqliteSchemaHelper.js +22 -26
  41. package/dialects/sqlite/index.d.ts +5 -5
  42. package/dialects/sqlite/index.js +5 -21
  43. package/index.d.ts +11 -11
  44. package/index.js +13 -34
  45. package/package.json +7 -16
  46. package/query/ArrayCriteriaNode.d.ts +2 -2
  47. package/query/ArrayCriteriaNode.js +2 -6
  48. package/query/CriteriaNode.d.ts +1 -1
  49. package/query/CriteriaNode.js +25 -33
  50. package/query/CriteriaNodeFactory.d.ts +1 -1
  51. package/query/CriteriaNodeFactory.js +17 -21
  52. package/query/NativeQueryBuilder.d.ts +2 -2
  53. package/query/NativeQueryBuilder.js +33 -37
  54. package/query/ObjectCriteriaNode.d.ts +2 -2
  55. package/query/ObjectCriteriaNode.js +43 -43
  56. package/query/QueryBuilder.d.ts +17 -15
  57. package/query/QueryBuilder.js +204 -193
  58. package/query/QueryBuilderHelper.d.ts +4 -4
  59. package/query/QueryBuilderHelper.js +86 -96
  60. package/query/ScalarCriteriaNode.d.ts +2 -2
  61. package/query/ScalarCriteriaNode.js +12 -16
  62. package/query/enums.js +4 -7
  63. package/query/index.d.ts +9 -9
  64. package/query/index.js +9 -25
  65. package/schema/DatabaseSchema.d.ts +3 -3
  66. package/schema/DatabaseSchema.js +7 -11
  67. package/schema/DatabaseTable.d.ts +3 -3
  68. package/schema/DatabaseTable.js +44 -33
  69. package/schema/SchemaComparator.d.ts +4 -4
  70. package/schema/SchemaComparator.js +15 -19
  71. package/schema/SchemaHelper.d.ts +5 -5
  72. package/schema/SchemaHelper.js +22 -26
  73. package/schema/SqlSchemaGenerator.d.ts +4 -4
  74. package/schema/SqlSchemaGenerator.js +23 -36
  75. package/schema/index.d.ts +5 -5
  76. package/schema/index.js +5 -21
  77. package/typings.d.ts +7 -4
  78. package/typings.js +1 -2
  79. package/index.mjs +0 -232
@@ -1,7 +1,7 @@
1
1
  import { type ControlledTransaction, type Dialect, Kysely } from 'kysely';
2
2
  import { type AnyEntity, Connection, type Dictionary, type EntityData, type IsolationLevel, type LoggingOptions, type QueryResult, RawQueryFragment, type Transaction, type TransactionEventBroadcaster } from '@mikro-orm/core';
3
- import type { AbstractSqlPlatform } from './AbstractSqlPlatform';
4
- import { NativeQueryBuilder } from './query';
3
+ import type { AbstractSqlPlatform } from './AbstractSqlPlatform.js';
4
+ import { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
5
5
  export declare abstract class AbstractSqlConnection extends Connection {
6
6
  protected platform: AbstractSqlPlatform;
7
7
  protected client: Kysely<any>;
@@ -1,27 +1,24 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AbstractSqlConnection = void 0;
4
- const kysely_1 = require("kysely");
5
- const fs_extra_1 = require("fs-extra");
6
- const core_1 = require("@mikro-orm/core");
7
- const query_1 = require("./query");
8
- class AbstractSqlConnection extends core_1.Connection {
1
+ import { CompiledQuery, Kysely, } from 'kysely';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { Connection, EventType, RawQueryFragment, } from '@mikro-orm/core';
4
+ import { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
5
+ export class AbstractSqlConnection extends Connection {
9
6
  client;
10
7
  async connect() {
11
8
  let driverOptions = this.options.driverOptions ?? this.config.get('driverOptions');
12
9
  if (typeof driverOptions === 'function') {
13
10
  driverOptions = await driverOptions();
14
11
  }
15
- if (driverOptions instanceof kysely_1.Kysely) {
12
+ if (driverOptions instanceof Kysely) {
16
13
  this.logger.log('info', 'Reusing Kysely client provided via `driverOptions`');
17
14
  this.client = driverOptions;
18
15
  }
19
16
  else if ('createDriver' in driverOptions) {
20
17
  this.logger.log('info', 'Reusing Kysely dialect provided via `driverOptions`');
21
- this.client = new kysely_1.Kysely({ dialect: driverOptions });
18
+ this.client = new Kysely({ dialect: driverOptions });
22
19
  }
23
20
  else {
24
- this.client = new kysely_1.Kysely({
21
+ this.client = new Kysely({
25
22
  dialect: this.createKyselyDialect(driverOptions),
26
23
  // log: m => console.log(m),
27
24
  });
@@ -51,7 +48,7 @@ class AbstractSqlConnection extends core_1.Connection {
51
48
  return { ok: false, reason: 'Connection not established' };
52
49
  }
53
50
  try {
54
- await this.client.executeQuery(kysely_1.CompiledQuery.raw('select 1'));
51
+ await this.client.executeQuery(CompiledQuery.raw('select 1'));
55
52
  return { ok: true };
56
53
  }
57
54
  catch (error) {
@@ -76,15 +73,18 @@ class AbstractSqlConnection extends core_1.Connection {
76
73
  async begin(options = {}) {
77
74
  if (options.ctx) {
78
75
  const ctx = options.ctx;
76
+ await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart, ctx);
79
77
  ctx.index ??= 0;
80
78
  const savepointName = `trx${ctx.index + 1}`;
81
79
  const trx = await options.ctx.savepoint(savepointName).execute();
82
80
  Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 });
83
81
  Reflect.defineProperty(trx, 'savepointName', { value: savepointName });
84
82
  this.logQuery(this.platform.getSavepointSQL(savepointName));
83
+ await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx);
85
84
  return trx;
86
85
  }
87
- await options.eventBroadcaster?.dispatchEvent(core_1.EventType.beforeTransactionStart);
86
+ await this.ensureConnection();
87
+ await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
88
88
  let trxBuilder = this.client.startTransaction();
89
89
  if (options.isolationLevel) {
90
90
  trxBuilder = trxBuilder.setIsolationLevel(options.isolationLevel);
@@ -96,40 +96,42 @@ class AbstractSqlConnection extends core_1.Connection {
96
96
  for (const query of this.platform.getBeginTransactionSQL(options)) {
97
97
  this.logQuery(query);
98
98
  }
99
- await options.eventBroadcaster?.dispatchEvent(core_1.EventType.afterTransactionStart, trx);
99
+ await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx);
100
100
  return trx;
101
101
  }
102
102
  async commit(ctx, eventBroadcaster) {
103
103
  if (ctx.isRolledBack) {
104
104
  return;
105
105
  }
106
+ await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionCommit, ctx);
106
107
  if ('savepointName' in ctx) {
107
108
  await ctx.releaseSavepoint(ctx.savepointName).execute();
108
109
  this.logQuery(this.platform.getReleaseSavepointSQL(ctx.savepointName));
109
- return;
110
110
  }
111
- await eventBroadcaster?.dispatchEvent(core_1.EventType.beforeTransactionCommit, ctx);
112
- await ctx.commit().execute();
113
- this.logQuery(this.platform.getCommitTransactionSQL());
114
- await eventBroadcaster?.dispatchEvent(core_1.EventType.afterTransactionCommit, ctx);
111
+ else {
112
+ await ctx.commit().execute();
113
+ this.logQuery(this.platform.getCommitTransactionSQL());
114
+ }
115
+ await eventBroadcaster?.dispatchEvent(EventType.afterTransactionCommit, ctx);
115
116
  }
116
117
  async rollback(ctx, eventBroadcaster) {
118
+ await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionRollback, ctx);
117
119
  if ('savepointName' in ctx) {
118
120
  await ctx.rollbackToSavepoint(ctx.savepointName).execute();
119
121
  this.logQuery(this.platform.getRollbackToSavepointSQL(ctx.savepointName));
120
- return;
121
122
  }
122
- await eventBroadcaster?.dispatchEvent(core_1.EventType.beforeTransactionRollback, ctx);
123
- await ctx.rollback().execute();
124
- this.logQuery(this.platform.getRollbackTransactionSQL());
125
- await eventBroadcaster?.dispatchEvent(core_1.EventType.afterTransactionRollback, ctx);
123
+ else {
124
+ await ctx.rollback().execute();
125
+ this.logQuery(this.platform.getRollbackTransactionSQL());
126
+ }
127
+ await eventBroadcaster?.dispatchEvent(EventType.afterTransactionRollback, ctx);
126
128
  }
127
129
  async execute(query, params = [], method = 'all', ctx, loggerContext) {
128
130
  await this.ensureConnection();
129
- if (query instanceof query_1.NativeQueryBuilder) {
131
+ if (query instanceof NativeQueryBuilder) {
130
132
  query = query.toRaw();
131
133
  }
132
- if (query instanceof core_1.RawQueryFragment) {
134
+ if (query instanceof RawQueryFragment) {
133
135
  params = query.params;
134
136
  query = query.sql;
135
137
  }
@@ -137,7 +139,7 @@ class AbstractSqlConnection extends core_1.Connection {
137
139
  const formatted = this.platform.formatQuery(query, params);
138
140
  const sql = this.getSql(query, formatted, loggerContext);
139
141
  return this.executeQuery(sql, async () => {
140
- const compiled = kysely_1.CompiledQuery.raw(formatted);
142
+ const compiled = CompiledQuery.raw(formatted);
141
143
  if (ctx) {
142
144
  const res = await ctx.executeQuery(compiled);
143
145
  return this.transformRawResult(res, method);
@@ -150,13 +152,14 @@ class AbstractSqlConnection extends core_1.Connection {
150
152
  * Execute raw SQL queries from file
151
153
  */
152
154
  async loadFile(path) {
153
- const buf = await (0, fs_extra_1.readFile)(path);
155
+ await this.ensureConnection();
156
+ const buf = await readFile(path);
154
157
  try {
155
- const raw = kysely_1.CompiledQuery.raw(buf.toString());
158
+ const raw = CompiledQuery.raw(buf.toString());
156
159
  await this.client.executeQuery(raw);
157
160
  }
158
161
  catch (e) {
159
- /* istanbul ignore next */
162
+ /* v8 ignore next */
160
163
  throw this.platform.getExceptionConverter().convertException(e);
161
164
  }
162
165
  }
@@ -185,4 +188,3 @@ class AbstractSqlConnection extends core_1.Connection {
185
188
  };
186
189
  }
187
190
  }
188
- exports.AbstractSqlConnection = AbstractSqlConnection;
@@ -1,9 +1,11 @@
1
- import { type AnyEntity, type Collection, type Configuration, type ConnectionType, type Constructor, type CountOptions, DatabaseDriver, type DeleteOptions, type Dictionary, type DriverMethodOptions, type EntityData, type EntityDictionary, type EntityField, EntityManagerType, type EntityMetadata, type EntityName, type EntityProperty, type FilterQuery, type FindOneOptions, type FindOptions, type IDatabaseDriver, type LockOptions, type LoggingOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type ObjectQuery, type Options, type OrderDefinition, type PopulateOptions, type PopulatePath, type Primary, type QueryOrderMap, type QueryResult, RawQueryFragment, type Transaction, type UpsertManyOptions, type UpsertOptions } from '@mikro-orm/core';
2
- import type { AbstractSqlConnection } from './AbstractSqlConnection';
3
- import type { AbstractSqlPlatform } from './AbstractSqlPlatform';
4
- import { type NativeQueryBuilder, QueryBuilder, QueryType } from './query';
5
- import { SqlEntityManager } from './SqlEntityManager';
6
- import type { Field } from './typings';
1
+ import { type AnyEntity, type Collection, type Configuration, type ConnectionType, type Constructor, type CountOptions, DatabaseDriver, type DeleteOptions, type Dictionary, type DriverMethodOptions, type EntityData, type EntityDictionary, type EntityField, EntityManagerType, type EntityMetadata, type EntityName, type EntityProperty, type FilterQuery, type FindOneOptions, type FindOptions, type LockOptions, type LoggingOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type ObjectQuery, type Options, type OrderDefinition, type PopulateOptions, type PopulatePath, type Primary, type QueryOrderMap, type QueryResult, RawQueryFragment, type Transaction, type UpsertManyOptions, type UpsertOptions } from '@mikro-orm/core';
2
+ import type { AbstractSqlConnection } from './AbstractSqlConnection.js';
3
+ import type { AbstractSqlPlatform } from './AbstractSqlPlatform.js';
4
+ import { QueryBuilder } from './query/QueryBuilder.js';
5
+ import { type NativeQueryBuilder } from './query/NativeQueryBuilder.js';
6
+ import { QueryType } from './query/enums.js';
7
+ import { SqlEntityManager } from './SqlEntityManager.js';
8
+ import type { Field } from './typings.js';
7
9
  export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection = AbstractSqlConnection, Platform extends AbstractSqlPlatform = AbstractSqlPlatform> extends DatabaseDriver<Connection> {
8
10
  [EntityManagerType]: SqlEntityManager<this>;
9
11
  protected readonly connection: Connection;
@@ -11,13 +13,13 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
11
13
  protected readonly platform: Platform;
12
14
  protected constructor(config: Configuration, platform: Platform, connection: Constructor<Connection>, connector: string[]);
13
15
  getPlatform(): Platform;
14
- createEntityManager<D extends IDatabaseDriver = IDatabaseDriver>(useContext?: boolean): D[typeof EntityManagerType];
15
- find<T extends object, P extends string = never, F extends string = PopulatePath.ALL, E extends string = never>(entityName: string, where: FilterQuery<T>, options?: FindOptions<T, P, F, E>): Promise<EntityData<T>[]>;
16
- findOne<T extends object, P extends string = never, F extends string = PopulatePath.ALL, E extends string = never>(entityName: string, where: FilterQuery<T>, options?: FindOneOptions<T, P, F, E>): Promise<EntityData<T> | null>;
16
+ createEntityManager(useContext?: boolean): this[typeof EntityManagerType];
17
+ find<T extends object, P extends string = never, F extends string = PopulatePath.ALL, E extends string = never>(entityName: string, where: ObjectQuery<T>, options?: FindOptions<T, P, F, E>): Promise<EntityData<T>[]>;
18
+ findOne<T extends object, P extends string = never, F extends string = PopulatePath.ALL, E extends string = never>(entityName: string, where: ObjectQuery<T>, options?: FindOneOptions<T, P, F, E>): Promise<EntityData<T> | null>;
17
19
  protected hasToManyJoins<T extends object>(hint: PopulateOptions<T>, meta: EntityMetadata<T>): boolean;
18
- findVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
19
- countVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: CountOptions<T, any>): Promise<number>;
20
- protected findFromVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: FindOptions<T, any> | CountOptions<T, any>, type: QueryType): Promise<EntityData<T>[] | number>;
20
+ findVirtual<T extends object>(entityName: string, where: ObjectQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
21
+ countVirtual<T extends object>(entityName: string, where: ObjectQuery<T>, options: CountOptions<T, any>): Promise<number>;
22
+ protected findFromVirtual<T extends object>(entityName: string, where: ObjectQuery<T>, options: FindOptions<T, any> | CountOptions<T, any>, type: QueryType): Promise<EntityData<T>[] | number>;
21
23
  protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any>, type: QueryType): Promise<T[] | number>;
22
24
  mapResult<T extends object>(result: EntityData<T>, meta: EntityMetadata<T>, populate?: PopulateOptions<T>[], qb?: QueryBuilder<T, any, any, any>, map?: Dictionary): EntityData<T> | null;
23
25
  private mapJoinedProps;