-
-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bud new model #238
Comments
Yupp, that'll be a lot awesome... |
Hey folks, thanks for your interest in Bud! We're working on adding built-in model support, but in the meantime, you can do this today yourself. Conceptually, take a look at the Dependency Injection docs to see how you can bring models into your controllers. Then you can take a look at a real-world example using xo in this https://github.com/vito/bass-loop. Let me know if you have any questions! |
Thank you really good example. I was thinking about something like the ruby and rails model field generator from the cli. Or the jhipster one for generating Java spring boot apps. Or may be "bud" just have a different philosophy |
We definitely want to have a Rails-like model generator once we have model support. The philosophy is similar here. I hadn't heard of jhipster, I'll check that one out, thanks for sharing! |
We got the model? |
@matthewmueller I am a fan of SQLBoiler. I suppose it will work just the same, correct? I can simply generate my models and use the dependency injection to pass a |
Yep, that would work! You might just need to wrap it to tell Bud how to initialize it. I usually put these wrappers in internal, but it doesn't really matter. Perhaps in package postgres
import "database/sql"
func Open() (*DB, error) {
// Open handle to database like normal
return sql.Open("postgres", "dbname=fun user=abc")
}
type DB = sql.DB The type alias is to make sure dependency injection uses this file to initialize package users
import "app.com/internal/postgres"
type Controller struct {
DB *postgres.DB
}
func (c *Controller) Index(ctx context.Context) error {
// Query all users
users, err := models.Users().All(ctx, c.DB)
// ...
} You can also couple it with an environment parser like https://github.com/caarlos0/env to be able to set this config programmatically: import "app.com/internal/env"
func Open(env *env.Env) (*sql.DB, error) {
// Open handle to database like normal
return sql.Open(env.Database.Name, env.Database.URL)
} Then in say package env
import "https://github.com/caarlos0/env"
type Env struct {
Database Database
}
type Database struct {
Name string `env:"DATABASE_NAME"`
URL string `env:"DATABASE_URL"`
}
func Parse() (*Env, error) {
var e Env
if err := env.Parse(&e); err != nil {
return nil, err
}
return &e, nil
} |
Is there a plan to have a command for generating models and connecting it with database?
The text was updated successfully, but these errors were encountered: