Feat(modules-sdk,inventory,stock-location): modules isolated connection (#3329)
* feat: scoped container for modules
This commit is contained in:
@@ -1 +0,0 @@
|
||||
export const CONNECTION_NAME = "inventory_connection"
|
||||
@@ -1,12 +1,14 @@
|
||||
import ConnectionLoader from "./loaders/connection"
|
||||
import InventoryService from "./services/inventory"
|
||||
import loadConnection from "./loaders/connection"
|
||||
import loadContainer from "./loaders/container"
|
||||
|
||||
import migrations from "./migrations"
|
||||
import * as InventoryModels from "./models"
|
||||
import * as SchemaMigration from "./migrations/schema-migrations/1665748086258-inventory_setup"
|
||||
import InventoryService from "./services/inventory"
|
||||
|
||||
import { ModuleExports } from "@medusajs/modules-sdk"
|
||||
|
||||
const service = InventoryService
|
||||
const migrations = [SchemaMigration]
|
||||
const loaders = [ConnectionLoader]
|
||||
const loaders = [loadContainer, loadConnection]
|
||||
const models = Object.values(InventoryModels)
|
||||
|
||||
const moduleDefinition: ModuleExports = {
|
||||
|
||||
@@ -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"
|
||||
@@ -0,0 +1,3 @@
|
||||
import * as setup from "./schema-migrations/1665748086258-inventory_setup"
|
||||
|
||||
export default [setup]
|
||||
+1
-43
@@ -1,46 +1,4 @@
|
||||
import { ConfigModule } from "@medusajs/medusa"
|
||||
import {
|
||||
createConnection,
|
||||
ConnectionOptions,
|
||||
MigrationInterface,
|
||||
QueryRunner,
|
||||
} from "typeorm"
|
||||
|
||||
import { CONNECTION_NAME } from "../../config"
|
||||
|
||||
export const up = async ({ configModule }: { configModule: ConfigModule }) => {
|
||||
const connection = await createConnection({
|
||||
name: CONNECTION_NAME,
|
||||
type: configModule.projectConfig.database_type,
|
||||
url: configModule.projectConfig.database_url,
|
||||
extra: configModule.projectConfig.database_extra || {},
|
||||
schema: configModule.projectConfig.database_schema,
|
||||
migrations: [inventorySetup1665748086258],
|
||||
logging: true,
|
||||
} as ConnectionOptions)
|
||||
|
||||
await connection.runMigrations()
|
||||
await connection.close()
|
||||
}
|
||||
|
||||
export const down = async ({
|
||||
configModule,
|
||||
}: {
|
||||
configModule: ConfigModule
|
||||
}) => {
|
||||
const connection = await createConnection({
|
||||
name: CONNECTION_NAME,
|
||||
type: configModule.projectConfig.database_type,
|
||||
url: configModule.projectConfig.database_url,
|
||||
extra: configModule.projectConfig.database_extra || {},
|
||||
schema: configModule.projectConfig.database_schema,
|
||||
migrations: [inventorySetup1665748086258],
|
||||
logging: true,
|
||||
} as ConnectionOptions)
|
||||
|
||||
await connection.undoLastMigration({ transaction: "all" })
|
||||
await connection.close()
|
||||
}
|
||||
import { MigrationInterface, QueryRunner } from "typeorm"
|
||||
|
||||
export class inventorySetup1665748086258 implements MigrationInterface {
|
||||
name = "inventorySetup1665748086258"
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import {
|
||||
ConfigurableModuleDeclaration,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { InternalModuleDeclaration } from "@medusajs/modules-sdk"
|
||||
|
||||
import {
|
||||
CreateInventoryItemInput,
|
||||
@@ -22,18 +19,20 @@ import {
|
||||
} from "@medusajs/medusa"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
|
||||
import { EntityManager } from "typeorm"
|
||||
import {
|
||||
InventoryItemService,
|
||||
InventoryLevelService,
|
||||
ReservationItemService,
|
||||
} from "./"
|
||||
import { EntityManager } from "typeorm"
|
||||
|
||||
type InjectedDependencies = {
|
||||
manager: EntityManager
|
||||
eventBusService: IEventBusService
|
||||
inventoryItemService: InventoryItemService
|
||||
inventoryLevelService: InventoryLevelService
|
||||
reservationItemService: ReservationItemService
|
||||
}
|
||||
|
||||
export default class InventoryService
|
||||
extends TransactionBaseService
|
||||
implements IInventoryService
|
||||
@@ -44,34 +43,23 @@ export default class InventoryService
|
||||
protected readonly inventoryLevelService_: InventoryLevelService
|
||||
|
||||
constructor(
|
||||
{ eventBusService, manager }: InjectedDependencies,
|
||||
{
|
||||
eventBusService,
|
||||
manager,
|
||||
inventoryItemService,
|
||||
inventoryLevelService,
|
||||
reservationItemService,
|
||||
}: InjectedDependencies,
|
||||
options?: unknown,
|
||||
moduleDeclaration?: ConfigurableModuleDeclaration
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
|
||||
if (moduleDeclaration?.resources !== MODULE_RESOURCE_TYPE.SHARED) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
"At the moment this module can only be used with shared resources"
|
||||
)
|
||||
}
|
||||
|
||||
this.eventBusService_ = eventBusService
|
||||
this.inventoryItemService_ = new InventoryItemService({
|
||||
eventBusService,
|
||||
manager,
|
||||
})
|
||||
this.inventoryLevelService_ = new InventoryLevelService({
|
||||
eventBusService,
|
||||
manager,
|
||||
})
|
||||
this.reservationItemService_ = new ReservationItemService({
|
||||
eventBusService,
|
||||
manager,
|
||||
inventoryLevelService: this.inventoryLevelService_,
|
||||
})
|
||||
this.inventoryItemService_ = inventoryItemService
|
||||
this.inventoryLevelService_ = inventoryLevelService
|
||||
this.reservationItemService_ = reservationItemService
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user