Skip to content

fix(medusa): prevent plugin:db:generate crash when model files export…#14718

Open
shubhamchoudhary-2003 wants to merge 2 commits intomedusajs:developfrom
Eryx-Innovations-Lab:fix/plugin-db-generate-enum-crash
Open

fix(medusa): prevent plugin:db:generate crash when model files export…#14718
shubhamchoudhary-2003 wants to merge 2 commits intomedusajs:developfrom
Eryx-Innovations-Lab:fix/plugin-db-generate-enum-crash

Conversation

@shubhamchoudhary-2003
Copy link

@shubhamchoudhary-2003 shubhamchoudhary-2003 commented Feb 6, 2026

fix(medusa): prevent plugin:db:generate crash when model files export enums

MetadataStorage.getMetadataFromDecorator() returns a truthy EntityMetadata for any input with a .name property, causing TypeScript enums to be incorrectly treated as database entities. This crashes MikroORM when it tries to process them during migration generation.

Replace the MetadataStorage check with typeof === "function", which correctly identifies MikroORM class entities while rejecting enum objects. This matches the proven pattern already used in load-internal.ts cleanupResources for the regular db:generate path.

Summary

What — Fix entity detection in plugin:db:generate to prevent TypeScript enums from being treated as MikroORM entities.

Why — When model files export enums, MikroORM crashes during migration generation because enums are mistakenly identified as database entities. MetadataStorage.getMetadataFromDecorator() returns truthy for anything with a .name property, which includes compiled TypeScript enums.

How — Replaced the MetadataStorage.getMetadataFromDecorator() check with typeof potentialEntity === "function" in the entity filter inside getEntitiesForModule() (packages/medusa/src/commands/plugin/db/generate.ts). Class constructors are functions; enum objects are not — so this correctly filters out enums while keeping real entities. This aligns with the same pattern already used in load-internal.ts cleanupResources.

Testing — Create a plugin with a model file that exports both a MikroORM entity and a TypeScript enum, then run npx medusa plugin:db:generate. Before this fix the command crashes; after it generates migrations correctly.


Examples

// packages/my-plugin/src/models/my-entity.ts

// This enum export previously caused plugin:db:generate to crash
export enum MyStatus {
  ACTIVE = "active",
  INACTIVE = "inactive",
}

@Entity()
export class MyEntity {
  @PrimaryKey()
  id: string

  @Enum(() => MyStatus)
  status: MyStatus
}
# Reproduce
npx medusa plugin:db:generate

# Before fix: MikroORM crashes trying to process the enum as an entity
# After fix: migration generated successfully

Checklist

  • I have added a changeset for this PR
    • Every non-breaking change should be marked as a patch
    • To add a changeset, run yarn changeset and follow the prompts
  • The changes are covered by relevant tests
  • I have verified the code works as intended locally
  • I have linked the related issue(s) if applicable

Additional Context

The regular (non-plugin) db:generate path in load-internal.ts already uses the same typeof === "function" pattern in its cleanupResources function. This PR aligns the plugin path with that existing approach.


Note

Low Risk
Small, localized change to entity filtering for the plugin migration generator; primary risk is inadvertently skipping edge-case entity exports that aren’t functions.

Overview
Prevents plugin:db:generate from crashing when plugin model files export non-entity values (e.g., TypeScript enums) by tightening entity detection.

In commands/plugin/db/generate.ts, the entity filter no longer relies on MetadataStorage.getMetadataFromDecorator() and instead treats only DML entities or function exports (class constructors) as MikroORM entities, avoiding misclassification of enum objects during migration generation.

Written by Cursor Bugbot for commit b2bc415. This will update automatically on new commits. Configure here.

… enums

MetadataStorage.getMetadataFromDecorator() returns a truthy EntityMetadata
for any input with a .name property, causing TypeScript enums to be
incorrectly treated as database entities. This crashes MikroORM when it
tries to process them during migration generation.

Replace the MetadataStorage check with typeof === "function", which
correctly identifies MikroORM class entities while rejecting enum objects.
This matches the proven pattern already used in load-internal.ts
cleanupResources for the regular db:generate path.
@shubhamchoudhary-2003 shubhamchoudhary-2003 requested a review from a team as a code owner February 6, 2026 11:17
@changeset-bot
Copy link

changeset-bot bot commented Feb 6, 2026

⚠️ No Changeset found

Latest commit: b2bc415

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Feb 6, 2026

@shubhamchoudhary-2003 is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant