dvbcrud

package module
v0.1.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2022 License: MIT Imports: 4 Imported by: 0

README

dvbcrud-go

Simple and generic wrapper for github.com/jmoiron/sqlx that handle common CRUD queries as well as conversion between rows and struct types. It relies on formatting and reflection to achieve this.

This module is useful for rapidly integrating databases in your application, especially as part of a microservice architecture.

Usage

Below is a simple example on how to use dvbcrud.

package example

import (
    "fmt"
    "github.com/Dekamik/dvbcrud-go"
    "github.com/jmoiron/sqlx"
    "time"
)

type User struct {
    ID        uint64    `db:"user_id"`
    Name      string    `db:"name"`
    Birthdate time.Time `db:"birthdate"`
}

func main() {
    // All errors are ignored for brevity
    DB, _ := sqlx.Connect("mssql", "datasource")
    _ = DB.Ping()
    userRepo, _ := dvbcrud.NewSql[User](DB, "Users", "UserId")

    seed := []User{
        {
            Name:      "Winston",
            Birthdate: time.Date(1984, time.April, 3, 12, 59, 3, 0, time.UTC),
        },
        {
            Name:      "Julia",
            Birthdate: time.Date(1985, time.April, 3, 12, 59, 3, 0, time.UTC),
        },
    }

    for _, user := range seed {
        _ = userRepo.Create(user)
    }

    results, _ := userRepo.ReadAll()

    for _, result := range results {
        fmt.Printf("Hello, %s!\n", result.Name)
    }
}

Documentation

Index

Constants

View Source
const (
	Columns parameterType = iota
	Values
)

Variables

This section is empty.

Functions

This section is empty.

Types

type SQLDialect

type SQLDialect int

SQLDialect denotes the different dialects which define placeholders differently.

const (
	MySQL SQLDialect = iota
	PostgreSQL
	Oracle
	SQLite
	ODBC
	MariaDB
)

type SQLRepository

type SQLRepository[T any] struct {
	// contains filtered or unexported fields
}

SQLRepository handles CRUD queries to a table in an SQL database. T is the struct type that will be mapped against the table rows.

func New

func New[T any](db *sqlx.DB, config SQLRepositoryConfig) (*SQLRepository[T], error)

New creates and returns a new SQLRepository.

func (SQLRepository[T]) Create

func (r SQLRepository[T]) Create(model T) error

Create inserts the values in model into a new row in the table.

func (SQLRepository[T]) Delete

func (r SQLRepository[T]) Delete(id any) error

Delete removes the row whose ID matches id.

func (SQLRepository[T]) Read

func (r SQLRepository[T]) Read(id any) (*T, error)

Read fetches a row from the table whose ID matches id.

func (SQLRepository[T]) ReadAll

func (r SQLRepository[T]) ReadAll() ([]T, error)

ReadAll fetches all rows from the table.

func (SQLRepository[T]) Update

func (r SQLRepository[T]) Update(id any, model T) error

Update updates the row in the table, whose ID matches id, with the data found in model.

type SQLRepositoryConfig

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

type StructParser

type StructParser interface {
	// ParseFieldNames TODO: write docs
	ParseFieldNames(typ reflect.Type) ([]string, error)

	// ParseProperties reads the struct type T and returns its fields
	// and values as two slices. The slices are guaranteed to match indices.
	//
	// Separating the properties into fields and values slices is required
	// when formatting and preparing statements.
	//
	// Specifying idFieldName filters out that field in the resulting slices,
	// which is necessary in INSERTS and UPDATES.
	ParseProperties(model any, idFieldName string) ([]string, []any, error)
}

Jump to

Keyboard shortcuts

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