docs: added troubleshooting guides + improvements (#11927)
* docs: added troubleshooting guides + improvements * build fixes
This commit is contained in:
@@ -28,8 +28,8 @@ So, to run database queries in a service:
|
||||
For example, in your service, add the following methods:
|
||||
|
||||
export const methodsHighlight = [
|
||||
["12", "getCount", "Retrieves the number of records in `my_custom` using the `count` method."],
|
||||
["19", "getCountSql", "Retrieves the number of records in `my_custom` using the `execute` method."]
|
||||
["13", "getCount", "Retrieves the number of records in `my_custom` using the `count` method."],
|
||||
["20", "getCountSql", "Retrieves the number of records in `my_custom` using the `execute` method."]
|
||||
]
|
||||
|
||||
```ts highlights={methodsHighlight}
|
||||
@@ -38,7 +38,8 @@ import {
|
||||
InjectManager,
|
||||
MedusaContext,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/knex"
|
||||
import { Context } from "@medusajs/framework/types"
|
||||
import { EntityManager } from "@mikro-orm/knex"
|
||||
|
||||
class BlogModuleService {
|
||||
// ...
|
||||
@@ -46,19 +47,19 @@ class BlogModuleService {
|
||||
@InjectManager()
|
||||
async getCount(
|
||||
@MedusaContext() sharedContext?: Context<EntityManager>
|
||||
): Promise<number> {
|
||||
return await sharedContext.manager.count("my_custom")
|
||||
): Promise<number | undefined> {
|
||||
return await sharedContext?.manager?.count("my_custom")
|
||||
}
|
||||
|
||||
@InjectManager()
|
||||
async getCountSql(
|
||||
@MedusaContext() sharedContext?: Context<EntityManager>
|
||||
): Promise<number> {
|
||||
const data = await sharedContext.manager.execute(
|
||||
const data = await sharedContext?.manager?.execute(
|
||||
"SELECT COUNT(*) as num FROM my_custom"
|
||||
)
|
||||
|
||||
return parseInt(data[0].num)
|
||||
return parseInt(data?.[0].num || 0)
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -115,8 +116,8 @@ class BlogModuleService {
|
||||
},
|
||||
@MedusaContext() sharedContext?: Context<EntityManager>
|
||||
): Promise<any> {
|
||||
const transactionManager = sharedContext.transactionManager
|
||||
await transactionManager.nativeUpdate(
|
||||
const transactionManager = sharedContext?.transactionManager
|
||||
await transactionManager?.nativeUpdate(
|
||||
"my_custom",
|
||||
{
|
||||
id: input.id,
|
||||
@@ -127,7 +128,7 @@ class BlogModuleService {
|
||||
)
|
||||
|
||||
// retrieve again
|
||||
const updatedRecord = await transactionManager.execute(
|
||||
const updatedRecord = await transactionManager?.execute(
|
||||
`SELECT * FROM my_custom WHERE id = '${input.id}'`
|
||||
)
|
||||
|
||||
@@ -178,10 +179,22 @@ For example, the `update` method could be changed to the following:
|
||||
|
||||
```ts
|
||||
// other imports...
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { Context } from "@medusajs/framework/types"
|
||||
import { EntityManager } from "@mikro-orm/knex"
|
||||
|
||||
class BlogModuleService {
|
||||
// ...
|
||||
@InjectTransactionManager()
|
||||
protected async update_(
|
||||
// ...
|
||||
): Promise<any> {
|
||||
// ...
|
||||
}
|
||||
@InjectManager()
|
||||
async update(
|
||||
input: {
|
||||
@@ -192,12 +205,14 @@ class BlogModuleService {
|
||||
) {
|
||||
const newData = await this.update_(input, sharedContext)
|
||||
|
||||
await sendNewDataToSystem(newData)
|
||||
// example method that sends data to another system
|
||||
await this.sendNewDataToSystem(newData)
|
||||
|
||||
return newData
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this case, only the `update_` method is wrapped in a transaction. The returned value `newData` holds the committed result, which can be used for other operations, such as passed to a `sendNewDataToSystem` method.
|
||||
|
||||
### Using Methods in Transactional Methods
|
||||
@@ -208,6 +223,11 @@ For example:
|
||||
|
||||
```ts
|
||||
// other imports...
|
||||
import {
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { Context } from "@medusajs/framework/types"
|
||||
import { EntityManager } from "@mikro-orm/knex"
|
||||
|
||||
class BlogModuleService {
|
||||
@@ -343,7 +363,7 @@ class BlogModuleService {
|
||||
return updatedRecord
|
||||
},
|
||||
{
|
||||
transaction: sharedContext.transactionManager,
|
||||
transaction: sharedContext?.transactionManager,
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -382,6 +402,12 @@ The second parameter of the `baseRepository_.transaction` method is an object of
|
||||
```ts highlights={[["16"]]}
|
||||
// other imports...
|
||||
import { EntityManager } from "@mikro-orm/knex"
|
||||
import {
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { Context } from "@medusajs/framework/types"
|
||||
import { EntityManager } from "@mikro-orm/knex"
|
||||
|
||||
class BlogModuleService {
|
||||
// ...
|
||||
@@ -398,7 +424,7 @@ class BlogModuleService {
|
||||
// ...
|
||||
},
|
||||
{
|
||||
transaction: sharedContext.transactionManager,
|
||||
transaction: sharedContext?.transactionManager,
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -414,6 +440,12 @@ class BlogModuleService {
|
||||
|
||||
```ts highlights={[["19"]]}
|
||||
// other imports...
|
||||
import {
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { Context } from "@medusajs/framework/types"
|
||||
import { EntityManager } from "@mikro-orm/knex"
|
||||
import { IsolationLevel } from "@mikro-orm/core"
|
||||
|
||||
class BlogModuleService {
|
||||
@@ -442,6 +474,14 @@ class BlogModuleService {
|
||||
- If `transaction` is provided and this is disabled, the manager in `transaction` is re-used.
|
||||
|
||||
```ts highlights={[["16"]]}
|
||||
// other imports...
|
||||
import {
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { Context } from "@medusajs/framework/types"
|
||||
import { EntityManager } from "@mikro-orm/knex"
|
||||
|
||||
class BlogModuleService {
|
||||
// ...
|
||||
@InjectTransactionManager()
|
||||
|
||||
Reference in New Issue
Block a user