Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/netologist/frodao
Golang DAO library
https://github.com/netologist/frodao
dao dao-design-pattern golang golang-library postgres sql
Last synced: 3 days ago
JSON representation
Golang DAO library
- Host: GitHub
- URL: https://github.com/netologist/frodao
- Owner: netologist
- Created: 2022-05-01T13:40:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-02T03:54:09.000Z (over 1 year ago)
- Last Synced: 2023-09-05T02:28:40.356Z (about 1 year ago)
- Topics: dao, dao-design-pattern, golang, golang-library, postgres, sql
- Language: Go
- Homepage:
- Size: 49.8 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Frodao
The golang DAO library. Your best friend on the road like Frodo Baggins.
- Soft delete support.
- Postgresql support.
- Mysql support.## Example Schema
```sql
CREATE TABLE users (
id SERIAL PRIMARY KEY NOT NULL,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
address VARCHAR(100) NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted BOOLEAN NOT NULL DEFAULT FALSE
);
```## Table & DAO Interface
```go
package frodaoimport (
"context""github.com/netologist/frodao/tableid"
)type Table[T tableid.Constraint] struct {
Record `db:"-" goqu:"skipinsert,skipupdate"`ID ID[T] `db:"id" goqu:"skipinsert,skipupdate"`
CreatedAt time.Time `db:"created_at" goqu:"skipinsert,skipupdate"`
UpdatedAt time.Time `db:"updated_at" goqu:"skipinsert"`
Deleted bool `db:"deleted" goqu:"skipinsert,skipupdate"`
}type DAO[T Record, I tableid.Constraint] interface {
Create(ctx context.Context, record *T) (*T, error)
Update(ctx context.Context, record *T) error
Delete(ctx context.Context, id ID[I]) error
FindByID(ctx context.Context, id ID[I]) (*T, error)
GetTableName() string
}
```## Custom Table and DAO Definition
```go
import (
"github.com/netologist/frodao"
"github.com/netologist/frodao/nullable"
"github.com/netologist/frodao/tableid"
)type UserTable struct {
frodao.Table[tableid.Int]Username string `db:"username"`
Password string `db:"password"`
Address nullable.Type[string] `db:"address"`
}
``````go
import (
"context""github.com/doug-martin/goqu/v9"
"github.com/netologist/frodao/drivers/postgres"
"github.com/netologist/frodao/tableid"
)func NewUserDAO() *UserDAO {
return &UserDAO{
DAO: postgres.DAO[UserTable, tableid.Int]{
TableName: "users",
},
}
}type UserDAO struct {
postgres.DAO[UserTable]
}func (d *UserDAO) FindByUsername(ctx context.Context, username string) ([]*UserTable, error) {
return d.FindByQuery(ctx, d.SelectQuery().Where(goqu.Ex{"username": username}).Limit(1))
}
```