2025-07-26 20:22:16 +05:30
2025-07-26 18:34:56 +05:30
2025-07-26 18:34:56 +05:30
2025-07-29 21:45:32 +05:30
2025-07-26 21:45:09 +05:30
2025-07-26 18:34:56 +05:30
2025-07-26 18:34:56 +05:30
2025-07-26 18:34:56 +05:30
2025-07-26 18:34:56 +05:30
2025-07-26 18:34:56 +05:30

pgm - PostgreSQL query mapper

A simple ORM that will work on top of jackc/pgx db connection pool.

ORMs in go eco system tha I like

Why not to use ent?

ent is feature loadedm, you can easily define complex schemas, it has nice integration with graphQL. Very nice auto migration feature. Basically all the things you need in an ORM.

But to me its overkill for simple apps, I have seen ent related code is taking significatn space in app binary(feels bloated).

Why not to use sqlc?

sqlc is nice as well, but using it will start feeling that now your DB layer has its own Models and you need map you apps model with it or have no choice but to sart using DB layer models, which to me is not a good thing.

Things that I am not happy with

  • Auto migrations, at many points you will see ORM is either not providing or have a complex way to do the simple db schema related changes. Its like obscure db schema. you can simply tell and test it in sql query editor.

DB must be a in form sql statement that you see, fine tune and event run/test in sql query editor. Thers is lots of mature tools available in Go ecosystem, dbmate is one of them, its a nice tool to manage migrations, can be use in code or in cli

  • To much extra code is generated for various condition and scenarios that you may not use

  • Auto genearted models for select queries that will now force you to either use them or to map them to required model.

Okay, what to do now, plain old sql queries?

Yes and No may hybrid. Plain old sql queries are not bad, just have have few concerns

  • Change in schema are not detected well.
  • Sql in jection issue if not using parameterized queries.

We can address these issues using pgm, pgm cli will help to cream light weight DB schema go files that will help in writing sql queries this can help keeping eye on schema changes, goal is not to hard code table and table.column names

Generate pgm schema files

run following command to generate pgm schema files

go run code.patial.tech/go/pgm/cmd -o ./db ./schema.sql

It will create bunch of files and filders under ./db directory

package main
import "code.patial.tech/go/pgm"

type MyModel struct {
	ID string
	Email string
}

func main() {
	println("init pgx connection pool")
	pgm.InitPool(pgm.Config{
		ConnString: url,
	})

	// Select query with first record to scan
	// it is assuming that schema is already created and is in "db" package and has User table init
	var v MyModel
	err := db.User.Select(user.ID, user.Email).
		Where(user.Email.Like("anki%")).
		First(context.TODO(), &v.Email, &v.ID)
	if err != nil {
		println("error, ", err.Error())
		return
	}

	println("user email", v.Email)
}
Description
No description provided
Readme MIT 95 KiB
Languages
Go 99.5%
Makefile 0.5%