Feat(modules-sdk,inventory,stock-location): modules isolated connection (#3329)
* feat: scoped container for modules
This commit is contained in:
1 parent
8e78c533c4
commit
77d46220c2
65 files changed
+1116
-1011
No files matched your search
@@ -1,9 +1,49 @@
|
||||
import {
|
||||
ConfigurableModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
LoaderOptions,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { DataSource, DataSourceOptions } from "typeorm"
|
||||
|
||||
import * as InventoryModels from "../models"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { asValue } from "awilix"
|
||||
|
||||
export default async (
|
||||
{ configModule }: LoaderOptions,
|
||||
moduleDeclaration?: ConfigurableModuleDeclaration
|
||||
): Promise<void> => {}
|
||||
{ options, container }: LoaderOptions,
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
): Promise<void> => {
|
||||
if (
|
||||
moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL &&
|
||||
moduleDeclaration.resources === MODULE_RESOURCE_TYPE.SHARED
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
const dbData = options?.database as Record<string, string>
|
||||
|
||||
if (!dbData) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
`Database config is not present at module config "options.database"`
|
||||
)
|
||||
}
|
||||
|
||||
const entities = Object.values(InventoryModels)
|
||||
const dataSource = new DataSource({
|
||||
type: dbData.database_type,
|
||||
url: dbData.database_url,
|
||||
database: dbData.database_database,
|
||||
extra: dbData.database_extra || {},
|
||||
schema: dbData.database_schema,
|
||||
entities,
|
||||
logging: dbData.database_logging,
|
||||
} as DataSourceOptions)
|
||||
|
||||
await dataSource.initialize()
|
||||
|
||||
container.register({
|
||||
manager: asValue(dataSource.manager),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { InternalModuleDeclaration, LoaderOptions } from "@medusajs/modules-sdk"
|
||||
|
||||
import {
|
||||
InventoryItemService,
|
||||
InventoryLevelService,
|
||||
ReservationItemService,
|
||||
} from "../services"
|
||||
|
||||
import { asClass } from "awilix"
|
||||
|
||||
export default async (
|
||||
{ container }: LoaderOptions,
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
): Promise<void> => {
|
||||
container.register({
|
||||
inventoryItemService: asClass(InventoryItemService).singleton(),
|
||||
inventoryLevelService: asClass(InventoryLevelService).singleton(),
|
||||
reservationItemService: asClass(ReservationItemService).singleton(),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from "./connection"
|
||||
export * from "./container"
|
||||
Reference in new issue
Block a user