feat(medusa): Modules initializer (#3352)
This commit is contained in:
@@ -3,7 +3,11 @@ import featureFlagLoader from "../loaders/feature-flags"
|
||||
import Logger from "../loaders/logger"
|
||||
import databaseLoader from "../loaders/database"
|
||||
import configModuleLoader from "../loaders/config"
|
||||
import getMigrations, { getModuleSharedResources } from "./utils/get-migrations"
|
||||
import getMigrations, {
|
||||
getModuleSharedResources,
|
||||
revertIsolatedModulesMigration,
|
||||
runIsolatedModulesMigration,
|
||||
} from "./utils/get-migrations"
|
||||
|
||||
const getDataSource = async (directory) => {
|
||||
const configModule = configModuleLoader(directory)
|
||||
@@ -34,16 +38,19 @@ const main = async function ({ directory }) {
|
||||
args.shift()
|
||||
args.shift()
|
||||
|
||||
const configModule = configModuleLoader(directory)
|
||||
const dataSource = await getDataSource(directory)
|
||||
|
||||
if (args[0] === "run") {
|
||||
await dataSource.runMigrations()
|
||||
await dataSource.destroy()
|
||||
await runIsolatedModulesMigration(configModule)
|
||||
Logger.info("Migrations completed.")
|
||||
process.exit()
|
||||
} else if (args[0] === "revert") {
|
||||
await dataSource.undoLastMigration({ transaction: "all" })
|
||||
await dataSource.destroy()
|
||||
await revertIsolatedModulesMigration(configModule)
|
||||
Logger.info("Migrations reverted.")
|
||||
process.exit()
|
||||
} else if (args[0] === "show") {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { sync as existsSync } from "fs-exists-cached"
|
||||
import { getConfigFile } from "medusa-core-utils"
|
||||
import { track } from "medusa-telemetry"
|
||||
import path from "path"
|
||||
import { ConnectionOptions, createConnection } from "typeorm"
|
||||
import { DataSource, DataSourceOptions } from "typeorm"
|
||||
|
||||
import loaders from "../loaders"
|
||||
import { handleConfigError } from "../loaders/config"
|
||||
@@ -73,12 +73,14 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
|
||||
extra: configModule.projectConfig.database_extra || {},
|
||||
migrations: coreMigrations.concat(moduleMigrations),
|
||||
logging: true,
|
||||
} as ConnectionOptions
|
||||
} as DataSourceOptions
|
||||
|
||||
const connection = await createConnection(connectionOptions)
|
||||
const connection = new DataSource(connectionOptions)
|
||||
|
||||
await connection.initialize()
|
||||
await connection.runMigrations()
|
||||
await connection.close()
|
||||
await connection.destroy()
|
||||
|
||||
Logger.info("Migrations completed.")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { registerModules } from "@medusajs/modules-sdk"
|
||||
import { MedusaModule, registerModules } from "@medusajs/modules-sdk"
|
||||
import fs from "fs"
|
||||
import { sync as existsSync } from "fs-exists-cached"
|
||||
import glob from "glob"
|
||||
@@ -96,7 +96,7 @@ function resolvePlugin(pluginName) {
|
||||
export function getInternalModules(configModule) {
|
||||
const modules = []
|
||||
|
||||
const moduleResolutions = registerModules(configModule)
|
||||
const moduleResolutions = registerModules(configModule.modules)
|
||||
|
||||
for (const moduleResolution of Object.values(moduleResolutions)) {
|
||||
if (
|
||||
@@ -200,7 +200,6 @@ export const getModuleMigrations = (configModule, isFlagEnabled) => {
|
||||
for (const loadedModule of loadedModules) {
|
||||
const mod = loadedModule.loadedModule
|
||||
|
||||
const isolatedMigrations = {}
|
||||
const moduleMigrations = (mod.migrations ?? [])
|
||||
.map((migration) => {
|
||||
if (
|
||||
@@ -218,7 +217,6 @@ export const getModuleMigrations = (configModule, isFlagEnabled) => {
|
||||
moduleDeclaration: loadedModule.moduleDeclaration,
|
||||
models: mod.models ?? [],
|
||||
migrations: moduleMigrations,
|
||||
externalMigrations: isolatedMigrations,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -249,3 +247,43 @@ export const getModuleSharedResources = (configModule, featureFlagsRouter) => {
|
||||
migrations,
|
||||
}
|
||||
}
|
||||
|
||||
export const runIsolatedModulesMigration = async (configModule) => {
|
||||
const moduleResolutions = registerModules(configModule.modules)
|
||||
|
||||
for (const moduleResolution of Object.values(moduleResolutions)) {
|
||||
if (
|
||||
!moduleResolution.resolutionPath ||
|
||||
moduleResolution.moduleDeclaration.scope !== "internal" ||
|
||||
moduleResolution.moduleDeclaration.resources !== "isolated"
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
await MedusaModule.migrateUp(
|
||||
moduleResolution.definition.key,
|
||||
moduleResolution.resolutionPath,
|
||||
moduleResolution.options
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export const revertIsolatedModulesMigration = async (configModule) => {
|
||||
const moduleResolutions = registerModules(configModule.modules)
|
||||
|
||||
for (const moduleResolution of Object.values(moduleResolutions)) {
|
||||
if (
|
||||
!moduleResolution.resolutionPath ||
|
||||
moduleResolution.moduleDeclaration.scope !== "internal" ||
|
||||
moduleResolution.moduleDeclaration.resources !== "isolated"
|
||||
) {
|
||||
continue
|
||||
}
|
||||
|
||||
await MedusaModule.migrateDown(
|
||||
moduleResolution.definition.key,
|
||||
moduleResolution.resolutionPath,
|
||||
moduleResolution.options
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user