fix: make v2 with modules run (#6636)

This commit is contained in:
Sebastian Rindom
2024-03-09 16:09:06 +01:00
committed by GitHub
parent c2d56ca12b
commit a838ebae1b
10 changed files with 43 additions and 39 deletions

View File

@@ -55,7 +55,7 @@
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "16.3.1",
"dotenv": "^16.4.5",
"knex": "2.4.2"
}
}

View File

@@ -70,7 +70,7 @@
"core-js": "^3.6.5",
"cors": "^2.8.5",
"cross-spawn": "^7.0.3",
"dotenv": "^16.0.3",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"express-session": "^1.17.3",
"fs-exists-cached": "^1.0.0",

View File

@@ -61,12 +61,10 @@ export const DELETE = async (
res: MedusaResponse
) => {
const id = req.params.id
const manager = req.scope.resolve("manager")
const deleteCampaigns = deleteCampaignsWorkflow(req.scope)
const { errors } = await deleteCampaigns.run({
input: { ids: [id] },
context: { manager },
throwOnError: false,
})

View File

@@ -10,7 +10,6 @@ import {
import { AdminPostPromotionsPromotionReq } from "../validators"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { UpdateApplicationMethodDTO } from "@medusajs/types"
import { UpdatePromotionDTO } from "@medusajs/types"
export const GET = async (
@@ -58,12 +57,10 @@ export const DELETE = async (
res: MedusaResponse
) => {
const id = req.params.id
const manager = req.scope.resolve("manager")
const deletePromotions = deletePromotionsWorkflow(req.scope)
const { errors } = await deletePromotions.run({
input: { ids: [id] },
context: { manager },
throwOnError: false,
})

View File

@@ -8,12 +8,14 @@ import getMigrations, {
import {
ContainerRegistrationKeys,
createMedusaContainer,
MedusaV2Flag,
promiseAll,
} from "@medusajs/utils"
import configModuleLoader from "../loaders/config"
import databaseLoader from "../loaders/database"
import featureFlagLoader from "../loaders/feature-flags"
import Logger from "../loaders/logger"
import { loadMedusaApp } from "../loaders/medusa-app"
import { migrateMedusaApp, loadMedusaApp } from "../loaders/medusa-app"
import pgConnectionLoader from "../loaders/pg-connection"
const getDataSource = async (directory) => {
@@ -70,15 +72,31 @@ const main = async function ({ directory }) {
args.shift()
args.shift()
const featureFlagRouter = featureFlagLoader(configModule)
const configModule = configModuleLoader(directory)
const dataSource = await getDataSource(directory)
if (args[0] === "run") {
await dataSource.runMigrations()
await dataSource.destroy()
await runIsolatedModulesMigration(configModule)
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
const container = createMedusaContainer()
const pgConnection = await pgConnectionLoader({ configModule, container })
container.register({
[ContainerRegistrationKeys.CONFIG_MODULE]: asValue(configModule),
[ContainerRegistrationKeys.LOGGER]: asValue(Logger),
[ContainerRegistrationKeys.PG_CONNECTION]: asValue(pgConnection),
featureFlagRouter: asValue(featureFlagRouter),
})
await migrateMedusaApp(
{ configModule, container },
{ registerInContainer: false }
)
} else {
await dataSource.runMigrations()
await dataSource.destroy()
await runIsolatedModulesMigration(configModule)
await runLinkMigrations(directory)
await runLinkMigrations(directory)
}
process.exit()
Logger.info("Migrations completed.")

View File

@@ -3,7 +3,11 @@ import {
ModulesDefinition,
} from "@medusajs/modules-sdk"
import { MODULE_RESOURCE_TYPE } from "@medusajs/types"
import { ContainerRegistrationKeys, isString } from "@medusajs/utils"
import {
ContainerRegistrationKeys,
isString,
MedusaV2Flag,
} from "@medusajs/utils"
import { asValue } from "awilix"
import { Express, NextFunction, Request, Response } from "express"
import { createMedusaContainer } from "medusa-core-utils"
@@ -77,9 +81,7 @@ async function loadLegacyModulesEntities(configModules, container) {
}
}
async function loadMedusaV2({ directory, expressApp }) {
const configModule = loadConfig(directory)
async function loadMedusaV2({ configModule, featureFlagRouter, expressApp }) {
const container = createMedusaContainer()
// Add additional information to context of request
@@ -91,12 +93,9 @@ async function loadMedusaV2({ directory, expressApp }) {
next()
})
const featureFlagRouter = featureFlagsLoader(configModule, Logger)
const pgConnection = await pgConnectionLoader({ container, configModule })
container.register({
[ContainerRegistrationKeys.MANAGER]: asValue(dataSource.manager),
[ContainerRegistrationKeys.LOGGER]: asValue(Logger),
featureFlagRouter: asValue(featureFlagRouter),
[ContainerRegistrationKeys.CONFIG_MODULE]: asValue(configModule),
@@ -142,11 +141,13 @@ export default async ({
app: Express
pgConnection: unknown
}> => {
if (process.env.MEDUSA_FF_MEDUSA_V2 == "true") {
return await loadMedusaV2({ directory: rootDirectory, expressApp })
}
const configModule = loadConfig(rootDirectory)
const featureFlagRouter = featureFlagsLoader(configModule, Logger)
track("FEATURE_FLAGS_LOADED")
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
return await loadMedusaV2({ configModule, featureFlagRouter, expressApp })
}
const container = createMedusaContainer()
container.register(
@@ -163,9 +164,6 @@ export default async ({
next()
})
const featureFlagRouter = featureFlagsLoader(configModule, Logger)
track("FEATURE_FLAGS_LOADED")
container.register({
[ContainerRegistrationKeys.LOGGER]: asValue(Logger),
featureFlagRouter: asValue(featureFlagRouter),

View File

@@ -51,7 +51,6 @@
"@medusajs/modules-sdk": "^1.12.5",
"@medusajs/types": "^1.11.9",
"@medusajs/utils": "^1.11.2",
"@medusajs/workflows-sdk": "workspace:^",
"@mikro-orm/core": "5.9.7",
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",

View File

@@ -55,7 +55,7 @@
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "16.3.1",
"dotenv": "^16.4.5",
"knex": "2.4.2"
}
}

View File

@@ -3,17 +3,12 @@ import { isObject } from "./is-object"
/**
* In most casees, JSON.parse(JSON.stringify(obj)) is enough to deep copy an object.
* But in some cases, it's not enough. For example, if the object contains a function or a proxy, it will be lost after JSON.parse(JSON.stringify(obj)).
* Furthermore, structuredClone is not present in all environments, such as with jest so we need to use a custom deepCopy function.
*
* @param obj
*/
export function deepCopy<T extends Record<any, any> = Record<any, any>>(
obj: T | T[]
): T | T[] {
if (typeof structuredClone != "undefined") {
return structuredClone(obj)
}
if (obj === null || typeof obj !== "object") {
return obj
}

View File

@@ -8122,7 +8122,7 @@ __metadata:
"@mikro-orm/postgresql": 5.9.7
awilix: ^8.0.0
cross-env: ^5.2.1
dotenv: 16.3.1
dotenv: ^16.4.5
jest: ^29.6.3
knex: 2.4.2
medusa-test-utils: ^1.1.40
@@ -8499,7 +8499,7 @@ __metadata:
cors: ^2.8.5
cross-env: ^5.2.1
cross-spawn: ^7.0.3
dotenv: ^16.0.3
dotenv: ^16.4.5
express: ^4.18.2
express-session: ^1.17.3
fs-exists-cached: ^1.0.0
@@ -8683,7 +8683,6 @@ __metadata:
"@medusajs/modules-sdk": ^1.12.5
"@medusajs/types": ^1.11.9
"@medusajs/utils": ^1.11.2
"@medusajs/workflows-sdk": "workspace:^"
"@mikro-orm/cli": 5.9.7
"@mikro-orm/core": 5.9.7
"@mikro-orm/migrations": 5.9.7
@@ -8775,7 +8774,7 @@ __metadata:
"@mikro-orm/postgresql": 5.9.7
awilix: ^8.0.0
cross-env: ^5.2.1
dotenv: 16.3.1
dotenv: ^16.4.5
jest: ^29.6.3
knex: 2.4.2
medusa-test-utils: ^1.1.40
@@ -9194,7 +9193,7 @@ __metadata:
languageName: unknown
linkType: soft
"@medusajs/workflows-sdk@^0.1.2, @medusajs/workflows-sdk@^0.1.3, @medusajs/workflows-sdk@workspace:^, @medusajs/workflows-sdk@workspace:packages/workflows-sdk":
"@medusajs/workflows-sdk@^0.1.2, @medusajs/workflows-sdk@^0.1.3, @medusajs/workflows-sdk@workspace:packages/workflows-sdk":
version: 0.0.0-use.local
resolution: "@medusajs/workflows-sdk@workspace:packages/workflows-sdk"
dependencies:
@@ -25870,7 +25869,7 @@ __metadata:
languageName: node
linkType: hard
"dotenv@npm:16.4.5":
"dotenv@npm:16.4.5, dotenv@npm:^16.4.5":
version: 16.4.5
resolution: "dotenv@npm:16.4.5"
checksum: 48d92870076832af0418b13acd6e5a5a3e83bb00df690d9812e94b24aff62b88ade955ac99a05501305b8dc8f1b0ee7638b18493deb6fe93d680e5220936292f