norm

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 3 Imported by: 1

README

NORM

test Go Report Card Go Reference

NORM is Not an ORM.

It does:

  • provide a unified CRUD API for your queries;
  • use generics for your types;
  • not generate SQL migrations;
  • not generate queries from structs;
  • not manage transactions.

Drivers

  • SQL
  • MongoDB (TODO)

Examples

SQL
View of the model list
package main

import (
    "context"
    "database/sql"
    
    "github.com/WinPooh32/norm"
    normsql "github.com/WinPooh32/norm/driver/sql"
    "github.com/lib/pq"
)

type Model struct {
    ID        string    `db:"id"`
    FieldA    string    `db:"field_a"`
    FieldB    string    `db:"field_b"`
    FieldC    int       `db:"field_c"`
    CreatedAt time.Time `db:"created_at"`
    UpdatedAt time.Time `db:"updated_at"`
}

type ArgIDs struct {
    IDs pq.StringArray
}

var db *sql.DB

var modelsView norm.View[[]Model, ArgIDs]

func init(){
    // Connect to the database.
    db = ...

    modelsView = normsql.NewView[[]Model, ArgIDs](db, `
    SELECT 
        "id", 
        "field_a",
        "field_b",
        "field_c",
        "created_at",
        "updated_at"
    FROM 
        "tests" 
    WHERE 
        "id" = ANY( {{ .A.IDs }} )
    ORDER BY 
        "id" ASC
    ;`,
    )
}

func main(){
    values, _ := modelsView.Read(context.Background(), ArgsIDs{
        IDs: []string{"id01", "id02"},
    })

    fmt.Println(values)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound    = errors.New("not found")
	ErrNotAffected = errors.New("not affected by create/update")
)

Functions

func Group added in v0.1.1

func Group[
	M ~[]T,
	T Keyer[K],
	A any,
	K comparable,
](
	ctx context.Context,
	r Reader[M, A],
	args A,
) (
	groups map[K][]T,
	err error,
)

Group performs values grouping.

Types

type Creator added in v0.0.2

type Creator[M, A any] interface {
	Create(ctx context.Context, key A, value M) error
}

type Deleter added in v0.0.2

type Deleter[M, A any] interface {
	Delete(ctx context.Context, args A) error
}

type ImmutableObject

type ImmutableObject[M, A any] interface {
	Creator[M, A]
	Reader[M, A]
}

type Keyer added in v0.1.0

type Keyer[K comparable] interface {
	Key() K
}

type Merge added in v0.1.0

type Merge[M1, M2 any] struct {
	L M1
	R []M2
}

func Lookup added in v0.1.0

func Lookup[
	M1 ~[]T1,
	M2 ~[]T2,
	T1, T2 Keyer[K],
	A1, A2 any,
	K comparable,
](
	ctx context.Context,
	lhs Reader[M1, A1], lhsArgs A1,
	rhs Reader[M2, A2], rhsArgs A2,
) (
	merge []Merge[T1, T2],
	err error,
)

Lookup performs left outer join.

type Object

type Object[M, A any] interface {
	Creator[M, A]
	Reader[M, A]
	Updater[M, A]
	Deleter[M, A]
}

type PersistentObject

type PersistentObject[M, A any] interface {
	Creator[M, A]
	Reader[M, A]
	Updater[M, A]
}

type Reader added in v0.0.2

type Reader[M, A any] interface {
	Read(ctx context.Context, args A) (value M, err error)
}

type Updater added in v0.0.2

type Updater[M, A any] interface {
	Update(ctx context.Context, args A, value M) error
}

type View

type View[M, A any] interface {
	Reader[M, A]
}

Directories

Path Synopsis
driver
sql module

Jump to

Keyboard shortcuts

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