You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/recipes/mikroorm.md
+35-61Lines changed: 35 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,41 +1,44 @@
1
1
### MikroORM
2
2
3
-
This recipe is here to help users getting started with MikroORM in Nest. MikroORM is the TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. It is a great alternative to TypeORM and migration from TypeORM should be fairly easy. The complete documentation on MikroORM can be found [here](https://mikro-orm.io/docs).
3
+
This recipe is here to help users get started with MikroORM in Nest. MikroORM is the TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. It is a great alternative to TypeORM and migration from TypeORM should be fairly easy. The complete documentation on MikroORM can be found [here](https://mikro-orm.io/docs).
4
4
5
-
> info **info**`@mikro-orm/nestjs` is a third party package and is not managed by the NestJS core team. Please, report any issues found with the library in the [appropriate repository](https://github.com/mikro-orm/nestjs).
5
+
> info **info**`@mikro-orm/nestjs` is a third party package and is not managed by the NestJS core team. Please report any issues found with the library in the [appropriate repository](https://github.com/mikro-orm/nestjs).
6
6
7
7
#### Installation
8
8
9
9
Easiest way to integrate MikroORM to Nest is via [`@mikro-orm/nestjs` module](https://github.com/mikro-orm/nestjs).
10
10
Simply install it next to Nest, MikroORM and underlying driver:
11
11
12
12
```bash
13
-
$ npm i @mikro-orm/core @mikro-orm/nestjs @mikro-orm/mysql # for mysql/mariadb
13
+
$ npm i @mikro-orm/core @mikro-orm/nestjs @mikro-orm/sqlite
14
14
```
15
15
16
16
MikroORM also supports `postgres`, `sqlite`, and `mongo`. See the [official docs](https://mikro-orm.io/docs/usage-with-sql/) for all drivers.
17
17
18
18
Once the installation process is completed, we can import the `MikroOrmModule` into the root `AppModule`.
19
19
20
20
```typescript
21
+
import { SqliteDriver } from'@mikro-orm/sqlite';
22
+
21
23
@Module({
22
24
imports: [
23
25
MikroOrmModule.forRoot({
24
26
entities: ['./dist/entities'],
25
27
entitiesTs: ['./src/entities'],
26
28
dbName: 'my-db-name.sqlite3',
27
-
type: 'sqlite',
29
+
driver: SqliteDriver,
28
30
}),
29
31
],
30
32
controllers: [AppController],
31
33
providers: [AppService],
32
34
})
33
-
exportclassAppModule {}
35
+
exportclassAppModule {
36
+
}
34
37
```
35
38
36
39
The `forRoot()` method accepts the same configuration object as `init()` from the MikroORM package. Check [this page](https://mikro-orm.io/docs/configuration) for the complete configuration documentation.
37
40
38
-
Alternatively we can [configure the CLI](https://mikro-orm.io/docs/installation#setting-up-the-commandline-tool) by creating a configuration file `mikro-orm.config.ts` and then call the `forRoot()` without any arguments. This won't work when you use a build tools that use tree shaking.
41
+
Alternatively we can [configure the CLI](https://mikro-orm.io/docs/installation#setting-up-the-commandline-tool) by creating a configuration file `mikro-orm.config.ts` and then call the `forRoot()` without any arguments.
39
42
40
43
```typescript
41
44
@Module({
@@ -47,12 +50,25 @@ Alternatively we can [configure the CLI](https://mikro-orm.io/docs/installation#
47
50
exportclassAppModule {}
48
51
```
49
52
50
-
Afterward, the `EntityManager` will be available to inject across entire project (without importing any module elsewhere).
53
+
But this won't work when you use a build tools that use tree shaking, for that it is better to provide the config explicitly:
54
+
55
+
```typescript
56
+
importconfigfrom'./mikro-orm.config'; // your ORM config
57
+
58
+
@Module({
59
+
imports: [
60
+
MikroOrmModule.forRoot(config),
61
+
],
62
+
...
63
+
})
64
+
exportclassAppModule {}
65
+
```
66
+
67
+
Afterward, the `EntityManager` will be available to inject across the entire project (without importing any module elsewhere).
51
68
52
69
```ts
53
-
import { MikroORM } from'@mikro-orm/core';
54
-
// Import EntityManager from your driver package or `@mikro-orm/knex`
55
-
import { EntityManager } from'@mikro-orm/mysql';
70
+
// Import everytyhing from your driver package or `@mikro-orm/knex`
MikroORM supports the repository design pattern. For every entity we can create a repository. Read the complete documentation on repositories [here](https://mikro-orm.io/docs/repositories). To define which repositories should be registered in the current scope you can use the `forFeature()` method. For example, in this way:
86
+
MikroORM supports the repository design pattern. For every entity, we can create a repository. Read the complete documentation on repositories [here](https://mikro-orm.io/docs/repositories). To define which repositories should be registered in the current scope you can use the `forFeature()` method. For example, in this way:
71
87
72
88
> info **info** You should **not** register your base entities via `forFeature()`, as there are no
73
89
> repositories for those. On the other hand, base entities need to be part of the list in `forRoot()` (or in the ORM config in general).
@@ -106,28 +122,18 @@ export class PhotoService {
106
122
107
123
#### Using custom repositories
108
124
109
-
When using custom repositories, we can get around the need for `@InjectRepository()`
110
-
decorator by naming our repositories the same way as `getRepositoryToken()` method do:
> info **info**`autoLoadEntities` option was added in v4.1.0
149
-
150
154
Manually adding entities to the entities array of the connection options can be
151
155
tedious. In addition, referencing entities from the root module breaks application
152
156
domain boundaries and causes leaking implementation details to other parts of the
@@ -207,15 +211,13 @@ export class Book {
207
211
208
212
#### Request scoped handlers in queues
209
213
210
-
> info **info**`@UseRequestContext()` decorator was added in v4.1.0
211
-
212
214
As mentioned in the [docs](https://mikro-orm.io/docs/identity-map), we need a clean state for each request. That is handled automatically thanks to the `RequestContext` helper registered via middleware.
213
215
214
216
But middlewares are executed only for regular HTTP request handles, what if we need
215
217
a request scoped method outside of that? One example of that is queue handlers or
216
218
scheduled tasks.
217
219
218
-
We can use the `@UseRequestContext()` decorator. It requires you to first inject the
220
+
We can use the `@CreateRequestContext()` decorator. It requires you to first inject the
219
221
`MikroORM` instance to current context, it will be then used to create the context
220
222
for you. Under the hood, the decorator will register new request context for your
221
223
method and execute it inside the context.
@@ -225,43 +227,14 @@ method and execute it inside the context.
225
227
exportclassMyService {
226
228
constructor(privatereadonlyorm:MikroORM) {}
227
229
228
-
@UseRequestContext()
230
+
@CreateRequestContext()
229
231
async doSomething() {
230
232
// this will be executed in a separate context
231
233
}
232
234
}
233
235
```
234
236
235
-
#### Using `AsyncLocalStorage` for request context
236
-
237
-
By default, the `domain` api is used in the `RequestContext` helper. Since `@mikro-orm/[email protected]`,
238
-
you can use the new `AsyncLocalStorage` too, if you are on up to date node version:
> warning **Note** As the name suggests, this decorator always creates new context, as opposed to its alternative `@EnsureRequestContext` that only creates it if it's already not inside another one.
265
238
266
239
#### Testing
267
240
@@ -272,7 +245,8 @@ The `@mikro-orm/nestjs` package exposes `getRepositoryToken()` function that ret
272
245
providers: [
273
246
PhotoService,
274
247
{
275
-
provide: getRepositoryToken(Photo),
248
+
// or when you have a custom repository: `provide: PhotoRepository`
0 commit comments