Skip to content
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

Open
JusticeN opened this issue Aug 4, 2022 · 7 comments
Open

Bud new model #238

JusticeN opened this issue Aug 4, 2022 · 7 comments
Labels
waiting for info We're waiting for a response

Comments

@JusticeN
Copy link

JusticeN commented Aug 4, 2022

Is there a plan to have a command for generating models and connecting it with database?

@Bishwas-py
Copy link

Yupp, that'll be a lot awesome...

@matthewmueller
Copy link
Contributor

matthewmueller commented Aug 7, 2022

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!

@matthewmueller matthewmueller added the waiting for info We're waiting for a response label Aug 7, 2022
@JusticeN
Copy link
Author

JusticeN commented Aug 9, 2022

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

@matthewmueller
Copy link
Contributor

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!

@Bishwas-py
Copy link

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?

@preslavrachev
Copy link

@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 sql.DB connection to all my controllers.

@matthewmueller
Copy link
Contributor

matthewmueller commented Jan 29, 2023

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 internal/postgres/postgres.go (untested):

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 *sql.DB. Then from the controllers, you can do:

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 internal/env/env.go (untested):

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for info We're waiting for a response
Projects
None yet
Development

No branches or pull requests

4 participants