AlexanderMatveev / dbmap

Tiny zero-dependency package to auto map struct fields to database columns and vice versa without ORM's.

Home Page:https://pkg.go.dev/github.com/AlexanderMatveev/dbmap

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DbMap

Tiny zero-dependency package to auto map struct fields to database columns and vice versa without ORM's.

Go Report Card

Usage

import (
    "github.com/AlexanderMatveev/dbmap"
    "github.com/Masterminds/squirrel"
)

type Entity struct {
    Id            int       `db:"id"`
    Name          string    `db:"name"`
    DateTimeField time.Time `db:"created_at"`
    FieldNotIdDb  int
    NullableField *int
}

var structColumns = dbmap.Columns(reflect.TypeOf(Entity{}))

func main(){
    var e Entity
    // ...
    squirrel.Select(structColumns...)
    // ...
    rows.Scan(dbmap.Scan(&e)...)
    // ...
    squirrel.Insert("table").Columns(structColumns...).Values(dbmap.Values(e)...)
    // ...
}

About

Tiny zero-dependency package to auto map struct fields to database columns and vice versa without ORM's.

https://pkg.go.dev/github.com/AlexanderMatveev/dbmap

License:MIT License


Languages

Language:Go 100.0%