@mikro-orm/knex 6.0.0-dev.9 → 6.0.0-dev.90
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.
- package/AbstractSqlConnection.d.ts +5 -2
- package/AbstractSqlConnection.js +25 -17
- package/AbstractSqlDriver.d.ts +19 -18
- package/AbstractSqlDriver.js +171 -70
- package/AbstractSqlPlatform.d.ts +1 -0
- package/AbstractSqlPlatform.js +26 -5
- package/MonkeyPatchable.d.ts +1 -0
- package/MonkeyPatchable.js +3 -0
- package/README.md +64 -107
- package/SqlEntityManager.d.ts +2 -5
- package/SqlEntityManager.js +5 -9
- package/SqlEntityRepository.d.ts +6 -4
- package/SqlEntityRepository.js +10 -8
- package/index.mjs +20 -6
- package/package.json +7 -7
- package/query/ArrayCriteriaNode.d.ts +3 -3
- package/query/CriteriaNode.d.ts +8 -8
- package/query/CriteriaNode.js +9 -9
- package/query/CriteriaNodeFactory.d.ts +6 -6
- package/query/CriteriaNodeFactory.js +10 -13
- package/query/ObjectCriteriaNode.d.ts +3 -3
- package/query/ObjectCriteriaNode.js +12 -8
- package/query/QueryBuilder.d.ts +27 -16
- package/query/QueryBuilder.js +250 -91
- package/query/QueryBuilderHelper.d.ts +6 -9
- package/query/QueryBuilderHelper.js +112 -96
- package/query/ScalarCriteriaNode.d.ts +3 -2
- package/query/ScalarCriteriaNode.js +9 -6
- package/query/enums.js +1 -1
- package/schema/DatabaseSchema.d.ts +4 -1
- package/schema/DatabaseSchema.js +22 -6
- package/schema/DatabaseTable.d.ts +2 -1
- package/schema/DatabaseTable.js +25 -24
- package/schema/SchemaComparator.js +9 -2
- package/schema/SchemaHelper.d.ts +5 -3
- package/schema/SchemaHelper.js +10 -4
- package/schema/SqlSchemaGenerator.d.ts +3 -5
- package/schema/SqlSchemaGenerator.js +41 -20
- package/typings.d.ts +10 -7
package/MonkeyPatchable.js
CHANGED
|
@@ -13,6 +13,8 @@ const mysql_1 = __importDefault(require("knex/lib/dialects/mysql"));
|
|
|
13
13
|
// @ts-ignore
|
|
14
14
|
const mysql_columncompiler_1 = __importDefault(require("knex/lib/dialects/mysql/schema/mysql-columncompiler"));
|
|
15
15
|
// @ts-ignore
|
|
16
|
+
const mysql_querycompiler_1 = __importDefault(require("knex/lib/dialects/mysql/query/mysql-querycompiler"));
|
|
17
|
+
// @ts-ignore
|
|
16
18
|
const pg_tablecompiler_1 = __importDefault(require("knex/lib/dialects/postgres/schema/pg-tablecompiler"));
|
|
17
19
|
// @ts-ignore
|
|
18
20
|
const sqlite3_1 = __importDefault(require("knex/lib/dialects/sqlite3"));
|
|
@@ -29,6 +31,7 @@ exports.MonkeyPatchable = {
|
|
|
29
31
|
QueryExecutioner: query_executioner_1.default,
|
|
30
32
|
MySqlDialect: mysql_1.default,
|
|
31
33
|
MySqlColumnCompiler: mysql_columncompiler_1.default,
|
|
34
|
+
MySqlQueryCompiler: mysql_querycompiler_1.default,
|
|
32
35
|
PostgresDialectTableCompiler: pg_tablecompiler_1.default,
|
|
33
36
|
Sqlite3Dialect: sqlite3_1.default,
|
|
34
37
|
Sqlite3DialectTableCompiler: sqlite_tablecompiler_1.default,
|
package/README.md
CHANGED
|
@@ -2,11 +2,9 @@
|
|
|
2
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
3
|
</h1>
|
|
4
4
|
|
|
5
|
-
TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/)
|
|
6
|
-
and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL,
|
|
7
|
-
MariaDB, PostgreSQL and SQLite databases.
|
|
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 databases.
|
|
8
6
|
|
|
9
|
-
> Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [
|
|
7
|
+
> Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [Hibernate](https://hibernate.org/).
|
|
10
8
|
|
|
11
9
|
[](https://www.npmjs.com/package/@mikro-orm/core)
|
|
12
10
|
[](https://www.npmjs.com/package/@mikro-orm/core)
|
|
@@ -31,13 +29,9 @@ So what benefits does it bring to us?
|
|
|
31
29
|
|
|
32
30
|
### Implicit Transactions
|
|
33
31
|
|
|
34
|
-
First and most important implication of having Unit of Work is that it allows handling
|
|
35
|
-
transactions automatically.
|
|
32
|
+
First and most important implication of having Unit of Work is that it allows handling transactions automatically.
|
|
36
33
|
|
|
37
|
-
When you call `em.flush()`, all computed changes are queried inside a database
|
|
38
|
-
transaction (if supported by given driver). This means that you can control the boundaries
|
|
39
|
-
of transactions simply by calling `em.persistLater()` and once all your changes
|
|
40
|
-
are ready, calling `flush()` will run them inside a transaction.
|
|
34
|
+
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.
|
|
41
35
|
|
|
42
36
|
> You can also control the transaction boundaries manually via `em.transactional(cb)`.
|
|
43
37
|
|
|
@@ -55,9 +49,7 @@ await em.flush();
|
|
|
55
49
|
|
|
56
50
|
### ChangeSet based persistence
|
|
57
51
|
|
|
58
|
-
MikroORM allows you to implement your domain/business logic directly in the entities.
|
|
59
|
-
To maintain always valid entities, you can use constructors to mark required properties.
|
|
60
|
-
Let's define the `User` entity used in previous example:
|
|
52
|
+
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:
|
|
61
53
|
|
|
62
54
|
```typescript
|
|
63
55
|
@Entity()
|
|
@@ -69,10 +61,10 @@ export class User {
|
|
|
69
61
|
@Property()
|
|
70
62
|
name!: string;
|
|
71
63
|
|
|
72
|
-
@OneToOne()
|
|
64
|
+
@OneToOne(() => Address)
|
|
73
65
|
address?: Address;
|
|
74
66
|
|
|
75
|
-
@ManyToMany()
|
|
67
|
+
@ManyToMany(() => Car)
|
|
76
68
|
cars = new Collection<Car>(this);
|
|
77
69
|
|
|
78
70
|
constructor(name: string) {
|
|
@@ -95,7 +87,9 @@ then call `em.flush()`. This will trigger computing of change sets. Only entitie
|
|
|
95
87
|
no transaction will be started.
|
|
96
88
|
|
|
97
89
|
```typescript
|
|
98
|
-
const user = await em.findOneOrFail(User, 1,
|
|
90
|
+
const user = await em.findOneOrFail(User, 1, {
|
|
91
|
+
populate: ['cars', 'address.city'],
|
|
92
|
+
});
|
|
99
93
|
user.title = 'Mr.';
|
|
100
94
|
user.address.street = '10 Downing Street'; // address is 1:1 relation of Address entity
|
|
101
95
|
user.cars.getItems().forEach(car => car.forSale = true); // cars is 1:m collection of Car entities
|
|
@@ -110,53 +104,49 @@ await em.flush();
|
|
|
110
104
|
|
|
111
105
|
```sql
|
|
112
106
|
begin;
|
|
113
|
-
update user set title = 'Mr.' where id = 1;
|
|
114
|
-
update user_address set street = '10 Downing Street' where id = 123;
|
|
115
|
-
update car
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
107
|
+
update "user" set "title" = 'Mr.' where "id" = 1;
|
|
108
|
+
update "user_address" set "street" = '10 Downing Street' where "id" = 123;
|
|
109
|
+
update "car"
|
|
110
|
+
set "for_sale" = case
|
|
111
|
+
when ("id" = 1) then true
|
|
112
|
+
when ("id" = 2) then true
|
|
113
|
+
when ("id" = 3) then true
|
|
114
|
+
else "for_sale" end
|
|
115
|
+
where "id" in (1, 2, 3)
|
|
116
|
+
insert into "car" ("brand", "owner") values ('VW', 1);
|
|
119
117
|
commit;
|
|
120
118
|
```
|
|
121
119
|
|
|
122
|
-
###
|
|
120
|
+
### Identity Map
|
|
123
121
|
|
|
124
|
-
Thanks to Identity Map, you will always have only one instance of given entity in one context.
|
|
125
|
-
This allows for some optimizations (skipping loading of already loaded entities), as well as
|
|
126
|
-
comparison by identity (`ent1 === ent2`).
|
|
122
|
+
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`).
|
|
127
123
|
|
|
128
124
|
## 📖 Documentation
|
|
129
125
|
|
|
130
|
-
MikroORM
|
|
131
|
-
[Jekyll](https://jekyllrb.com/) and publicly hosted on GitHub Pages at https://mikro-orm.io.
|
|
132
|
-
|
|
133
|
-
There is also auto-generated [CHANGELOG.md](CHANGELOG.md) file based on commit messages
|
|
134
|
-
(via `semantic-release`).
|
|
126
|
+
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.
|
|
135
127
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
> To upgrade to v4, please see the [upgrading guide](https://mikro-orm.io/docs/upgrading-v3-to-v4).
|
|
128
|
+
There is also auto-generated [CHANGELOG.md](CHANGELOG.md) file based on commit messages (via `semantic-release`).
|
|
139
129
|
|
|
140
130
|
## ✨ Core Features
|
|
141
131
|
|
|
142
|
-
- [Clean and Simple Entity Definition](https://mikro-orm.io/docs/defining-entities
|
|
143
|
-
- [Identity Map](https://mikro-orm.io/docs/identity-map
|
|
144
|
-
- [Entity References](https://mikro-orm.io/docs/entity-references
|
|
145
|
-
- [Using Entity Constructors](https://mikro-orm.io/docs/
|
|
146
|
-
- [Modelling Relationships](https://mikro-orm.io/docs/relationships
|
|
147
|
-
- [Collections](https://mikro-orm.io/docs/collections
|
|
148
|
-
- [Unit of Work](https://mikro-orm.io/docs/unit-of-work
|
|
149
|
-
- [Transactions](https://mikro-orm.io/docs/transactions
|
|
150
|
-
- [Cascading persist and remove](https://mikro-orm.io/docs/cascading
|
|
151
|
-
- [Composite and Foreign Keys as Primary Key](https://mikro-orm.io/docs/composite-keys
|
|
152
|
-
- [Filters](https://mikro-orm.io/docs/filters
|
|
153
|
-
- [Using `QueryBuilder`](https://mikro-orm.io/docs/query-builder
|
|
154
|
-
- [Preloading Deeply Nested Structures via populate](https://mikro-orm.io/docs/nested-populate
|
|
155
|
-
- [Property Validation](https://mikro-orm.io/docs/property-validation
|
|
156
|
-
- [Lifecycle Hooks](https://mikro-orm.io/docs/lifecycle-hooks
|
|
157
|
-
- [Vanilla JS Support](https://mikro-orm.io/docs/usage-with-js
|
|
158
|
-
- [Schema Generator](https://mikro-orm.io/docs/schema-generator
|
|
159
|
-
- [Entity Generator](https://mikro-orm.io/docs/entity-generator
|
|
132
|
+
- [Clean and Simple Entity Definition](https://mikro-orm.io/docs/defining-entities)
|
|
133
|
+
- [Identity Map](https://mikro-orm.io/docs/identity-map)
|
|
134
|
+
- [Entity References](https://mikro-orm.io/docs/entity-references)
|
|
135
|
+
- [Using Entity Constructors](https://mikro-orm.io/docs/entity-constructors)
|
|
136
|
+
- [Modelling Relationships](https://mikro-orm.io/docs/relationships)
|
|
137
|
+
- [Collections](https://mikro-orm.io/docs/collections)
|
|
138
|
+
- [Unit of Work](https://mikro-orm.io/docs/unit-of-work)
|
|
139
|
+
- [Transactions](https://mikro-orm.io/docs/transactions)
|
|
140
|
+
- [Cascading persist and remove](https://mikro-orm.io/docs/cascading)
|
|
141
|
+
- [Composite and Foreign Keys as Primary Key](https://mikro-orm.io/docs/composite-keys)
|
|
142
|
+
- [Filters](https://mikro-orm.io/docs/filters)
|
|
143
|
+
- [Using `QueryBuilder`](https://mikro-orm.io/docs/query-builder)
|
|
144
|
+
- [Preloading Deeply Nested Structures via populate](https://mikro-orm.io/docs/nested-populate)
|
|
145
|
+
- [Property Validation](https://mikro-orm.io/docs/property-validation)
|
|
146
|
+
- [Lifecycle Hooks](https://mikro-orm.io/docs/lifecycle-hooks)
|
|
147
|
+
- [Vanilla JS Support](https://mikro-orm.io/docs/usage-with-js)
|
|
148
|
+
- [Schema Generator](https://mikro-orm.io/docs/schema-generator)
|
|
149
|
+
- [Entity Generator](https://mikro-orm.io/docs/entity-generator)
|
|
160
150
|
|
|
161
151
|
## 📦 Example Integrations
|
|
162
152
|
|
|
@@ -173,27 +163,14 @@ You can find example integrations for some popular frameworks in the [`mikro-orm
|
|
|
173
163
|
- [NextJS + MySQL](https://github.com/jonahallibone/mikro-orm-nextjs)
|
|
174
164
|
|
|
175
165
|
### JavaScript Examples
|
|
176
|
-
- [Express + MongoDB](https://github.com/mikro-orm/express-js-example-app)
|
|
177
166
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
- Introducing MikroORM, TypeScript data-mapper ORM with Identity Map
|
|
181
|
-
- on [medium.com](https://medium.com/dailyjs/introducing-mikro-orm-typescript-data-mapper-orm-with-identity-map-9ba58d049e02)
|
|
182
|
-
- on [dev.to](https://dev.to/b4nan/introducing-mikroorm-typescript-data-mapper-orm-with-identity-map-pc8)
|
|
183
|
-
- Handling transactions and concurrency in MikroORM
|
|
184
|
-
- on [medium.com](https://medium.com/dailyjs/handling-transactions-and-concurrency-in-mikro-orm-ba80d0a65805)
|
|
185
|
-
- on [dev.to](https://dev.to/b4nan/handling-transactions-and-concurrency-in-mikroorm-2cfj)
|
|
186
|
-
- MikroORM 3: Knex.js, CLI, Schema Updates, Entity Generator and more…
|
|
187
|
-
- on [medium.com](https://medium.com/dailyjs/mikro-orm-3-knex-js-cli-schema-updates-entity-generator-and-more-e51ecbbc508c)
|
|
188
|
-
- on [dev.to](https://dev.to/b4nan/mikroorm-3-knex-js-cli-schema-updates-entity-generator-and-more-3g56)
|
|
167
|
+
- [Express + MongoDB](https://github.com/mikro-orm/express-js-example-app)
|
|
189
168
|
|
|
190
169
|
## 🚀 Quick Start
|
|
191
170
|
|
|
192
171
|
First install the module via `yarn` or `npm` and do not forget to install the database driver as well:
|
|
193
172
|
|
|
194
|
-
> Since v4, you should install the driver package, but not the db connector itself,
|
|
195
|
-
> e.g. install `@mikro-orm/sqlite`, but not `sqlite3` as that is already included
|
|
196
|
-
> in the driver package.
|
|
173
|
+
> 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.
|
|
197
174
|
|
|
198
175
|
```sh
|
|
199
176
|
yarn add @mikro-orm/core @mikro-orm/mongodb # for mongo
|
|
@@ -213,8 +190,7 @@ npm i -s @mikro-orm/core @mikro-orm/postgresql # for postgresql
|
|
|
213
190
|
npm i -s @mikro-orm/core @mikro-orm/sqlite # for sqlite
|
|
214
191
|
```
|
|
215
192
|
|
|
216
|
-
Next you will need to enable support for [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html)
|
|
217
|
-
as well as `esModuleInterop` in `tsconfig.json` via:
|
|
193
|
+
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:
|
|
218
194
|
|
|
219
195
|
```json
|
|
220
196
|
"experimentalDecorators": true,
|
|
@@ -222,11 +198,11 @@ as well as `esModuleInterop` in `tsconfig.json` via:
|
|
|
222
198
|
"esModuleInterop": true,
|
|
223
199
|
```
|
|
224
200
|
|
|
201
|
+
Alternatively, you can use [`EntitySchema`](https://mikro-orm.io/docs/entity-schema).
|
|
202
|
+
|
|
225
203
|
Then call `MikroORM.init` as part of bootstrapping your app:
|
|
226
204
|
|
|
227
|
-
> To access driver specific methods like `em.createQueryBuilder()` we need to specify
|
|
228
|
-
> the driver type when calling `MikroORM.init()`. Alternatively we can cast the
|
|
229
|
-
> `orm.em` to `EntityManager` exported from the driver package:
|
|
205
|
+
> 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:
|
|
230
206
|
>
|
|
231
207
|
> ```ts
|
|
232
208
|
> import { EntityManager } from '@mikro-orm/postgresql';
|
|
@@ -245,14 +221,11 @@ const orm = await MikroORM.init<PostgreSqlDriver>({
|
|
|
245
221
|
console.log(orm.em); // access EntityManager via `em` property
|
|
246
222
|
```
|
|
247
223
|
|
|
248
|
-
There are more ways to configure your entities, take a look at
|
|
249
|
-
[installation page](https://mikro-orm.io/docs/installation/).
|
|
224
|
+
There are more ways to configure your entities, take a look at [installation page](https://mikro-orm.io/docs/installation/).
|
|
250
225
|
|
|
251
226
|
> Read more about all the possible configuration options in [Advanced Configuration](https://mikro-orm.io/docs/configuration) section.
|
|
252
227
|
|
|
253
|
-
Then you will need to fork entity manager for each request so their
|
|
254
|
-
[identity maps](https://mikro-orm.io/docs/identity-map/) will not collide.
|
|
255
|
-
To do so, use the `RequestContext` helper:
|
|
228
|
+
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:
|
|
256
229
|
|
|
257
230
|
```typescript
|
|
258
231
|
const app = express();
|
|
@@ -262,15 +235,11 @@ app.use((req, res, next) => {
|
|
|
262
235
|
});
|
|
263
236
|
```
|
|
264
237
|
|
|
265
|
-
> You should register this middleware as the last one just before request handlers and before
|
|
266
|
-
> any of your custom middleware that is using the ORM. There might be issues when you register
|
|
267
|
-
> it before request processing middleware like `queryParser` or `bodyParser`, so definitely
|
|
268
|
-
> register the context after them.
|
|
238
|
+
> 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.
|
|
269
239
|
|
|
270
240
|
More info about `RequestContext` is described [here](https://mikro-orm.io/docs/identity-map/#request-context).
|
|
271
241
|
|
|
272
|
-
Now you can start defining your entities (in one of the `entities` folders). This is how
|
|
273
|
-
simple entity can look like in mongo driver:
|
|
242
|
+
Now you can start defining your entities (in one of the `entities` folders). This is how simple entity can look like in mongo driver:
|
|
274
243
|
|
|
275
244
|
**`./entities/MongoBook.ts`**
|
|
276
245
|
|
|
@@ -287,10 +256,10 @@ export class MongoBook {
|
|
|
287
256
|
@Property()
|
|
288
257
|
title: string;
|
|
289
258
|
|
|
290
|
-
@ManyToOne()
|
|
259
|
+
@ManyToOne(() => Author)
|
|
291
260
|
author: Author;
|
|
292
261
|
|
|
293
|
-
@ManyToMany()
|
|
262
|
+
@ManyToMany(() => BookTag)
|
|
294
263
|
tags = new Collection<BookTag>(this);
|
|
295
264
|
|
|
296
265
|
constructor(title: string, author: Author) {
|
|
@@ -331,15 +300,11 @@ export class UuidBook {
|
|
|
331
300
|
}
|
|
332
301
|
```
|
|
333
302
|
|
|
334
|
-
More information can be found in
|
|
335
|
-
[defining entities section](https://mikro-orm.io/docs/defining-entities/) in docs.
|
|
303
|
+
More information can be found in [defining entities section](https://mikro-orm.io/docs/defining-entities/) in docs.
|
|
336
304
|
|
|
337
|
-
When you have your entities defined, you can start using ORM either via `EntityManager`
|
|
338
|
-
or via `EntityRepository`s.
|
|
305
|
+
When you have your entities defined, you can start using ORM either via `EntityManager` or via `EntityRepository`s.
|
|
339
306
|
|
|
340
|
-
To save entity state to database, you need to persist it. Persist takes care or deciding
|
|
341
|
-
whether to use `insert` or `update` and computes appropriate change-set. Entity references
|
|
342
|
-
that are not persisted yet (does not have identifier) will be cascade persisted automatically.
|
|
307
|
+
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.
|
|
343
308
|
|
|
344
309
|
```typescript
|
|
345
310
|
// use constructors in your entities for required parameters
|
|
@@ -356,13 +321,13 @@ const book3 = new Book('My Life on The Wall, part 3', author);
|
|
|
356
321
|
book3.publisher = publisher;
|
|
357
322
|
|
|
358
323
|
// just persist books, author and publisher will be automatically cascade persisted
|
|
359
|
-
await
|
|
324
|
+
await em.persistAndFlush([book1, book2, book3]);
|
|
360
325
|
```
|
|
361
326
|
|
|
362
327
|
To fetch entities from database you can use `find()` and `findOne()` of `EntityManager`:
|
|
363
328
|
|
|
364
329
|
```typescript
|
|
365
|
-
const authors =
|
|
330
|
+
const authors = em.find(Author, {}, { populate: ['books'] });
|
|
366
331
|
|
|
367
332
|
for (const author of authors) {
|
|
368
333
|
console.log(author); // instance of Author entity
|
|
@@ -375,16 +340,11 @@ for (const author of authors) {
|
|
|
375
340
|
}
|
|
376
341
|
```
|
|
377
342
|
|
|
378
|
-
More convenient way of fetching entities from database is by using `EntityRepository`, that
|
|
379
|
-
carries the entity name so you do not have to pass it to every `find` and `findOne` calls:
|
|
343
|
+
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:
|
|
380
344
|
|
|
381
345
|
```typescript
|
|
382
|
-
const booksRepository =
|
|
383
|
-
|
|
384
|
-
// with sorting, limit and offset parameters, populating author references
|
|
385
|
-
const books = await booksRepository.find({ author: '...' }, ['author'], { title: QueryOrder.DESC }, 2, 1);
|
|
346
|
+
const booksRepository = em.getRepository(Book);
|
|
386
347
|
|
|
387
|
-
// or with options object
|
|
388
348
|
const books = await booksRepository.find({ author: '...' }, {
|
|
389
349
|
populate: ['author'],
|
|
390
350
|
limit: 1,
|
|
@@ -392,17 +352,14 @@ const books = await booksRepository.find({ author: '...' }, {
|
|
|
392
352
|
orderBy: { title: QueryOrder.DESC },
|
|
393
353
|
});
|
|
394
354
|
|
|
395
|
-
console.log(books); // Book[]
|
|
355
|
+
console.log(books); // Loaded<Book, 'author'>[]
|
|
396
356
|
```
|
|
397
357
|
|
|
398
|
-
Take a look at docs about [working with `EntityManager`](https://mikro-orm.io/docs/entity-manager/)
|
|
399
|
-
or [using `EntityRepository` instead](https://mikro-orm.io/docs/repositories/).
|
|
358
|
+
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/).
|
|
400
359
|
|
|
401
360
|
## 🤝 Contributing
|
|
402
361
|
|
|
403
|
-
Contributions, issues and feature requests are welcome. Please read
|
|
404
|
-
[CONTRIBUTING.md](CONTRIBUTING.md)
|
|
405
|
-
for details on the process for submitting pull requests to us.
|
|
362
|
+
Contributions, issues and feature requests are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on the process for submitting pull requests to us.
|
|
406
363
|
|
|
407
364
|
## Authors
|
|
408
365
|
|
package/SqlEntityManager.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Knex } from 'knex';
|
|
2
|
-
import type { AnyEntity, ConnectionType, EntityData, EntityName, EntityRepository, GetRepository, QueryResult } from '@mikro-orm/core';
|
|
2
|
+
import type { AnyEntity, ConnectionType, EntityData, EntityName, EntityRepository, GetRepository, QueryResult, FilterQuery } from '@mikro-orm/core';
|
|
3
3
|
import { EntityManager } from '@mikro-orm/core';
|
|
4
4
|
import type { AbstractSqlDriver } from './AbstractSqlDriver';
|
|
5
5
|
import { QueryBuilder } from './query';
|
|
@@ -16,14 +16,11 @@ export declare class SqlEntityManager<D extends AbstractSqlDriver = AbstractSqlD
|
|
|
16
16
|
* Shortcut for `createQueryBuilder()`
|
|
17
17
|
*/
|
|
18
18
|
qb<T extends object>(entityName: EntityName<T>, alias?: string, type?: ConnectionType): QueryBuilder<T>;
|
|
19
|
-
/**
|
|
20
|
-
* Creates raw SQL query that won't be escaped when used as a parameter.
|
|
21
|
-
*/
|
|
22
|
-
raw<R = Knex.Raw>(sql: string, bindings?: Knex.RawBinding[] | Knex.ValueDict): R;
|
|
23
19
|
/**
|
|
24
20
|
* Returns configured knex instance.
|
|
25
21
|
*/
|
|
26
22
|
getKnex(type?: ConnectionType): Knex<any, any[]>;
|
|
27
23
|
execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(queryOrKnex: string | Knex.QueryBuilder | Knex.Raw, params?: any[], method?: 'all' | 'get' | 'run'): Promise<T>;
|
|
28
24
|
getRepository<T extends object, U extends EntityRepository<T> = SqlEntityRepository<T>>(entityName: EntityName<T>): GetRepository<T, U>;
|
|
25
|
+
protected applyDiscriminatorCondition<Entity extends object>(entityName: string, where: FilterQuery<Entity>): FilterQuery<Entity>;
|
|
29
26
|
}
|
package/SqlEntityManager.js
CHANGED
|
@@ -20,14 +20,6 @@ class SqlEntityManager extends core_1.EntityManager {
|
|
|
20
20
|
qb(entityName, alias, type) {
|
|
21
21
|
return this.createQueryBuilder(entityName, alias, type);
|
|
22
22
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Creates raw SQL query that won't be escaped when used as a parameter.
|
|
25
|
-
*/
|
|
26
|
-
raw(sql, bindings = []) {
|
|
27
|
-
const raw = this.getKnex().raw(sql, bindings);
|
|
28
|
-
raw.__raw = true; // tag it as there is now way to check via `instanceof`
|
|
29
|
-
return raw;
|
|
30
|
-
}
|
|
31
23
|
/**
|
|
32
24
|
* Returns configured knex instance.
|
|
33
25
|
*/
|
|
@@ -35,10 +27,14 @@ class SqlEntityManager extends core_1.EntityManager {
|
|
|
35
27
|
return this.getConnection(type).getKnex();
|
|
36
28
|
}
|
|
37
29
|
async execute(queryOrKnex, params = [], method = 'all') {
|
|
38
|
-
return this.getDriver().execute(queryOrKnex, params, method, this.getTransactionContext());
|
|
30
|
+
return this.getDriver().execute(queryOrKnex, params, method, this.getContext(false).getTransactionContext());
|
|
39
31
|
}
|
|
40
32
|
getRepository(entityName) {
|
|
41
33
|
return super.getRepository(entityName);
|
|
42
34
|
}
|
|
35
|
+
applyDiscriminatorCondition(entityName, where) {
|
|
36
|
+
// this is handled in QueryBuilder now for SQL drivers
|
|
37
|
+
return where;
|
|
38
|
+
}
|
|
43
39
|
}
|
|
44
40
|
exports.SqlEntityManager = SqlEntityManager;
|
package/SqlEntityRepository.d.ts
CHANGED
|
@@ -4,9 +4,8 @@ import { EntityRepository } from '@mikro-orm/core';
|
|
|
4
4
|
import type { SqlEntityManager } from './SqlEntityManager';
|
|
5
5
|
import type { QueryBuilder } from './query';
|
|
6
6
|
export declare class SqlEntityRepository<T extends object> extends EntityRepository<T> {
|
|
7
|
-
protected readonly
|
|
8
|
-
|
|
9
|
-
constructor(_em: SqlEntityManager, entityName: EntityName<T>);
|
|
7
|
+
protected readonly em: SqlEntityManager;
|
|
8
|
+
constructor(em: SqlEntityManager, entityName: EntityName<T>);
|
|
10
9
|
/**
|
|
11
10
|
* Creates a QueryBuilder instance
|
|
12
11
|
*/
|
|
@@ -19,5 +18,8 @@ export declare class SqlEntityRepository<T extends object> extends EntityReposit
|
|
|
19
18
|
* Returns configured knex instance.
|
|
20
19
|
*/
|
|
21
20
|
getKnex(type?: ConnectionType): Knex;
|
|
22
|
-
|
|
21
|
+
/**
|
|
22
|
+
* @inheritDoc
|
|
23
|
+
*/
|
|
24
|
+
getEntityManager(): SqlEntityManager;
|
|
23
25
|
}
|
package/SqlEntityRepository.js
CHANGED
|
@@ -3,16 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SqlEntityRepository = void 0;
|
|
4
4
|
const core_1 = require("@mikro-orm/core");
|
|
5
5
|
class SqlEntityRepository extends core_1.EntityRepository {
|
|
6
|
-
constructor(
|
|
7
|
-
super(
|
|
8
|
-
this.
|
|
9
|
-
this.entityName = entityName;
|
|
6
|
+
constructor(em, entityName) {
|
|
7
|
+
super(em, entityName);
|
|
8
|
+
this.em = em;
|
|
10
9
|
}
|
|
11
10
|
/**
|
|
12
11
|
* Creates a QueryBuilder instance
|
|
13
12
|
*/
|
|
14
13
|
createQueryBuilder(alias) {
|
|
15
|
-
return this.
|
|
14
|
+
return this.getEntityManager().createQueryBuilder(this.entityName, alias);
|
|
16
15
|
}
|
|
17
16
|
/**
|
|
18
17
|
* Shortcut for `createQueryBuilder()`
|
|
@@ -24,10 +23,13 @@ class SqlEntityRepository extends core_1.EntityRepository {
|
|
|
24
23
|
* Returns configured knex instance.
|
|
25
24
|
*/
|
|
26
25
|
getKnex(type) {
|
|
27
|
-
return this.
|
|
26
|
+
return this.getEntityManager().getKnex();
|
|
28
27
|
}
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
/**
|
|
29
|
+
* @inheritDoc
|
|
30
|
+
*/
|
|
31
|
+
getEntityManager() {
|
|
32
|
+
return this.em;
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
35
|
exports.SqlEntityRepository = SqlEntityRepository;
|
package/index.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import mod from "./index.js";
|
|
2
2
|
|
|
3
3
|
export default mod;
|
|
4
|
+
export const ALIAS_REPLACEMENT = mod.ALIAS_REPLACEMENT;
|
|
5
|
+
export const ALIAS_REPLACEMENT_RE = mod.ALIAS_REPLACEMENT_RE;
|
|
4
6
|
export const ARRAY_OPERATORS = mod.ARRAY_OPERATORS;
|
|
5
7
|
export const AbstractNamingStrategy = mod.AbstractNamingStrategy;
|
|
6
8
|
export const AbstractSchemaGenerator = mod.AbstractSchemaGenerator;
|
|
@@ -10,6 +12,7 @@ export const AbstractSqlPlatform = mod.AbstractSqlPlatform;
|
|
|
10
12
|
export const AfterCreate = mod.AfterCreate;
|
|
11
13
|
export const AfterDelete = mod.AfterDelete;
|
|
12
14
|
export const AfterUpdate = mod.AfterUpdate;
|
|
15
|
+
export const AfterUpsert = mod.AfterUpsert;
|
|
13
16
|
export const ArrayCollection = mod.ArrayCollection;
|
|
14
17
|
export const ArrayCriteriaNode = mod.ArrayCriteriaNode;
|
|
15
18
|
export const ArrayType = mod.ArrayType;
|
|
@@ -17,6 +20,7 @@ export const BaseEntity = mod.BaseEntity;
|
|
|
17
20
|
export const BeforeCreate = mod.BeforeCreate;
|
|
18
21
|
export const BeforeDelete = mod.BeforeDelete;
|
|
19
22
|
export const BeforeUpdate = mod.BeforeUpdate;
|
|
23
|
+
export const BeforeUpsert = mod.BeforeUpsert;
|
|
20
24
|
export const BigIntType = mod.BigIntType;
|
|
21
25
|
export const BlobType = mod.BlobType;
|
|
22
26
|
export const BooleanType = mod.BooleanType;
|
|
@@ -34,8 +38,10 @@ export const ConfigurationLoader = mod.ConfigurationLoader;
|
|
|
34
38
|
export const Connection = mod.Connection;
|
|
35
39
|
export const ConnectionException = mod.ConnectionException;
|
|
36
40
|
export const ConstraintViolationException = mod.ConstraintViolationException;
|
|
41
|
+
export const CreateRequestContext = mod.CreateRequestContext;
|
|
37
42
|
export const CriteriaNode = mod.CriteriaNode;
|
|
38
43
|
export const CriteriaNodeFactory = mod.CriteriaNodeFactory;
|
|
44
|
+
export const Cursor = mod.Cursor;
|
|
39
45
|
export const DatabaseDriver = mod.DatabaseDriver;
|
|
40
46
|
export const DatabaseObjectExistsException = mod.DatabaseObjectExistsException;
|
|
41
47
|
export const DatabaseObjectNotFoundException = mod.DatabaseObjectNotFoundException;
|
|
@@ -48,8 +54,10 @@ export const DecimalType = mod.DecimalType;
|
|
|
48
54
|
export const DefaultLogger = mod.DefaultLogger;
|
|
49
55
|
export const DoubleType = mod.DoubleType;
|
|
50
56
|
export const DriverException = mod.DriverException;
|
|
57
|
+
export const EagerProps = mod.EagerProps;
|
|
51
58
|
export const Embeddable = mod.Embeddable;
|
|
52
59
|
export const Embedded = mod.Embedded;
|
|
60
|
+
export const EnsureRequestContext = mod.EnsureRequestContext;
|
|
53
61
|
export const Entity = mod.Entity;
|
|
54
62
|
export const EntityAssigner = mod.EntityAssigner;
|
|
55
63
|
export const EntityCaseNamingStrategy = mod.EntityCaseNamingStrategy;
|
|
@@ -72,6 +80,7 @@ export const EnumArrayType = mod.EnumArrayType;
|
|
|
72
80
|
export const EnumType = mod.EnumType;
|
|
73
81
|
export const EventManager = mod.EventManager;
|
|
74
82
|
export const EventType = mod.EventType;
|
|
83
|
+
export const EventTypeMap = mod.EventTypeMap;
|
|
75
84
|
export const ExceptionConverter = mod.ExceptionConverter;
|
|
76
85
|
export const FileCacheAdapter = mod.FileCacheAdapter;
|
|
77
86
|
export const Filter = mod.Filter;
|
|
@@ -79,14 +88,15 @@ export const FloatType = mod.FloatType;
|
|
|
79
88
|
export const FlushMode = mod.FlushMode;
|
|
80
89
|
export const ForeignKeyConstraintViolationException = mod.ForeignKeyConstraintViolationException;
|
|
81
90
|
export const Formula = mod.Formula;
|
|
91
|
+
export const GeneratedCacheAdapter = mod.GeneratedCacheAdapter;
|
|
82
92
|
export const GroupOperator = mod.GroupOperator;
|
|
93
|
+
export const HiddenProps = mod.HiddenProps;
|
|
83
94
|
export const Hydrator = mod.Hydrator;
|
|
84
95
|
export const IdentityMap = mod.IdentityMap;
|
|
85
96
|
export const Index = mod.Index;
|
|
86
97
|
export const IntegerType = mod.IntegerType;
|
|
87
98
|
export const InvalidFieldNameException = mod.InvalidFieldNameException;
|
|
88
99
|
export const IsolationLevel = mod.IsolationLevel;
|
|
89
|
-
export const JavaScriptMetadataProvider = mod.JavaScriptMetadataProvider;
|
|
90
100
|
export const JsonProperty = mod.JsonProperty;
|
|
91
101
|
export const JsonType = mod.JsonType;
|
|
92
102
|
export const Knex = mod.Knex;
|
|
@@ -125,7 +135,6 @@ export const Platform = mod.Platform;
|
|
|
125
135
|
export const PopulateHint = mod.PopulateHint;
|
|
126
136
|
export const PrimaryKey = mod.PrimaryKey;
|
|
127
137
|
export const PrimaryKeyProp = mod.PrimaryKeyProp;
|
|
128
|
-
export const PrimaryKeyType = mod.PrimaryKeyType;
|
|
129
138
|
export const Property = mod.Property;
|
|
130
139
|
export const QueryBuilder = mod.QueryBuilder;
|
|
131
140
|
export const QueryBuilderHelper = mod.QueryBuilderHelper;
|
|
@@ -135,13 +144,16 @@ export const QueryOperator = mod.QueryOperator;
|
|
|
135
144
|
export const QueryOrder = mod.QueryOrder;
|
|
136
145
|
export const QueryOrderNumeric = mod.QueryOrderNumeric;
|
|
137
146
|
export const QueryType = mod.QueryType;
|
|
147
|
+
export const RawQueryFragment = mod.RawQueryFragment;
|
|
138
148
|
export const ReadOnlyException = mod.ReadOnlyException;
|
|
149
|
+
export const Ref = mod.Ref;
|
|
139
150
|
export const Reference = mod.Reference;
|
|
140
|
-
export const
|
|
151
|
+
export const ReferenceKind = mod.ReferenceKind;
|
|
141
152
|
export const ReflectMetadataProvider = mod.ReflectMetadataProvider;
|
|
142
153
|
export const RequestContext = mod.RequestContext;
|
|
143
154
|
export const SCALAR_TYPES = mod.SCALAR_TYPES;
|
|
144
155
|
export const ScalarCriteriaNode = mod.ScalarCriteriaNode;
|
|
156
|
+
export const ScalarReference = mod.ScalarReference;
|
|
145
157
|
export const SchemaComparator = mod.SchemaComparator;
|
|
146
158
|
export const SchemaGenerator = mod.SchemaGenerator;
|
|
147
159
|
export const SchemaHelper = mod.SchemaHelper;
|
|
@@ -154,7 +166,6 @@ export const SqlEntityManager = mod.SqlEntityManager;
|
|
|
154
166
|
export const SqlEntityRepository = mod.SqlEntityRepository;
|
|
155
167
|
export const SqlSchemaGenerator = mod.SqlSchemaGenerator;
|
|
156
168
|
export const StringType = mod.StringType;
|
|
157
|
-
export const Subscriber = mod.Subscriber;
|
|
158
169
|
export const SyntaxErrorException = mod.SyntaxErrorException;
|
|
159
170
|
export const TableExistsException = mod.TableExistsException;
|
|
160
171
|
export const TableNotFoundException = mod.TableNotFoundException;
|
|
@@ -164,12 +175,12 @@ export const TinyIntType = mod.TinyIntType;
|
|
|
164
175
|
export const TransactionContext = mod.TransactionContext;
|
|
165
176
|
export const TransactionEventBroadcaster = mod.TransactionEventBroadcaster;
|
|
166
177
|
export const Type = mod.Type;
|
|
178
|
+
export const Uint8ArrayType = mod.Uint8ArrayType;
|
|
167
179
|
export const UnderscoreNamingStrategy = mod.UnderscoreNamingStrategy;
|
|
168
180
|
export const Unique = mod.Unique;
|
|
169
181
|
export const UniqueConstraintViolationException = mod.UniqueConstraintViolationException;
|
|
170
182
|
export const UnitOfWork = mod.UnitOfWork;
|
|
171
183
|
export const UnknownType = mod.UnknownType;
|
|
172
|
-
export const UseRequestContext = mod.UseRequestContext;
|
|
173
184
|
export const Utils = mod.Utils;
|
|
174
185
|
export const UuidType = mod.UuidType;
|
|
175
186
|
export const ValidationError = mod.ValidationError;
|
|
@@ -182,11 +193,14 @@ export const compareBuffers = mod.compareBuffers;
|
|
|
182
193
|
export const compareObjects = mod.compareObjects;
|
|
183
194
|
export const defineConfig = mod.defineConfig;
|
|
184
195
|
export const equals = mod.equals;
|
|
185
|
-
export const expr = mod.expr;
|
|
186
196
|
export const helper = mod.helper;
|
|
187
197
|
export const knex = mod.knex;
|
|
198
|
+
export const parseJsonSafe = mod.parseJsonSafe;
|
|
199
|
+
export const raw = mod.raw;
|
|
188
200
|
export const ref = mod.ref;
|
|
201
|
+
export const rel = mod.rel;
|
|
189
202
|
export const serialize = mod.serialize;
|
|
203
|
+
export const sql = mod.sql;
|
|
190
204
|
export const t = mod.t;
|
|
191
205
|
export const types = mod.types;
|
|
192
206
|
export const wrap = mod.wrap;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "6.0.0-dev.
|
|
3
|
+
"version": "6.0.0-dev.90",
|
|
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
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -50,22 +50,22 @@
|
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "yarn clean && yarn compile && yarn copy && yarn run -T gen-esm-wrapper index.js index.mjs",
|
|
53
|
-
"clean": "rimraf ./dist",
|
|
54
|
-
"compile": "tsc -p tsconfig.build.json",
|
|
53
|
+
"clean": "yarn run -T rimraf ./dist",
|
|
54
|
+
"compile": "yarn run -T tsc -p tsconfig.build.json",
|
|
55
55
|
"copy": "node ../../scripts/copy.mjs"
|
|
56
56
|
},
|
|
57
57
|
"publishConfig": {
|
|
58
58
|
"access": "public"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"fs-extra": "11.1.
|
|
62
|
-
"knex": "2.
|
|
61
|
+
"fs-extra": "11.1.1",
|
|
62
|
+
"knex": "2.4.2",
|
|
63
63
|
"sqlstring": "2.3.3"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@mikro-orm/core": "^5.
|
|
66
|
+
"@mikro-orm/core": "^5.7.12"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@mikro-orm/core": "~6.0.0-dev.
|
|
69
|
+
"@mikro-orm/core": "~6.0.0-dev.90"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -3,7 +3,7 @@ import type { IQueryBuilder } from '../typings';
|
|
|
3
3
|
/**
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
|
-
export declare class ArrayCriteriaNode extends CriteriaNode {
|
|
7
|
-
process
|
|
8
|
-
willAutoJoin
|
|
6
|
+
export declare class ArrayCriteriaNode<T extends object> extends CriteriaNode<T> {
|
|
7
|
+
process(qb: IQueryBuilder<T>, alias?: string): any;
|
|
8
|
+
willAutoJoin(qb: IQueryBuilder<T>, alias?: string): any;
|
|
9
9
|
}
|