@mikro-orm/sql 7.0.0-dev.97 → 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.
Files changed (91) hide show
  1. package/AbstractSqlConnection.d.ts +57 -0
  2. package/AbstractSqlConnection.js +239 -0
  3. package/AbstractSqlDriver.d.ts +94 -0
  4. package/AbstractSqlDriver.js +1387 -0
  5. package/AbstractSqlPlatform.d.ts +38 -0
  6. package/AbstractSqlPlatform.js +104 -0
  7. package/LICENSE +21 -0
  8. package/PivotCollectionPersister.d.ts +22 -0
  9. package/PivotCollectionPersister.js +159 -0
  10. package/README.md +390 -0
  11. package/SqlEntityManager.d.ts +33 -0
  12. package/SqlEntityManager.js +44 -0
  13. package/SqlEntityRepository.d.ts +19 -0
  14. package/SqlEntityRepository.js +26 -0
  15. package/dialects/index.d.ts +4 -0
  16. package/dialects/index.js +4 -0
  17. package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +14 -0
  18. package/dialects/mssql/MsSqlNativeQueryBuilder.js +200 -0
  19. package/dialects/mssql/index.d.ts +1 -0
  20. package/dialects/mssql/index.js +1 -0
  21. package/dialects/mysql/MySqlExceptionConverter.d.ts +9 -0
  22. package/dialects/mysql/MySqlExceptionConverter.js +80 -0
  23. package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +7 -0
  24. package/dialects/mysql/MySqlNativeQueryBuilder.js +77 -0
  25. package/dialects/mysql/MySqlPlatform.d.ts +45 -0
  26. package/dialects/mysql/MySqlPlatform.js +116 -0
  27. package/dialects/mysql/MySqlSchemaHelper.d.ts +36 -0
  28. package/dialects/mysql/MySqlSchemaHelper.js +269 -0
  29. package/dialects/mysql/index.d.ts +4 -0
  30. package/dialects/mysql/index.js +4 -0
  31. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +5 -0
  32. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +8 -0
  33. package/dialects/postgresql/PostgreSqlTableCompiler.d.ts +1 -0
  34. package/dialects/postgresql/PostgreSqlTableCompiler.js +1 -0
  35. package/dialects/postgresql/index.d.ts +1 -0
  36. package/dialects/postgresql/index.js +1 -0
  37. package/dialects/sqlite/BaseSqliteConnection.d.ts +6 -0
  38. package/dialects/sqlite/BaseSqliteConnection.js +8 -0
  39. package/dialects/sqlite/BaseSqlitePlatform.d.ts +70 -0
  40. package/dialects/sqlite/BaseSqlitePlatform.js +104 -0
  41. package/dialects/sqlite/SqliteExceptionConverter.d.ts +9 -0
  42. package/dialects/sqlite/SqliteExceptionConverter.js +54 -0
  43. package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +6 -0
  44. package/dialects/sqlite/SqliteNativeQueryBuilder.js +11 -0
  45. package/dialects/sqlite/SqliteSchemaHelper.d.ts +38 -0
  46. package/dialects/sqlite/SqliteSchemaHelper.js +379 -0
  47. package/dialects/sqlite/index.d.ts +5 -0
  48. package/dialects/sqlite/index.js +5 -0
  49. package/index.d.ts +19 -0
  50. package/index.js +19 -0
  51. package/package.json +3 -3
  52. package/plugin/index.d.ts +53 -0
  53. package/plugin/index.js +42 -0
  54. package/plugin/transformer.d.ts +115 -0
  55. package/plugin/transformer.js +883 -0
  56. package/query/ArrayCriteriaNode.d.ts +11 -0
  57. package/query/ArrayCriteriaNode.js +24 -0
  58. package/query/CriteriaNode.d.ts +29 -0
  59. package/query/CriteriaNode.js +121 -0
  60. package/query/CriteriaNodeFactory.d.ts +12 -0
  61. package/query/CriteriaNodeFactory.js +90 -0
  62. package/query/NativeQueryBuilder.d.ts +108 -0
  63. package/query/NativeQueryBuilder.js +425 -0
  64. package/query/ObjectCriteriaNode.d.ts +19 -0
  65. package/query/ObjectCriteriaNode.js +249 -0
  66. package/query/QueryBuilder.d.ts +389 -0
  67. package/query/QueryBuilder.js +1558 -0
  68. package/query/QueryBuilderHelper.d.ts +73 -0
  69. package/query/QueryBuilderHelper.js +756 -0
  70. package/query/ScalarCriteriaNode.d.ts +10 -0
  71. package/query/ScalarCriteriaNode.js +49 -0
  72. package/query/enums.d.ts +18 -0
  73. package/query/enums.js +20 -0
  74. package/query/index.d.ts +10 -0
  75. package/query/index.js +10 -0
  76. package/query/raw.d.ts +59 -0
  77. package/query/raw.js +68 -0
  78. package/schema/DatabaseSchema.d.ts +45 -0
  79. package/schema/DatabaseSchema.js +185 -0
  80. package/schema/DatabaseTable.d.ts +68 -0
  81. package/schema/DatabaseTable.js +793 -0
  82. package/schema/SchemaComparator.d.ts +58 -0
  83. package/schema/SchemaComparator.js +577 -0
  84. package/schema/SchemaHelper.d.ts +76 -0
  85. package/schema/SchemaHelper.js +545 -0
  86. package/schema/SqlSchemaGenerator.d.ts +65 -0
  87. package/schema/SqlSchemaGenerator.js +375 -0
  88. package/schema/index.d.ts +5 -0
  89. package/schema/index.js +5 -0
  90. package/typings.d.ts +272 -0
  91. package/typings.js +1 -0
package/README.md ADDED
@@ -0,0 +1,390 @@
1
+ <h1 align="center">
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
+ </h1>
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 and SQLite (including libSQL) databases.
6
+
7
+ > Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [Hibernate](https://hibernate.org/).
8
+
9
+ [![NPM version](https://img.shields.io/npm/v/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
10
+ [![NPM dev version](https://img.shields.io/npm/v/@mikro-orm/core/next.svg)](https://www.npmjs.com/package/@mikro-orm/core)
11
+ [![Chat on discord](https://img.shields.io/discord/1214904142443839538?label=discord&color=blue)](https://discord.gg/w8bjxFHS7X)
12
+ [![Downloads](https://img.shields.io/npm/dm/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
13
+ [![Coverage Status](https://img.shields.io/coveralls/mikro-orm/mikro-orm.svg)](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
14
+ [![Build Status](https://github.com/mikro-orm/mikro-orm/workflows/tests/badge.svg?branch=master)](https://github.com/mikro-orm/mikro-orm/actions?workflow=tests)
15
+
16
+ ## 🤔 Unit of What?
17
+
18
+ You might be asking: _What the hell is Unit of Work and why should I care about it?_
19
+
20
+ > Unit of Work maintains a list of objects (_entities_) affected by a business transaction
21
+ > and coordinates the writing out of changes. [(Martin Fowler)](https://www.martinfowler.com/eaaCatalog/unitOfWork.html)
22
+
23
+ > Identity Map ensures that each object (_entity_) gets loaded only once by keeping every
24
+ > loaded object in a map. Looks up objects using the map when referring to them.
25
+ > [(Martin Fowler)](https://www.martinfowler.com/eaaCatalog/identityMap.html)
26
+
27
+ So what benefits does it bring to us?
28
+
29
+ ### Implicit Transactions
30
+
31
+ First and most important implication of having Unit of Work is that it allows handling transactions automatically.
32
+
33
+ When you call `em.flush()`, all computed changes are queried inside a database transaction (if supported by given driver). This means that you can control the boundaries of transactions simply by calling `em.persistLater()` and once all your changes are ready, calling `flush()` will run them inside a transaction.
34
+
35
+ > You can also control the transaction boundaries manually via `em.transactional(cb)`.
36
+
37
+ ```typescript
38
+ const user = await em.findOneOrFail(User, 1);
39
+ user.email = 'foo@bar.com';
40
+ const car = new Car();
41
+ user.cars.add(car);
42
+
43
+ // thanks to bi-directional cascading we only need to persist user entity
44
+ // flushing will create a transaction, insert new car and update user with new email
45
+ // as user entity is managed, calling flush() is enough
46
+ await em.flush();
47
+ ```
48
+
49
+ ### ChangeSet based persistence
50
+
51
+ MikroORM allows you to implement your domain/business logic directly in the entities. To maintain always valid entities, you can use constructors to mark required properties. Let's define the `User` entity used in previous example:
52
+
53
+ ```typescript
54
+ @Entity()
55
+ export class User {
56
+
57
+ @PrimaryKey()
58
+ id!: number;
59
+
60
+ @Property()
61
+ name!: string;
62
+
63
+ @OneToOne(() => Address)
64
+ address?: Address;
65
+
66
+ @ManyToMany(() => Car)
67
+ cars = new Collection<Car>(this);
68
+
69
+ constructor(name: string) {
70
+ this.name = name;
71
+ }
72
+
73
+ }
74
+ ```
75
+
76
+ Now to create new instance of the `User` entity, we are forced to provide the `name`:
77
+
78
+ ```typescript
79
+ const user = new User('John Doe'); // name is required to create new user instance
80
+ user.address = new Address('10 Downing Street'); // address is optional
81
+ ```
82
+
83
+ Once your entities are loaded, make a number of synchronous actions on your entities,
84
+ then call `em.flush()`. This will trigger computing of change sets. Only entities
85
+ (and properties) that were changed will generate database queries, if there are no changes,
86
+ no transaction will be started.
87
+
88
+ ```typescript
89
+ const user = await em.findOneOrFail(User, 1, {
90
+ populate: ['cars', 'address.city'],
91
+ });
92
+ user.title = 'Mr.';
93
+ user.address.street = '10 Downing Street'; // address is 1:1 relation of Address entity
94
+ user.cars.getItems().forEach(car => car.forSale = true); // cars is 1:m collection of Car entities
95
+ const car = new Car('VW');
96
+ user.cars.add(car);
97
+
98
+ // now we can flush all changes done to managed entities
99
+ await em.flush();
100
+ ```
101
+
102
+ `em.flush()` will then execute these queries from the example above:
103
+
104
+ ```sql
105
+ begin;
106
+ update "user" set "title" = 'Mr.' where "id" = 1;
107
+ update "user_address" set "street" = '10 Downing Street' where "id" = 123;
108
+ update "car"
109
+ set "for_sale" = case
110
+ when ("id" = 1) then true
111
+ when ("id" = 2) then true
112
+ when ("id" = 3) then true
113
+ else "for_sale" end
114
+ where "id" in (1, 2, 3)
115
+ insert into "car" ("brand", "owner") values ('VW', 1);
116
+ commit;
117
+ ```
118
+
119
+ ### Identity Map
120
+
121
+ Thanks to Identity Map, you will always have only one instance of given entity in one context. This allows for some optimizations (skipping loading of already loaded entities), as well as comparison by identity (`ent1 === ent2`).
122
+
123
+ ## 📖 Documentation
124
+
125
+ MikroORM documentation, included in this repo in the root directory, is built with [Docusaurus](https://docusaurus.io) and publicly hosted on GitHub Pages at https://mikro-orm.io.
126
+
127
+ There is also auto-generated [CHANGELOG.md](CHANGELOG.md) file based on commit messages (via `semantic-release`).
128
+
129
+ ## ✨ Core Features
130
+
131
+ - [Clean and Simple Entity Definition](https://mikro-orm.io/docs/defining-entities)
132
+ - [Identity Map](https://mikro-orm.io/docs/identity-map)
133
+ - [Entity References](https://mikro-orm.io/docs/entity-references)
134
+ - [Using Entity Constructors](https://mikro-orm.io/docs/entity-constructors)
135
+ - [Modelling Relationships](https://mikro-orm.io/docs/relationships)
136
+ - [Collections](https://mikro-orm.io/docs/collections)
137
+ - [Unit of Work](https://mikro-orm.io/docs/unit-of-work)
138
+ - [Transactions](https://mikro-orm.io/docs/transactions)
139
+ - [Cascading persist and remove](https://mikro-orm.io/docs/cascading)
140
+ - [Composite and Foreign Keys as Primary Key](https://mikro-orm.io/docs/composite-keys)
141
+ - [Filters](https://mikro-orm.io/docs/filters)
142
+ - [Using `QueryBuilder`](https://mikro-orm.io/docs/query-builder)
143
+ - [Populating relations](https://mikro-orm.io/docs/populating-relations)
144
+ - [Property Validation](https://mikro-orm.io/docs/property-validation)
145
+ - [Lifecycle Hooks](https://mikro-orm.io/docs/events#hooks)
146
+ - [Vanilla JS Support](https://mikro-orm.io/docs/usage-with-js)
147
+ - [Schema Generator](https://mikro-orm.io/docs/schema-generator)
148
+ - [Entity Generator](https://mikro-orm.io/docs/entity-generator)
149
+
150
+ ## 📦 Example Integrations
151
+
152
+ You can find example integrations for some popular frameworks in the [`mikro-orm-examples` repository](https://github.com/mikro-orm/mikro-orm-examples):
153
+
154
+ ### TypeScript Examples
155
+
156
+ - [Express + MongoDB](https://github.com/mikro-orm/express-ts-example-app)
157
+ - [Nest + MySQL](https://github.com/mikro-orm/nestjs-example-app)
158
+ - [RealWorld example app (Nest + MySQL)](https://github.com/mikro-orm/nestjs-realworld-example-app)
159
+ - [Koa + SQLite](https://github.com/mikro-orm/koa-ts-example-app)
160
+ - [GraphQL + PostgreSQL](https://github.com/driescroons/mikro-orm-graphql-example)
161
+ - [Inversify + PostgreSQL](https://github.com/PodaruDragos/inversify-example-app)
162
+ - [NextJS + MySQL](https://github.com/jonahallibone/mikro-orm-nextjs)
163
+ - [Accounts.js REST and GraphQL authentication + SQLite](https://github.com/darkbasic/mikro-orm-accounts-example)
164
+ - [Nest + Shopify + PostgreSQL + GraphQL](https://github.com/Cloudshelf/Shopify_CSConnector)
165
+ - [Elysia.js + libSQL + Bun](https://github.com/mikro-orm/elysia-bun-example-app)
166
+ - [Electron.js + PostgreSQL](https://github.com/adnanlah/electron-mikro-orm-example-app)
167
+
168
+ ### JavaScript Examples
169
+
170
+ - [Express + SQLite](https://github.com/mikro-orm/express-js-example-app)
171
+
172
+ ## 🚀 Quick Start
173
+
174
+ First install the module via `yarn` or `npm` and do not forget to install the database driver as well:
175
+
176
+ > Since v4, you should install the driver package, but not the db connector itself, e.g. install `@mikro-orm/sqlite`, but not `sqlite3` as that is already included in the driver package.
177
+
178
+ ```sh
179
+ yarn add @mikro-orm/core @mikro-orm/mongodb # for mongo
180
+ yarn add @mikro-orm/core @mikro-orm/mysql # for mysql/mariadb
181
+ yarn add @mikro-orm/core @mikro-orm/mariadb # for mysql/mariadb
182
+ yarn add @mikro-orm/core @mikro-orm/postgresql # for postgresql
183
+ yarn add @mikro-orm/core @mikro-orm/mssql # for mssql
184
+ yarn add @mikro-orm/core @mikro-orm/sqlite # for sqlite
185
+ yarn add @mikro-orm/core @mikro-orm/libsql # for libsql
186
+ ```
187
+
188
+ or
189
+
190
+ ```sh
191
+ npm i -s @mikro-orm/core @mikro-orm/mongodb # for mongo
192
+ npm i -s @mikro-orm/core @mikro-orm/mysql # for mysql/mariadb
193
+ npm i -s @mikro-orm/core @mikro-orm/mariadb # for mysql/mariadb
194
+ npm i -s @mikro-orm/core @mikro-orm/postgresql # for postgresql
195
+ npm i -s @mikro-orm/core @mikro-orm/mssql # for mssql
196
+ npm i -s @mikro-orm/core @mikro-orm/sqlite # for sqlite
197
+ npm i -s @mikro-orm/core @mikro-orm/libsql # for libsql
198
+ ```
199
+
200
+ Next, if you want to use decorators for your entity definition, you will need to enable support for [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) as well as `esModuleInterop` in `tsconfig.json` via:
201
+
202
+ ```json
203
+ "experimentalDecorators": true,
204
+ "emitDecoratorMetadata": true,
205
+ "esModuleInterop": true,
206
+ ```
207
+
208
+ Alternatively, you can use [`EntitySchema`](https://mikro-orm.io/docs/entity-schema).
209
+
210
+ Then call `MikroORM.init` as part of bootstrapping your app:
211
+
212
+ > To access driver specific methods like `em.createQueryBuilder()` we need to specify the driver type when calling `MikroORM.init()`. Alternatively we can cast the `orm.em` to `EntityManager` exported from the driver package:
213
+ >
214
+ > ```ts
215
+ > import { EntityManager } from '@mikro-orm/postgresql';
216
+ > const em = orm.em as EntityManager;
217
+ > const qb = em.createQueryBuilder(...);
218
+ > ```
219
+
220
+ ```typescript
221
+ import type { PostgreSqlDriver } from '@mikro-orm/postgresql'; // or any other SQL driver package
222
+
223
+ const orm = await MikroORM.init<PostgreSqlDriver>({
224
+ entities: ['./dist/entities'], // path to your JS entities (dist), relative to `baseDir`
225
+ dbName: 'my-db-name',
226
+ type: 'postgresql',
227
+ });
228
+ console.log(orm.em); // access EntityManager via `em` property
229
+ ```
230
+
231
+ There are more ways to configure your entities, take a look at [installation page](https://mikro-orm.io/docs/installation/).
232
+
233
+ > Read more about all the possible configuration options in [Advanced Configuration](https://mikro-orm.io/docs/configuration) section.
234
+
235
+ Then you will need to fork entity manager for each request so their [identity maps](https://mikro-orm.io/docs/identity-map/) will not collide. To do so, use the `RequestContext` helper:
236
+
237
+ ```typescript
238
+ const app = express();
239
+
240
+ app.use((req, res, next) => {
241
+ RequestContext.create(orm.em, next);
242
+ });
243
+ ```
244
+
245
+ > You should register this middleware as the last one just before request handlers and before any of your custom middleware that is using the ORM. There might be issues when you register it before request processing middleware like `queryParser` or `bodyParser`, so definitely register the context after them.
246
+
247
+ More info about `RequestContext` is described [here](https://mikro-orm.io/docs/identity-map/#request-context).
248
+
249
+ Now you can start defining your entities (in one of the `entities` folders). This is how simple entity can look like in mongo driver:
250
+
251
+ **`./entities/MongoBook.ts`**
252
+
253
+ ```typescript
254
+ @Entity()
255
+ export class MongoBook {
256
+
257
+ @PrimaryKey()
258
+ _id: ObjectID;
259
+
260
+ @SerializedPrimaryKey()
261
+ id: string;
262
+
263
+ @Property()
264
+ title: string;
265
+
266
+ @ManyToOne(() => Author)
267
+ author: Author;
268
+
269
+ @ManyToMany(() => BookTag)
270
+ tags = new Collection<BookTag>(this);
271
+
272
+ constructor(title: string, author: Author) {
273
+ this.title = title;
274
+ this.author = author;
275
+ }
276
+
277
+ }
278
+ ```
279
+
280
+ For SQL drivers, you can use `id: number` PK:
281
+
282
+ **`./entities/SqlBook.ts`**
283
+
284
+ ```typescript
285
+ @Entity()
286
+ export class SqlBook {
287
+
288
+ @PrimaryKey()
289
+ id: number;
290
+
291
+ }
292
+ ```
293
+
294
+ Or if you want to use UUID primary keys:
295
+
296
+ **`./entities/UuidBook.ts`**
297
+
298
+ ```typescript
299
+ import { randomUUID } from 'node:crypto';
300
+
301
+ @Entity()
302
+ export class UuidBook {
303
+
304
+ @PrimaryKey()
305
+ uuid = randomUUID();
306
+
307
+ }
308
+ ```
309
+
310
+ More information can be found in [defining entities section](https://mikro-orm.io/docs/defining-entities/) in docs.
311
+
312
+ When you have your entities defined, you can start using ORM either via `EntityManager` or via `EntityRepository`s.
313
+
314
+ To save entity state to database, you need to persist it. Persist takes care or deciding whether to use `insert` or `update` and computes appropriate change-set. Entity references that are not persisted yet (does not have identifier) will be cascade persisted automatically.
315
+
316
+ ```typescript
317
+ // use constructors in your entities for required parameters
318
+ const author = new Author('Jon Snow', 'snow@wall.st');
319
+ author.born = new Date();
320
+
321
+ const publisher = new Publisher('7K publisher');
322
+
323
+ const book1 = new Book('My Life on The Wall, part 1', author);
324
+ book1.publisher = publisher;
325
+ const book2 = new Book('My Life on The Wall, part 2', author);
326
+ book2.publisher = publisher;
327
+ const book3 = new Book('My Life on The Wall, part 3', author);
328
+ book3.publisher = publisher;
329
+
330
+ // just persist books, author and publisher will be automatically cascade persisted
331
+ await em.persistAndFlush([book1, book2, book3]);
332
+ ```
333
+
334
+ To fetch entities from database you can use `find()` and `findOne()` of `EntityManager`:
335
+
336
+ ```typescript
337
+ const authors = em.find(Author, {}, { populate: ['books'] });
338
+
339
+ for (const author of authors) {
340
+ console.log(author); // instance of Author entity
341
+ console.log(author.name); // Jon Snow
342
+
343
+ for (const book of author.books) { // iterating books collection
344
+ console.log(book); // instance of Book entity
345
+ console.log(book.title); // My Life on The Wall, part 1/2/3
346
+ }
347
+ }
348
+ ```
349
+
350
+ More convenient way of fetching entities from database is by using `EntityRepository`, that carries the entity name, so you do not have to pass it to every `find` and `findOne` calls:
351
+
352
+ ```typescript
353
+ const booksRepository = em.getRepository(Book);
354
+
355
+ const books = await booksRepository.find({ author: '...' }, {
356
+ populate: ['author'],
357
+ limit: 1,
358
+ offset: 2,
359
+ orderBy: { title: QueryOrder.DESC },
360
+ });
361
+
362
+ console.log(books); // Loaded<Book, 'author'>[]
363
+ ```
364
+
365
+ Take a look at docs about [working with `EntityManager`](https://mikro-orm.io/docs/entity-manager/) or [using `EntityRepository` instead](https://mikro-orm.io/docs/repositories/).
366
+
367
+ ## 🤝 Contributing
368
+
369
+ Contributions, issues and feature requests are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the process for submitting pull requests to us.
370
+
371
+ ## Authors
372
+
373
+ 👤 **Martin Adámek**
374
+
375
+ - Twitter: [@B4nan](https://twitter.com/B4nan)
376
+ - Github: [@b4nan](https://github.com/b4nan)
377
+
378
+ See also the list of contributors who [participated](https://github.com/mikro-orm/mikro-orm/contributors) in this project.
379
+
380
+ ## Show Your Support
381
+
382
+ Please ⭐️ this repository if this project helped you!
383
+
384
+ > If you'd like to support my open-source work, consider sponsoring me directly at [github.com/sponsors/b4nan](https://github.com/sponsors/b4nan).
385
+
386
+ ## 📝 License
387
+
388
+ Copyright © 2018 [Martin Adámek](https://github.com/b4nan).
389
+
390
+ This project is licensed under the MIT License - see the [LICENSE file](LICENSE) for details.
@@ -0,0 +1,33 @@
1
+ import { type EntitySchemaWithMeta, EntityManager, type AnyEntity, type ConnectionType, type EntityData, type EntityName, type EntityRepository, type GetRepository, type QueryResult, type FilterQuery, type LoggingOptions, type RawQueryFragment } from '@mikro-orm/core';
2
+ import type { AbstractSqlDriver } from './AbstractSqlDriver.js';
3
+ import type { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
4
+ import type { QueryBuilder } from './query/QueryBuilder.js';
5
+ import type { SqlEntityRepository } from './SqlEntityRepository.js';
6
+ import type { Kysely } from 'kysely';
7
+ import type { InferKyselyDB } from './typings.js';
8
+ import { type MikroKyselyPluginOptions } from './plugin/index.js';
9
+ export interface GetKyselyOptions extends MikroKyselyPluginOptions {
10
+ type?: ConnectionType;
11
+ }
12
+ /**
13
+ * @inheritDoc
14
+ */
15
+ export declare class SqlEntityManager<Driver extends AbstractSqlDriver = AbstractSqlDriver> extends EntityManager<Driver> {
16
+ /**
17
+ * Creates a QueryBuilder instance
18
+ */
19
+ createQueryBuilder<Entity extends object, RootAlias extends string = never>(entityName: EntityName<Entity> | QueryBuilder<Entity>, alias?: RootAlias, type?: ConnectionType, loggerContext?: LoggingOptions): QueryBuilder<Entity, RootAlias>;
20
+ /**
21
+ * Shortcut for `createQueryBuilder()`
22
+ */
23
+ qb<Entity extends object, RootAlias extends string = never>(entityName: EntityName<Entity>, alias?: RootAlias, type?: ConnectionType, loggerContext?: LoggingOptions): QueryBuilder<Entity, RootAlias, never, never>;
24
+ /**
25
+ * Returns configured Kysely instance.
26
+ */
27
+ getKysely<TDB = undefined, TOptions extends GetKyselyOptions = GetKyselyOptions>(options?: TOptions): Kysely<TDB extends undefined ? InferKyselyDB<EntitiesFromManager<this>, TOptions> : TDB>;
28
+ execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(query: string | NativeQueryBuilder | RawQueryFragment, params?: any[], method?: 'all' | 'get' | 'run', loggerContext?: LoggingOptions): Promise<T>;
29
+ getRepository<T extends object, U extends EntityRepository<T> = SqlEntityRepository<T>>(entityName: EntityName<T>): GetRepository<T, U>;
30
+ protected applyDiscriminatorCondition<Entity extends object>(entityName: string, where: FilterQuery<Entity>): FilterQuery<Entity>;
31
+ }
32
+ type EntitiesFromManager<TEntityManager extends EntityManager<any>> = NonNullable<TEntityManager['~entities']> extends any[] ? (Extract<NonNullable<TEntityManager['~entities']>[number], EntitySchemaWithMeta>) : never;
33
+ export {};
@@ -0,0 +1,44 @@
1
+ import { EntityManager, } from '@mikro-orm/core';
2
+ import { MikroKyselyPlugin } from './plugin/index.js';
3
+ /**
4
+ * @inheritDoc
5
+ */
6
+ export class SqlEntityManager extends EntityManager {
7
+ /**
8
+ * Creates a QueryBuilder instance
9
+ */
10
+ createQueryBuilder(entityName, alias, type, loggerContext) {
11
+ const context = this.getContext(false);
12
+ return this.driver.createQueryBuilder(entityName, context.getTransactionContext(), type, true, loggerContext ?? context.loggerContext, alias, this);
13
+ }
14
+ /**
15
+ * Shortcut for `createQueryBuilder()`
16
+ */
17
+ qb(entityName, alias, type, loggerContext) {
18
+ return this.createQueryBuilder(entityName, alias, type, loggerContext);
19
+ }
20
+ /**
21
+ * Returns configured Kysely instance.
22
+ */
23
+ getKysely(options = {}) {
24
+ let kysely = this.getConnection(options.type).getClient();
25
+ if (options.columnNamingStrategy != null
26
+ || options.tableNamingStrategy != null
27
+ || options.processOnCreateHooks != null
28
+ || options.processOnUpdateHooks != null
29
+ || options.convertValues != null) {
30
+ kysely = kysely.withPlugin(new MikroKyselyPlugin(this, options));
31
+ }
32
+ return kysely;
33
+ }
34
+ async execute(query, params = [], method = 'all', loggerContext) {
35
+ return this.getDriver().execute(query, params, method, this.getContext(false).getTransactionContext(), loggerContext);
36
+ }
37
+ getRepository(entityName) {
38
+ return super.getRepository(entityName);
39
+ }
40
+ applyDiscriminatorCondition(entityName, where) {
41
+ // this is handled in QueryBuilder now for SQL drivers
42
+ return where;
43
+ }
44
+ }
@@ -0,0 +1,19 @@
1
+ import { EntityRepository, type EntityName } from '@mikro-orm/core';
2
+ import type { SqlEntityManager } from './SqlEntityManager.js';
3
+ import type { QueryBuilder } from './query/QueryBuilder.js';
4
+ export declare class SqlEntityRepository<Entity extends object> extends EntityRepository<Entity> {
5
+ protected readonly em: SqlEntityManager;
6
+ constructor(em: SqlEntityManager, entityName: EntityName<Entity>);
7
+ /**
8
+ * Creates a QueryBuilder instance
9
+ */
10
+ createQueryBuilder<RootAlias extends string = never>(alias?: RootAlias): QueryBuilder<Entity, RootAlias>;
11
+ /**
12
+ * Shortcut for `createQueryBuilder()`
13
+ */
14
+ qb<RootAlias extends string = never>(alias?: RootAlias): QueryBuilder<Entity, RootAlias>;
15
+ /**
16
+ * @inheritDoc
17
+ */
18
+ getEntityManager(): SqlEntityManager;
19
+ }
@@ -0,0 +1,26 @@
1
+ import { EntityRepository } from '@mikro-orm/core';
2
+ export class SqlEntityRepository extends EntityRepository {
3
+ em;
4
+ constructor(em, entityName) {
5
+ super(em, entityName);
6
+ this.em = em;
7
+ }
8
+ /**
9
+ * Creates a QueryBuilder instance
10
+ */
11
+ createQueryBuilder(alias) {
12
+ return this.getEntityManager().createQueryBuilder(this.entityName, alias);
13
+ }
14
+ /**
15
+ * Shortcut for `createQueryBuilder()`
16
+ */
17
+ qb(alias) {
18
+ return this.createQueryBuilder(alias);
19
+ }
20
+ /**
21
+ * @inheritDoc
22
+ */
23
+ getEntityManager() {
24
+ return this.em;
25
+ }
26
+ }
@@ -0,0 +1,4 @@
1
+ export * from './mssql/index.js';
2
+ export * from './mysql/index.js';
3
+ export * from './postgresql/index.js';
4
+ export * from './sqlite/index.js';
@@ -0,0 +1,4 @@
1
+ export * from './mssql/index.js';
2
+ export * from './mysql/index.js';
3
+ export * from './postgresql/index.js';
4
+ export * from './sqlite/index.js';
@@ -0,0 +1,14 @@
1
+ import { NativeQueryBuilder } from '../../query/NativeQueryBuilder.js';
2
+ /** @internal */
3
+ export declare class MsSqlNativeQueryBuilder extends NativeQueryBuilder {
4
+ compile(): {
5
+ sql: string;
6
+ params: unknown[];
7
+ };
8
+ protected compileInsert(): void;
9
+ private appendOutputTable;
10
+ private compileUpsert;
11
+ protected compileSelect(): void;
12
+ protected addLockClause(): void;
13
+ protected compileTruncate(): void;
14
+ }