Skip to content

feat: Add register command for auto-generating model registry#1

Merged
beesaferoot merged 2 commits into
beesaferoot:mainfrom
DivineUX23:feature/register-command
Jul 11, 2025
Merged

feat: Add register command for auto-generating model registry#1
beesaferoot merged 2 commits into
beesaferoot:mainfrom
DivineUX23:feature/register-command

Conversation

@DivineUX23

Copy link
Copy Markdown
Contributor

Description:

This pull request introduces a new register command to the migration tool. This feature significantly improves the user experience by automating the creation and maintenance of the models_registry.go file.

The Problem

Previously, users had to manually create the models_registry.go file and add every GORM model to the ModelTypeRegistry map. This process was tedious, error-prone, and difficult to keep in sync as a project's models evolved.

The Solution

The new register command solves this problem by:

  • Scanning a specified directory (or the models/ directory by default) for .go files.
  • Using Go's Abstract Syntax Tree (AST) parser to find all structs that embed gorm.Model.
  • Dynamically generating a models_registry.go file populated with all discovered models.

This automation removes a manual step, reduces the chance of human error, and makes the tool much easier to adopt and use.

How to Use

To generate the model registry, a user can now simply run:

go run cmd/migration/main.go register [path/to/your/models]

Key Changes

  • migration/commands/register.go: Added the new register cobra command.
  • migration/commands/utils.go: Implemented the core logic (createModelRegisterFile, getModels, modelPerser) for finding model files, parsing them with AST, and generating the registry file content.
  • README.md: Updated the documentation to reflect the new, simplified workflow and explain how to use the register command.

This change makes the initial setup and ongoing maintenance for users of this library much simpler and more robust.

@beesaferoot beesaferoot self-requested a review July 11, 2025 18:29
@beesaferoot

Copy link
Copy Markdown
Owner

Thanks for the PR @DivineUX23 will have a look

@beesaferoot beesaferoot left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪄 This is really cool. Thank you. 👍🏾

I added a few comments

Comment thread example/models/estate.go Outdated
Comment thread example/models/models_registry.go
Comment thread migration/commands/utils.go Outdated
Comment thread README.md Outdated
@beesaferoot beesaferoot merged commit 6e4be99 into beesaferoot:main Jul 11, 2025
2 checks passed
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.

2 participants