fix(medusa): prevent plugin:db:generate crash when model files export…#14718
fix(medusa): prevent plugin:db:generate crash when model files export…#14718shubhamchoudhary-2003 wants to merge 4 commits intomedusajs:developfrom
Conversation
… 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.
🦋 Changeset detectedLatest commit: 6aedc6b The changes in this PR will be included in the next version bump. This PR includes changesets to release 76 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@shubhamchoudhary-2003 is attempting to deploy a commit to the medusajs Team on Vercel. A member of the Team first needs to authorize it. |
|
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
|
@carlos-r-l-rodrigues any update |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
1d5e0a9 to
9aa2231
Compare
|
You have used all of your free Bugbot PR reviews. To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial. |
NicolasGorga
left a comment
There was a problem hiding this comment.
Only a changeset is missing. Should have same message as the PR title :)
NicolasGorga
left a comment
There was a problem hiding this comment.
Thanks for the contribution Shubham!
|
Thank you for your contribution! After reviewing this PR, we need a few things addressed before we can move forward: Required changes:
The fix itself looks correct and aligns with the |
NicolasGorga
left a comment
There was a problem hiding this comment.
Hey @shubhamchoudhary-2003 could you add tests? See for example

fix(medusa): prevent plugin:db:generate crash when model files export enums
MetadataStorage.getMetadataFromDecorator()returns a truthyEntityMetadatafor any input with a.nameproperty, causing TypeScript enums to be incorrectly treated as database entities. This crashes MikroORM when it tries to process them during migration generation.Replace the
MetadataStoragecheck withtypeof === "function", which correctly identifies MikroORM class entities while rejecting enum objects. This matches the proven pattern already used inload-internal.tscleanupResourcesfor the regulardb:generatepath.Summary
What — Fix entity detection in
plugin:db:generateto 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.nameproperty, which includes compiled TypeScript enums.How — Replaced the
MetadataStorage.getMetadataFromDecorator()check withtypeof potentialEntity === "function"in the entity filter insidegetEntitiesForModule()(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 inload-internal.tscleanupResources.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
Checklist
yarn changesetand follow the promptsAdditional Context
The regular (non-plugin)
db:generatepath inload-internal.tsalready uses the sametypeof === "function"pattern in itscleanupResourcesfunction. This PR aligns the plugin path with that existing approach.Note
Medium Risk
Changes entity/resource detection for migrations and module loading; if
MetadataStorage.PATH_SYMBOLisn’t present in some valid entities, they may be skipped and migrations could be incomplete, but the change is small and localized.Overview
Prevents
plugin:db:generatefrom crashing when model files export non-entity values by tightening the entity filter to only accept DML entities or exports marked with MikroORMMetadataStorage.PATH_SYMBOL.Aligns the module auto-loader’s
cleanupResourceslogic with the samePATH_SYMBOLcheck so only real entities/services are kept when scanning module directories.Written by Cursor Bugbot for commit 1d5e0a9. This will update automatically on new commits. Configure here.