2025-08-30 10:30:53 +03:00
v1
2025-08-30 10:30:53 +03:00
2025-05-04 21:19:29 +03:00
2025-05-04 21:19:29 +03:00
v1
2025-08-30 10:30:53 +03:00
2025-05-04 21:19:29 +03:00
v1
2025-08-30 10:30:53 +03:00
v1
2025-08-30 10:30:53 +03:00

repo

This module is mainly an exercise with generics. Although it is a fully functional repository for any Go structs.

Might be useful for pet-projects and like.

Example usage.

import (
    "code.uint32.ru/tiny/repo"
    _ "github.com/mattn/go-sqlite3"
)

type My struct {
	A string
	B int
}

func main() {
    db, err := sql.Open("sqlite3", "test.db")
    if err != nil {
		panic(err)
	}

    defer db.Close()

    ctx := context.Background()

    // init repository for our type "My"
    r, err := repo.OpenOrCreate[My](ctx, db, "my_test_table")
    if err != nil {
        panic(err)
    }

    first := &My{
		A: "hello",
		B: 42,
	}

	if err := r.Create(ctx, "first_record", first); err != nil {
		panic(err)
	}

    duplicate, err = r.Read(ctx, "first_record")
	if err != nil {
		panic(err)
	}

    fmt.Printf("Type: %T, Value: %+v", duplicate)
}

The interface is a plain CRUD.

type Repo[T any] interface {
	// Create inserts object into repository table. If id already exists
	// in the database it is an error.
	Create(ctx context.Context, id string, v *T) error
	// Read returns object with specified id or ErrNotFound
	Read(ctx context.Context, id string) (*T, error)
	// Update updates object with id.
	Update(ctx context.Context, id string, v *T) error
	// Delete deletes object with id.
	Delete(ctx context.Context, id string) error
}
Description
No description provided
Readme MIT 43 KiB
Languages
Go 96.7%
Makefile 3.3%