Feat(modules-sdk,inventory,stock-location): modules isolated connection (#3329)

* feat: scoped container for modules
This commit is contained in:
Carlos R. L. Rodrigues
2023-03-15 12:09:45 -03:00
committed by GitHub
parent 8e78c533c4
commit 77d46220c2
65 changed files with 1116 additions and 1011 deletions
-1
View File
@@ -1 +0,0 @@
export const CONNECTION_NAME = "inventory_connection"
+7 -5
View File
@@ -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 = {
+44 -4
View File
@@ -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(),
})
}
+2
View File
@@ -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,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"
+16 -28
View File
@@ -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
}
/**