Skip to content

Commit 1623aaf

Browse files
Merge branch 'issue-1442' of https://github.com/justvamp/docs.nestjs.com into justvamp-issue-1442
2 parents 51e8289 + 04753c5 commit 1623aaf

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

content/techniques/mongo.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ export class CatsService {
241241
}
242242
```
243243

244+
To inject a given `Connection` to a custom provider (for example, factory provider), use the `getConnectionToken()` function passing the name of the connection as an argument.
245+
246+
```typescript
247+
{
248+
provide: CatsService,
249+
useFactory: (catsConnection: Connection) => {
250+
return new CatsService(catsConnection);
251+
},
252+
inject: [getConnectionToken('cats')],
253+
}
254+
244255
#### Hooks (middleware)
245256

246257
Middleware (also called pre and post hooks) are functions which are passed control during execution of asynchronous functions. Middleware is specified on the schema level and is useful for writing plugins ([source](https://mongoosejs.com/docs/middleware.html)). Calling `pre()` or `post()` after compiling a model does not work in Mongoose. To register a hook **before** model registration, use the `forFeatureAsync()` method of the `MongooseModule` along with a factory provider (i.e., `useFactory`). With this technique, you can access a schema object, then use the `pre()` or `post()` method to register a hook on that schema. See example below:

content/techniques/sql.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,23 @@ export class AlbumsService {
593593
}
594594
```
595595

596+
It's also possible to inject any `Connection` to the providers:
597+
598+
```typescript
599+
@Module({
600+
providers: [
601+
{
602+
provide: AlbumsService,
603+
useFactory: (albumsConnection: Connection) => {
604+
return new AlbumsService(albumsConnection);
605+
},
606+
inject: [getConnectionToken('albumsConnection')],
607+
},
608+
],
609+
})
610+
export class AlbumsModule {}
611+
```
612+
596613
#### Testing
597614

598615
When it comes to unit testing an application, we usually want to avoid making a database connection, keeping our test suites independent and their execution process as fast as possible. But our classes might depend on repositories that are pulled from the connection instance. How do we handle that? The solution is to create mock repositories. In order to achieve that, we set up [custom providers](/fundamentals/custom-providers). Each registered repository is automatically represented by an `<EntityName>Repository` token, where `EntityName` is the name of your entity class.
@@ -1162,6 +1179,23 @@ export class AlbumsService {
11621179
}
11631180
```
11641181

1182+
It's also possible to inject any `Sequelize` instance to the providers:
1183+
1184+
```typescript
1185+
@Module({
1186+
providers: [
1187+
{
1188+
provide: AlbumsService,
1189+
useFactory: (albumsSequelize: Sequelize) => {
1190+
return new AlbumsService(albumsSequelize);
1191+
},
1192+
inject: [getConnectionToken('albumsConnection')],
1193+
},
1194+
],
1195+
})
1196+
export class AlbumsModule {}
1197+
```
1198+
11651199
#### Testing
11661200

11671201
When it comes to unit testing an application, we usually want to avoid making a database connection, keeping our test suites independent and their execution process as fast as possible. But our classes might depend on models that are pulled from the connection instance. How do we handle that? The solution is to create mock models. In order to achieve that, we set up [custom providers](/fundamentals/custom-providers). Each registered model is automatically represented by a `<ModelName>Model` token, where `ModelName` is the name of your model class.

0 commit comments

Comments
 (0)