Skip to content

Instantly share code, notes, and snippets.

@mhewedy
Created September 24, 2020 20:47
Show Gist options
  • Save mhewedy/a90cd7946907e9aabd04c873cb326c34 to your computer and use it in GitHub Desktop.
Save mhewedy/a90cd7946907e9aabd04c873cb326c34 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"database/sql"
"fmt"
"github.com/Fs02/rel"
"github.com/Fs02/rel/adapter/postgres"
"github.com/jackc/pgx/v4"
pgxs "github.com/jackc/pgx/v4/stdlib"
"time"
)
type employee struct {
ID int
Name string
CreatedAt time.Time
UpdatedAt time.Time
}
var (
db *sql.DB
host = "192.168.100.166"
port = 5432
user = "userdb"
password = "123456"
dbname = "userdb"
)
func main() {
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
c, err := pgx.ParseConfig(dsn)
if err != nil {
panic(err)
}
db = pgxs.OpenDB(*c)
adapter := postgres.New(db)
defer adapter.Close()
repo := rel.New(adapter)
var e = employee{Name: "Wael"}
if err = repo.Insert(context.Background(), &e); err != nil {
panic(err)
}
fmt.Println("employee ", e, "created")
var e2 = employee{Name: "Abbas"}
e2.ID = e.ID
time.Sleep(60 * time.Second)
if err = repo.Update(context.Background(), &e2); err != nil {
panic(err)
}
fmt.Println("employee ", e2, "updated")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment