Skip to content

Commit 471501f

Browse files
committed
docs: mention about accessing dynamic modules on standalone mode
1 parent 70ab91b commit 471501f

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

content/application-context.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@ To create a Nest standalone application, use the following construction:
1010
@@filename()
1111
async function bootstrap() {
1212
const app = await NestFactory.createApplicationContext(AppModule);
13-
// application logic...
13+
// your application logic here ...
1414
}
1515
bootstrap();
1616
```
1717

18-
The standalone application object allows you to obtain a reference to any instance registered within the Nest application. Let's imagine that we have a `TasksService` in the `TasksModule`. This class provides a set of methods that we want to call from within a CRON job.
18+
#### Retrieving providers for static modules
19+
20+
The standalone application object allows you to obtain a reference to any instance registered within the Nest application.
21+
Let's imagine that we have a `TasksService` provider in the `TasksModule` module that was imported by our `AppModule` module. This class provides a set of methods that we want to call from within a CRON job.
1922

2023
```typescript
2124
@@filename()
22-
const app = await NestFactory.createApplicationContext(AppModule);
25+
// ...
2326
const tasksService = app.get(TasksService);
2427
```
2528

26-
To access the `TasksService` instance we use the `get()` method. The `get()` method acts like a **query** that searches for an instance in each registered module. Alternatively, for strict context checking, pass an options object with the `strict: true` property. With this option in effect, you have to navigate through specific modules to obtain a particular instance from the selected context.
29+
To access the `TasksService` instance we use the `get()` method.
30+
The `get()` method acts like a **query** that searches for an instance in each registered module. You can pass any provider's token to it. Alternatively, for strict context checking, pass an options object with the `strict: true` property. With this option in effect, you have to navigate through specific modules to obtain a particular instance from the selected context.
2731

2832
```typescript
2933
@@filename()
30-
const app = await NestFactory.createApplicationContext(AppModule);
34+
// ...
3135
const tasksService = app.select(TasksModule).get(TasksService, { strict: true });
3236
```
3337

@@ -54,7 +58,32 @@ Following is a summary of the methods available for retrieving instance referenc
5458

5559
> info **Hint** In non-strict mode, the root module is selected by default. To select any other module, you need to navigate the modules graph manually, step by step.
5660
57-
If you want the node application to close after the script finishes (e.g., for a script running CRON jobs), add `await app.close()` to the end of your `bootstrap` function:
61+
#### Retrieving providers for dynamic modules
62+
63+
When dealing with [dynamic modules](./fundamentals/dynamic-modules.md), we should supply the same object that represents the registered dynamic module in the application to `app.select`. For example:
64+
65+
```typescript
66+
@@filename()
67+
export const dynamicConfigModule = ConfigModule.register({ folder: './config' });
68+
69+
@Module({
70+
imports: [dynamicConfigModule],
71+
})
72+
export class AppModule {}
73+
```
74+
75+
then youn can select that module later on:
76+
77+
```typescript
78+
@@filename()
79+
const configService = app.select(dynamicConfigModule).get(ConfigService, { strict: true });
80+
```
81+
82+
this is because we didn't registered the static version of `ConfigModule`, only the dynamic version.
83+
84+
#### Terminating phase
85+
86+
If you want the Node application to close after the script finishes (e.g., for a script running CRON jobs), you must call the `app.close()` method in the end of your `bootstrap` function like this:
5887

5988
```typescript
6089
@@filename()
@@ -66,6 +95,8 @@ async function bootstrap() {
6695
bootstrap();
6796
```
6897

98+
And as mentioned at [Lifecycle events](./fundamentals/lifecycle-events.md) chapter, that will trigger few lifecycle hooks.
99+
69100
#### Example
70101

71102
A working example is available [here](https://github.com/nestjs/nest/tree/master/sample/18-context).

0 commit comments

Comments
 (0)