https://github.com/cloudloyalty/db
Go PostgreSQL toolkit
https://github.com/cloudloyalty/db
db golang library postgresql
Last synced: 5 months ago
JSON representation
Go PostgreSQL toolkit
- Host: GitHub
- URL: https://github.com/cloudloyalty/db
- Owner: cloudloyalty
- Created: 2018-10-13T16:31:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-02T12:26:21.000Z (over 3 years ago)
- Last Synced: 2024-06-20T17:31:18.534Z (about 2 years ago)
- Topics: db, golang, library, postgresql
- Language: Go
- Homepage:
- Size: 36.1 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Go PostgreSQL Toolkit
=====================
*db.go* - query formatter
*migrate.go* - schema migration helpers
Migrations usage example:
```go
var migrations = []db.Migration{
{
1,
db.InitialMigration,
},
{
2,
`
CREATE TABLE test (
id BIGSERIAL NOT NULL PRIMARY KEY,
text TEXT
);
`,
},
}
dbh, err := sql.Open("postgres", config.DB.DSN)
if err != nil {
panic(err)
}
err = db.NewMigrate(dbh).Run(migrations)
if err != nil {
panic(err)
}
```
Formatter usage example:
```go
// run a query with param substitution
// in order to use db.ScanRowsIntoStruct() you have to format query result in JSON
rows, err := db.Query(ctx, dbh, `SELECT row_to_json(t.*) FROM test AS t WHERE id = :id`, db.Params{"id": 1})
if err != nil && err != sql.ErrNoRows {
panic(err)
}
// a struct for filling up with select data
struct testModel {
id int
}
var tests []testModel
// iterate over results and build slice of structs
for rows.Next() {
var test testModel
if err := db.ScanRowsIntoStruct(rows, &test); err != nil {
panic(err)
}
tests = append(tests, test)
}
```