sqlite

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: Apache-2.0 Imports: 5 Imported by: 1

README

SQLite Hexade

SQLite Repository The Hexades architecture uses a local bus with events which contain (a) a channel to send data back to the caller (errors and data) and (b) a higher order function to execute. The function is executed receving the repository and the data. This function is customizable to a client's specific requirements and would be the equivalent, perhaps, of an adapter in pure imperative hexagonal architecture which doesn't use messaging.

type Insert struct {
    bus.Event
    value      any
    insertFunc InsertFunction
}

This is the extent of the repository logic! It receives the event on a channel, verifies it is an SQLite event, and then simply calls execute on the event passing itself in. The Execute method in turn calls the InsertFunc and executes the logic.

func (r *repository) OnRepositoryEvent(repositoryChannel <-chan bus.RepositoryEvent) {

    for repoEvent := range repositoryChannel {
        switch evt := repoEvent.(type) {
        case SQLiteEvent:
            evt.Execute(r)
        }
    }
}

The basic insert function is where things can get interesting in the future. The event, the repository and all the plumbing stay the same but if I want some different handling of the insert, I simply inject a different function. An update is even a better example, perhaps, as I may need to look up some values first before updating. In any case, this is the highly customizable part. Just inject a different function into the event and you change the behavior.

var BasicInsertFunc = func(event *Insert, repo *repository) {
    value := event.value
    tx := repo.db.Create(value)
    event.Send(bus.NewResponse(value, tx.Error))
}

The final step in the function is to call Send on the event. That actually puts the response on a channel in the event and the receiver can listen for it and block while all the other processing happens concurrently or in parallel.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BasicDeleteFunc = func(data *Model, repo *Repository) Response {
	value := data.Value
	tx := repo.db.Delete(value)
	return NewResponse(value, tx.Error)
}
View Source
var BasicInsertFunc = func(data *Model, repo *Repository) Response {
	value := data.Value

	repo.db.AutoMigrate(&value)
	tx := repo.db.Create(value)
	return NewResponse(value, tx.Error)
}
View Source
var BasicOpenFunc = func(data *Model, repo *Repository) Response {
	log.Println("Received open: ", data)
	db, err := gorm.Open(sqlite.Open(data.String()), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	repo.db = db
	return NewResponse(repo.db, err)
}
View Source
var BasicUpdateFunc = func(data *Model, repo *Repository) Response {
	value := data.Value
	tx := repo.db.Updates(value)
	return NewResponse(value, tx.Error)
}
View Source
var ReadFirstFunc = func(data *Model, repo *Repository) Response {
	value := data.Value
	tx := repo.db.First(value)
	return NewResponse(value, tx.Error)
}

Functions

func AddListener added in v1.0.3

func AddListener(EventListener EventListener)

func NewRepository

func NewRepository()

func SendEvent added in v1.0.3

func SendEvent(Event Event)

Types

type Event added in v0.1.1

type Event interface {
	Execute(repo *Repository)
	Send(Response)
	Receive() Response
}

func NewEvent added in v0.1.1

func NewEvent(value any, executable Executable) Event

type EventListener added in v1.0.1

type EventListener interface {
	OnEvent(Event <-chan Event)
}

type EventModel added in v1.0.1

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

func (*EventModel) Execute added in v1.0.1

func (e *EventModel) Execute(repo *Repository)

func (*EventModel) Receive added in v1.0.1

func (e *EventModel) Receive() Response

func (*EventModel) Send added in v1.0.1

func (e *EventModel) Send(val Response)

type Executable added in v1.0.1

type Executable = func(data *Model, repo *Repository) Response

type Model added in v1.0.1

type Model struct {
	Value any
}

func (*Model) String added in v1.0.1

func (m *Model) String() string

type Repository added in v1.0.1

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

func (*Repository) OnEvent added in v1.0.3

func (r *Repository) OnEvent(repositoryChannel <-chan Event)

type Response added in v1.0.1

type Response struct {
	Value any
	Err   error
}

func NewResponse added in v1.0.1

func NewResponse(value any, err error) Response

Jump to

Keyboard shortcuts

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