reflectorm

package module
v0.0.0-...-f1c514e Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: MIT Imports: 7 Imported by: 0

README

Reflect ORM

reflect-orm codecov

Minimal proof of concept for an object relational mapper that can map data from tables without having to interact with the database beforehand.

It works by providing a struct that describes what is believed to be the data in the table.

It's meant to be simple with minimal boilerplate code.


import (
    "fmt"
    "github.com/WolvenSpirit/reflectorm"
)

var db *sql.DB

func connectDB(user, pass, host, database, sslMode string) {
	var err error
	driverName := "postgres"
	url := fmt.Sprintf("%s://%s:%s@%s/%s?sslmode=%s", driverName, user, pass, host, database, sslMode)
	if db, err = sql.Open(driverName, url); err != nil {
		fmt.Println("sql.Open: ", err.Error())
	}
}

// Define your table data, the struct doesn't need to be comprehensive
type Users struct {
    Id          int64
    Name        string
    Email       string
    // ...
}

func init() {
    // ... init db with credentials and other logic
}

func main() {
    // We use 'users' as our table definition
    var users Users
    // To be explicit regarding what is returned we define the result beforehand
    var result []Users
    // Get the users from the table
    result = Get(db, users, nil)
    // That's it!
    fmt.Println(result)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T interface{}](db *sql.DB, model T, where []string) (rows []T, err error)

Get constructs the query from the provided definition struct, performs the query and attempts to call MapFields for each row returned.

func MapFields

func MapFields[T interface{}](rows *sql.Rows, v T, rawValues []interface{}) T

MapFields attempts to map each row data as an instance of type T

Types

type CacheResult

type CacheResult[T interface{}] struct {
	Result T
	Error  error
}

type JoinTable

type JoinTable[model interface{}] struct {
	Model model
	On    []string
}

type QueryOptions

type QueryOptions[model interface{}] struct {
	Where []string
	Join  JoinTable[model]
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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