Files
medusa-store/www/apps/book/app/advanced-development/modules/connection-loader/page.mdx
Shahed Nasser 327e446974 docs: general fixes and overall changes (#7258)
* editing halfway

* edited second half

* adjust starter steps

* fix build

* typo fix
2024-05-07 18:00:28 +02:00

49 lines
1.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export const metadata = {
title: `${pageNumber} Database Connection Loader`,
}
# {metadata.title}
In this document, youll learn about how to load the database connection in your module.
## How to Add a Database Connection Loader?
To ensure that your module's database operations use the Medusa application's database connection, export a database connection loader in your module's definition.
Medusa provides a utility function that creates the database connection loader for you.
For example:
```ts title="src/modules/hello/index.ts"
// other imports...
import { ModulesSdkUtils } from "@medusajs/utils"
import { MyCustom } from "./models/my-custom"
const pathToMigrations = __dirname + "/migrations"
// ...
const connectionLoader = ModulesSdkUtils
.mikroOrmConnectionLoaderFactory({
moduleName,
moduleModels: [MyCustom],
migrationsPath: pathToMigrations,
})
export default {
// ...
loaders: [
// ...
connectionLoader,
],
}
```
The `mikroOrmConnectionLoaderFactory` function accepts an object with the following properties:
- `moduleName`: The name of the module.
- `moduleModules`: An array of data models in the module.
- `migrationsPath`: The path to the migrations file.
You can now implement data-management methods as explained in the next chapters.