Created
February 26, 2020 13:09
-
-
Save bastianccm/108b4feeb95320dff438024e7e2b1a0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package db | |
| import ( | |
| "log" | |
| "flamingo.me/dingo" | |
| "github.com/jinzhu/gorm" | |
| "github.com/spf13/cobra" | |
| ) | |
| type Model interface{} | |
| type Module struct{} | |
| func (*Module) Configure(injector *dingo.Injector) { | |
| injector.Bind(new(gorm.DB)).ToProvider(func() *gorm.DB { | |
| db, err := gorm.Open("sqlite3", "test.db") | |
| if err != nil { | |
| panic(err) | |
| } | |
| return db | |
| }).In(dingo.ChildSingleton) | |
| injector.BindMulti(new(cobra.Command)).ToProvider(func(models []Model, db *gorm.DB) *cobra.Command { | |
| return &cobra.Command{ | |
| Use: "migrate", | |
| Run: func(cmd *cobra.Command, args []string) { | |
| for _, model := range models { | |
| log.Printf("Migrating %T", model) | |
| db.AutoMigrate(model) | |
| } | |
| }, | |
| } | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment