feat(utils): define file config (#13283)

** What
 - Allow auto-loaded Medusa files to export a config object.
 - Currently supports isDisabled to control loading.
 - new instance `FeatureFlag` exported by `@medusajs/framework/utils`
 - `feature-flags` is now a supported folder for medusa projects, modules, providers and plugins. They will be loaded and added to `FeatureFlag`

** Why
 - Enables conditional loading of routes, migrations, jobs, subscribers, workflows, and other files based on feature flags.

```ts
// /src/feature-flags

import { FlagSettings } from "@medusajs/framework/feature-flags"

const CustomFeatureFlag: FlagSettings = {
  key: "custom_feature",
  default_val: false,
  env_key: "FF_MY_CUSTOM_FEATURE",
  description: "Enable xyz",
}

export default CustomFeatureFlag
```

```ts
// /src/modules/my-custom-module/migration/Migration20250822135845.ts

import { FeatureFlag } from "@medusajs/framework/utils"

export class Migration20250822135845 extends Migration {
  override async up(){ }
  override async down(){ }
}

defineFileConfig({
  isDisabled: () => !FeatureFlag.isFeatureEnabled("custom_feature")
})
```
This commit is contained in:
Carlos R. L. Rodrigues
2025-08-26 09:22:30 -03:00
committed by GitHub
parent 4cda412243
commit e413cfefc2
183 changed files with 1089 additions and 605 deletions

View File

@@ -1,10 +1,11 @@
export * from "./mikro-orm/base-entity"
export * from "./mikro-orm/big-number-field"
export * from "./mikro-orm/custom-db-migrator"
export * from "./mikro-orm/decorators/searchable"
export * from "./mikro-orm/mikro-orm-create-connection"
export * from "./mikro-orm/mikro-orm-free-text-search-filter"
export * from "./mikro-orm/mikro-orm-repository"
export * from "./mikro-orm/mikro-orm-soft-deletable-filter"
export * from "./mikro-orm/mikro-orm-serializer"
export * from "./mikro-orm/base-entity"
export * from "./mikro-orm/mikro-orm-soft-deletable-filter"
export * from "./mikro-orm/utils"
export * from "./mikro-orm/decorators/searchable"
export * from "./utils"

View File

@@ -0,0 +1,59 @@
import { Constructor } from "@medusajs/types"
import { MikroORM, Utils } from "@mikro-orm/core"
import {
Migrator as BaseMigrator,
Migration,
UmzugMigration,
} from "@mikro-orm/migrations"
import { isFileDisabled, isFileSkipped } from "../../common/define-file-config"
import { dynamicImport } from "../../common/dynamic-import"
export class CustomDBMigrator extends BaseMigrator {
static register(orm: MikroORM): void {
orm.config.registerExtension(
"@mikro-orm/migrator",
() => new CustomDBMigrator(orm.em as any)
)
}
resolve(params) {
require(params.path)
if (isFileDisabled(params.path)) {
return {
name: "Noop",
up: () => {},
down: () => {},
} as any
}
const $this = this as any
const createMigrationHandler = async (method) => {
const migration = await Utils.dynamicImport(params.path)
const MigrationClass = Object.values(
migration
)[0] as Constructor<Migration>
const instance = new MigrationClass($this.driver, $this.config)
await $this.runner.run(instance, method)
}
return {
name: $this.storage.getMigrationName(params.name),
up: () => createMigrationHandler("up"),
down: () => createMigrationHandler("down"),
}
}
async getPendingMigrations(): Promise<UmzugMigration[]> {
const pending = await super.getPendingMigrations()
// Filter out migrations that are disabled by file config
return pending.filter(async (pendingFile: UmzugMigration) => {
const migration = await dynamicImport(pendingFile.path!)
if (isFileSkipped(migration)) {
return false
}
return true
})
}
}

View File

@@ -3,6 +3,7 @@ import { Filter as MikroORMFilter } from "@mikro-orm/core"
import { TSMigrationGenerator } from "@mikro-orm/migrations"
import { isString, retryExecution, stringifyCircular } from "../../common"
import { normalizeMigrationSQL } from "../utils"
import { CustomDBMigrator } from "./custom-db-migrator"
type FilterDef = Parameters<typeof MikroORMFilter>[0]
@@ -107,6 +108,7 @@ export async function mikroOrmCreateConnection(
false
),
},
extensions: [CustomDBMigrator],
// We don't want to do any DB checks when establishing the connection. This happens once when creating the pg_connection, and it can happen again explicitly if necessary.
connect: false,
ensureDatabase: false,