Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/olegfomenko/pg-dao
Data Access Object for PostgreSQL for Distributed Lab projects.
https://github.com/olegfomenko/pg-dao
distributed-lab go postgresql
Last synced: 9 days ago
JSON representation
Data Access Object for PostgreSQL for Distributed Lab projects.
- Host: GitHub
- URL: https://github.com/olegfomenko/pg-dao
- Owner: olegfomenko
- Created: 2021-10-08T12:28:51.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-02-04T13:38:14.000Z (10 months ago)
- Last Synced: 2024-06-20T16:32:08.992Z (5 months ago)
- Topics: distributed-lab, go, postgresql
- Language: Go
- Homepage:
- Size: 1.58 MB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# pg-dao
Data Access Object for PostgreSQL for Distributed Lab projects.
## Usage
```go
package mainimport (
"errors"pg "github.com/olegfomenko/pg-dao"
"gitlab.com/distributed_lab/kit/kv"
)type Entry struct {
Id int64 `db:"id" structs:"-"`
Name string `db:"name" structs:"name"`
}func main() {
// Loading config
cfg := config.New(kv.MustFromEnv())// Creating DAO instance for table "entries"
dao := pg.NewDAO(cfg.DB(), "entries")// Saving record
// id - saved entry index
id, err := dao.Create(Entry{
Name: "First Entry",
})// Cloning DAO for new session
dao = dao.Clone()// Cleaning queries in DAO in current session
dao = dao.New()// Updating entry
err = dao.UpdateWhereID(id).UpdateColumn("name", "New First Entry").Update()// Getting entry by id
var entry Entry
// if ok is false - there is no entry with provided id
ok, err := dao.New().FilterByID(id).Get(&entry)// Getting entry by field
ok, err = dao.New().FilterByColumn("name", "New First Entry").Get(&entry)// Deleting entry
err = dao.New().DeleteWhereID(id).Delete()// Creating transaction
err = dao.Clone().TransactionSerializable(
func(q pg.DAO) error {
ok, err := q.FilterByID(id).Get(&entry)
if err != nil {
// rollback transaction
return err
}if !ok {
// entry does not exist
return errors.New("not found")
}err = q.New().UpdateWhereID(id).UpdateColumn("name", "Updated First Entry").Update()
if err != nil {
// rollback transaction
return err
}// commit transaction
return nil
},
)
}```