stephenafamo / bob

SQL query builder and ORM/Factory generator for Go with support for PostgreSQL, MySQL and SQLite
https://bob.stephenafamo.com
MIT License
696 stars 36 forks source link

Update documentation for code generation #182

Open rkrishnasanka opened 4 months ago

rkrishnasanka commented 4 months ago

It would be great if there was a full example for:

  1. Code Generation Config with all the options and explicitly listed so that we don't need to piece it together
  2. Full example of using the generated code (CRUD). The documentation is a little ambiguous right now in terms of how I should establish the database connections, etc.
stephenafamo commented 4 months ago

I would appreciate a PR on this, or other documentation improvements in general.

jacobmolby commented 4 months ago

@rkrishnasanka Simply call bob.New(db)

rkrishnasanka commented 4 months ago

Yeah I figured it out shortly after commenting. I missed the link for bob executors. I'll do a PR once I'm done implementing stuff.

rkrishnasanka commented 4 months ago

Is there a discord for this ? I feel like its better to get some direct feedback. The query parameters are confusing. For example:

    queuesView := psql.NewView[*models.Queue]("core", "queues")
    // Check if the queue already exists
    row, err := queuesView.Query(
        context.Background(),
        bob.NewDB(db),
        models.SelectWhere.Queues.QueueName.EQ(queuename),
    ).One()

In this scenario, I get:

"pq: missing FROM-clause entry for table \"queues\"" bob golang

as the error

jacobmolby commented 4 months ago

If you are using the code generation, the view/table is already created for you.

Simply use:

queue, err := models.Queues.Query(
        context.Background(),
        bob.NewDB(db),
        models.SelectWhere.Queues.QueueName.EQ(queuename),
    ).One()

//queue is of type models.Queues now
rkrishnasanka commented 4 months ago

@jacobmolby So turns out that there are a couple more errors happening here that made this complicated #187 . so thats why I've just been dealing with bugs.

pcriv commented 1 month ago

@rkrishnasanka I couldn't find any documentationion of how to use the CLI to generate the code, could you maybe share how you did it?

rkrishnasanka commented 1 month ago

Here's the command I use

go run github.com/stephenafamo/bob/gen/bobgen-psql@latest -c ./config/bobgen.yaml

Here's the config


psql:
  dsn: "postgres://postgres:@localhost:5432/postgres?sslmode=disable"

  schemas: ["application","core"]
  shared_schema: "public"