https://github.com/ziflex/dbx
Lightweight DB management toolkit
https://github.com/ziflex/dbx
database sql
Last synced: 3 months ago
JSON representation
Lightweight DB management toolkit
- Host: GitHub
- URL: https://github.com/ziflex/dbx
- Owner: ziflex
- License: mit
- Created: 2019-11-10T19:30:53.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-02T21:07:00.000Z (over 2 years ago)
- Last Synced: 2025-01-23T04:14:44.726Z (4 months ago)
- Topics: database, sql
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dbx
This Go library provides a light abstraction on top of the sql package from the Go standard library.
It provides convenient functions for working with SQL databases.[API](https://godoc.org/github.com/ziflex/dbx)
## Installation
```bash
go get github.com/ziflex/dbx@latest
```## Quick Start
```go
package mainimport (
"context"
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
"github.com/ziflex/dbx"
)func getUserNames(ctx dbx.Context) []string {
// Create a new query
executor := ctx.Executor()
// Execute the query
rows, err := executor.Query("SELECT name FROM users")
if err != nil {
log.Fatal(err)
}
// Iterate over the rows
var names []string
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
log.Fatal(err)
}
names = append(names, name)
}
return names
}func main() {
// Connect to a database
db, err := sql.Open("postgres", "postgres://user:password@localhost/dbname?sslmode=disable")
if err != nil {
fmt.Println(err)
return
}// Wrap the *sql.DB object with dbx.NewDatabase
dbxDB := dbx.New(db)
userNames := getUserNames(dbxDB.Context(context.Background()))
fmt.Println(userNames)
}
```## Context
```go
package mainimport (
"context"
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
"github.com/ziflex/dbx"
)func getUserNames(ctx context.Context) []string {
// Get dbx context from the context
dbxContext := dbx.FromContext(ctx)
if dbxContext == nil {
log.Fatal("dbx context is not found")
}
// Create a new query
executor := dbxContext.Executor()
// Execute the query
rows, err := executor.Query("SELECT name FROM users")
if err != nil {
log.Fatal(err)
}
// Iterate over the rows
var names []string
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
log.Fatal(err)
}
names = append(names, name)
}
return names
}func main() {
// Connect to a database
db, err := sql.Open("postgres", "postgres://user:password@localhost/dbname?sslmode=disable")if err != nil {
fmt.Println(err)
return
}// Wrap the *sql.DB object with dbx.NewDatabase
dbxDB := dbx.New(db)
// Create a new context
ctx := context.Background()
// Add dbx context to the context
ctx = dbxDB.Context(ctx)
userNames := getUserNames(ctx)
fmt.Println(userNames)
}
```## Transactions
```go
package mainimport (
"context"
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
"github.com/ziflex/dbx"
)func insertUser(ctx dbx.Context, name string) {
// Create a new query
executor := ctx.Executor()
// Execute the query
_, err := executor.Exec("INSERT INTO users (name) VALUES ($1)", name)
if err != nil {
log.Fatal(err)
}
}func main() {
// Connect to a database
db, err := sql.Open("postgres", "postgres://user:password@localhost/dbname?sslmode=disable")if err != nil {
fmt.Println(err)
return
}// Wrap the *sql.DB object with dbx.NewDatabase
dbxDB := dbx.New(db)
err = dbx.Transaction(context.Background(), dbxDB, func(ctx dbx.Context) error {
insertUser(ctx, "John")
insertUser(ctx, "Doe")
return nil
})if err != nil {
fmt.Println(err)
return
}
}
```> Transactions are reusable by default. Using ``dbx.Transaction`` multiple times within the same transaction will not create a new transaction.
> To disable this behavior, use ``dbx.WithNewTransaction`` option.```go
package mainimport (
"context"
"database/sql"
"fmt"
"log"_ "github.com/lib/pq"
"github.com/ziflex/dbx"
)func insertUser(ctx dbx.Context, name string) {
// Create a new query
executor := ctx.Executor()// Execute the query
_, err := executor.Exec("INSERT INTO users (name) VALUES ($1)", name)
if err != nil {
log.Fatal(err)
}
}func insertCompany(ctx dbx.Context, name string) {
// Create a new query
executor := ctx.Executor()// Execute the query
_, err := executor.Exec("INSERT INTO companies (name) VALUES ($1)", name)
if err != nil {
log.Fatal(err)
}
}func main() {
// Connect to a database
db, err := sql.Open("postgres", "postgres://user:password@localhost/dbname?sslmode=disable")if err != nil {
fmt.Println(err)
return
}// Wrap the *sql.DB object with dbx.NewDatabase
dbxDB := dbx.New(db)
err = dbx.Transaction(context.Background(), dbxDB, func(ctx dbx.Context) error {
insertUser(ctx, "John")
insertUser(ctx, "Doe")
return dbx.Transaction(ctx, dbxDB, func(ctx dbx.Context) error {
insertCompany(ctx, "Google")
insertCompany(ctx, "Apple")
return nil
}, dbx.WithNewTransaction())
})
if err != nil {
fmt.Println(err)
return
}
}
```