Skip to content

Commit abdf6b4

Browse files
Merge pull request #3053 from micalevisk/docs-clarify-app-select-dynamic-modules
docs: mention about accessing dynamic modules on standalone mode
2 parents 3b129fc + 368656a commit abdf6b4

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

content/application-context.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ 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 from static modules
19+
20+
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` 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.
1921

2022
```typescript
2123
@@filename()
22-
const app = await NestFactory.createApplicationContext(AppModule);
2324
const tasksService = app.get(TasksService);
2425
```
2526

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.
27+
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. 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.
2728

2829
```typescript
2930
@@filename()
30-
const app = await NestFactory.createApplicationContext(AppModule);
3131
const tasksService = app.select(TasksModule).get(TasksService, { strict: true });
3232
```
3333

@@ -54,7 +54,31 @@ Following is a summary of the methods available for retrieving instance referenc
5454

5555
> 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.
5656
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:
57+
#### Retrieving providers from dynamic modules
58+
59+
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:
60+
61+
```typescript
62+
@@filename()
63+
export const dynamicConfigModule = ConfigModule.register({ folder: './config' });
64+
65+
@Module({
66+
imports: [dynamicConfigModule],
67+
})
68+
export class AppModule {}
69+
```
70+
71+
Then you can select that module later on:
72+
73+
```typescript
74+
@@filename()
75+
const configService = app.select(dynamicConfigModule).get(ConfigService, { strict: true });
76+
```
77+
78+
79+
#### Terminating phase
80+
81+
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:
5882

5983
```typescript
6084
@@filename()
@@ -66,6 +90,8 @@ async function bootstrap() {
6690
bootstrap();
6791
```
6892

93+
And as mentioned in the [Lifecycle events](./fundamentals/lifecycle-events.md) chapter, that will trigger lifecycle hooks.
94+
6995
#### Example
7096

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

0 commit comments

Comments
 (0)