dbx

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2025 License: MIT Imports: 8 Imported by: 0

README

DBX

Test

Wrappers around database/sql for Postgres, SQLite3 and MySQL.

This is not an ORM but a set of granular wrappers for devs that enjoy hand tailor SQL queries but don't want to get bogged down in tedious CRUD operations and mappings.

From the rawest resource to the finest:

  • The library code has only one public method:

    • dbx.Open(driver string, source string) *DB
  • DB, operates with raw SQL strings and a parameter list:

    • dbx.Open(driver string, source string) *DB
    • DB.Close() error
    • DB.Read(query string, params ...any) []maps[string]any
    • DB.Write(query string, params ...any) int64
  • Query, fluent query builder, used by other resources but can be used to assemble SQL queries but doesn't run them.

  • Entry, loads a SQL query from and into a native go map(s).

  • Record, has simply two methods:

    • Record.Load(id int64), load the record with id into the .Data map
    • Record.Save() bool, updates or creates the loaded record into the database.

    Internally it exposes the SQL row data via its .Data field. Its private .id field tracks if an UPDATE or an INSERT is required for a Save().

Todos

Add an embedded Model that understands its own parent struct fields and supports validation.

Examples

Using SQLite3 and a record:

package main

import (
    "github.com/jpedro/dbx"
)

func main() {
    db := dbx.Open(dbx.DriverSqlite3, "./example.db")
    record := db.Record("users")
    record.Data["name"] = "some record"
    record.Data["password"] = "initial"
    saved := record.Save()
    println("Saved:", saved)

    record.Data["password"] = "UPDATED"
    updated := record.Save()
    println("Updated:", updated)

    rows := db.Read("SELECT * FROM users")
    println("All users:", rows)
    db.Close()
}

Using Postgres and an entry:

package main

import (
    "github.com/jpedro/dbx"
)

func main() {
    db := dbx.Open(dbx.DriverPostgres, "postgres://tests:tests@127.0.0.1:5432/tests?sslmode=disable")
    entry := db.Entry("users")
    newId := entry.Create("users", map[string]any{
        "name": "some entry",
        "age":  456,
    })
    println("Created user:", newId)

    found := entry.Get(newId)
    println("Found entry:", found)

    rows := db.Read("SELECT * FROM users")
    println("All users:", rows)
    db.Close()
}

Documentation

Index

Constants

View Source
const (
	DriverPostgres string = "postgres"
	DriverMySQL    string = "mysql"
	DriverSqlite3  string = "sqlite3"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	Driver string
	// contains filtered or unexported fields
}

func Open

func Open(driver string, source string) *DB

func (*DB) Close

func (me *DB) Close() error

func (*DB) Entry

func (me *DB) Entry(table string) *Entry

func (*DB) Insert

func (me *DB) Insert(query string, params ...any) int64

func (*DB) Query

func (me *DB) Query(table string) *Query

func (*DB) Read

func (me *DB) Read(query string, params ...any) []map[string]any

func (*DB) Record

func (me *DB) Record(table string) *Record

func (*DB) Write

func (me *DB) Write(query string, params ...any) int64

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

func (*Entry) Create

func (me *Entry) Create(data map[string]any) int64

func (*Entry) Delete

func (me *Entry) Delete(id int64) int64

func (*Entry) Find

func (me *Entry) Find(criteria ...any) []map[string]any

func (*Entry) Get

func (me *Entry) Get(id int64) map[string]any

func (*Entry) Update

func (me *Entry) Update(id int64, data map[string]any) int64

type Query

type Query struct {
	// contains filtered or unexported fields
}

func (*Query) All

func (me *Query) All(args ...any) []map[string]any

func (*Query) Build

func (me *Query) Build() *Query

func (*Query) Delete

func (me *Query) Delete(values ...any) *Query

func (*Query) Exec

func (me *Query) Exec() int64

func (*Query) Insert

func (me *Query) Insert(values ...any) *Query

func (*Query) One

func (me *Query) One() map[string]any

func (*Query) Params

func (me *Query) Params() []any

func (*Query) Query

func (me *Query) Query() string

func (*Query) Select

func (me *Query) Select(fields ...string) *Query

func (*Query) Set

func (me *Query) Set(values ...any) *Query

func (*Query) Update

func (me *Query) Update(values ...any) *Query

func (*Query) Where

func (me *Query) Where(values ...any) *Query

type Record

type Record struct {
	Data map[string]any
	// contains filtered or unexported fields
}

func (*Record) Delete

func (me *Record) Delete() bool

func (*Record) Id

func (me *Record) Id() int64

func (*Record) Load

func (me *Record) Load(id int64) bool

func (*Record) Save

func (me *Record) Save() bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL