From e7e7f1c8007837eae4e87f3a872972158acd9702 Mon Sep 17 00:00:00 2001 From: Ahmed Wael Date: Tue, 27 Feb 2024 11:11:08 +0200 Subject: [PATCH 1/3] docs(techniques/mongo): added sessions section added a small section on how to start a new session using the connection object --- content/techniques/mongo.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/content/techniques/mongo.md b/content/techniques/mongo.md index b5fa206cd8..137341e70d 100644 --- a/content/techniques/mongo.md +++ b/content/techniques/mongo.md @@ -191,6 +191,14 @@ import { Connection } from 'mongoose'; export class CatsService { constructor(@InjectConnection() private connection: Connection) {} } +``` +### Sessions +To start a session with mongoose you shouldn't use `mongoose.startSession()` directly. Instead inject the connection using [@InjectConnection](http://localhost:4200/techniques/mongodb#connection), then use the connection object to start the session. + +```typescript +const session = await this.connection.startSession(); +session.startTransaction(); + ``` #### Multiple databases From 6443f682c982236af248ec47b6eca9257c3779bd Mon Sep 17 00:00:00 2001 From: Ahmed Wael Date: Tue, 27 Feb 2024 11:18:55 +0200 Subject: [PATCH 2/3] docs(techniques/mongo): I forgot a line ensuring all section have lines between them --- content/techniques/mongo.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/techniques/mongo.md b/content/techniques/mongo.md index 137341e70d..2fdb39011d 100644 --- a/content/techniques/mongo.md +++ b/content/techniques/mongo.md @@ -192,6 +192,7 @@ export class CatsService { constructor(@InjectConnection() private connection: Connection) {} } ``` + ### Sessions To start a session with mongoose you shouldn't use `mongoose.startSession()` directly. Instead inject the connection using [@InjectConnection](http://localhost:4200/techniques/mongodb#connection), then use the connection object to start the session. From c9d7e043bef37e94c03d0e0741546dd19e331f7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Thu, 17 Oct 2024 13:03:18 +0200 Subject: [PATCH 3/3] chore: improve wording --- content/techniques/mongo.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/content/techniques/mongo.md b/content/techniques/mongo.md index e35ba37e6b..d95fb56344 100644 --- a/content/techniques/mongo.md +++ b/content/techniques/mongo.md @@ -193,15 +193,30 @@ export class CatsService { } ``` -### Sessions -To start a session with mongoose you shouldn't use `mongoose.startSession()` directly. Instead inject the connection using [@InjectConnection](http://localhost:4200/techniques/mongodb#connection), then use the connection object to start the session. +#### Sessions + +To start a session with Mongoose, it's recommended to inject the database connection using `@InjectConnection` rather than calling `mongoose.startSession()` directly. This approach allows better integration with the NestJS dependency injection system, ensuring proper connection management. + +Here's an example of how to start a session: ```typescript -const session = await this.connection.startSession(); -session.startTransaction(); +import { InjectConnection } from '@nestjs/mongoose'; +import { Connection } from 'mongoose'; + +@Injectable() +export class CatsService { + constructor(@InjectConnection() private readonly connection: Connection) {} + async startTransaction() { + const session = await this.connection.startSession(); + session.startTransaction(); + // Your transaction logic here + } +} ``` +In this example, `@InjectConnection()` is used to inject the Mongoose connection into the service. Once the connection is injected, you can use `connection.startSession()` to begin a new session. This session can be used to manage database transactions, ensuring atomic operations across multiple queries. After starting the session, remember to commit or abort the transaction based on your logic. + #### Multiple databases Some projects require multiple database connections. This can also be achieved with this module. To work with multiple connections, first create the connections. In this case, connection naming becomes **mandatory**.