cqrs

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: MIT Imports: 4 Imported by: 0

README

go-cqrs

Go Reference Coverage Status License

English | Bahasa Indonesia

Minimal implementation of the CQRS (Command Query Responsibility Segregation) pattern for Go.

Overview

go-cqrs is a lightweight library for applying the CQRS pattern in Go applications. It provides a simple API to register and dispatch commands, register and publish events, and register and dispatch queries.

Features

  • Register and Dispatch Commands (one handler per command type)
  • Register and Publish Events (multiple handlers per event type)
  • Register and Dispatch Queries (one handler per query, returns a result)
  • Generics-based API for working with concrete types
  • Safe for concurrent access (uses mutexes)

Installation

Ensure Go (>= 1.18) is installed. Add the module to your project:

go get github.com/ryanbekhen/cqrs

Or require the module directly in your go.mod:

require github.com/ryanbekhen/cqrs latest

Usage Example

See the example folder.

API (Summary)

Core types:

  • type Command interface{}
  • type Event interface{}
  • type Query interface{}

Commands:

  • RegisterCommand[C Command, R any](h CommandHandler[C, R]) — register a handler for command type C.
  • DispatchCommand[C Command, R any](ctx context.Context, cmd C) (R, error) — dispatch a command to its handler.

Events:

  • RegisterEvent[E Event](h EventHandler[E]) — register an event handler (multiple handlers allowed).
  • Publish[E Event](ctx context.Context, e E) error — publish an event to all registered handlers.

Queries:

  • RegisterQuery[Q Query, R any](h QueryHandler[Q, R]) — register a handler for query Q that returns R.
  • DispatchQuery[Q Query, R any](ctx context.Context, q Q) (R, error) — execute the query and receive the result.

Common Errors

  • ErrCommandHandlerNotFound — no handler registered for the dispatched command.
  • ErrQueryHandlerNotFound — no handler registered for the dispatched query.

Implementation Notes

  • Type-to-handler mapping is implemented with reflect.
  • sync.RWMutex is used to protect handler maps for concurrent access.
  • The design is intentionally minimal to stay readable and easy to extend (e.g., add middleware, tracing, or async dispatch).

Testing

Run tests with:

go test ./...

Contributing

  1. Fork the repository
  2. Create a new branch: git checkout -b feature-name
  3. Commit your changes
  4. Open a Pull Request and describe your changes

License

This project is licensed under the MIT License — see the LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCommandHandlerNotFound = errors.New("command handler not found")
	ErrQueryHandlerNotFound   = errors.New("query handler not found")
)

Functions

func DispatchCommand added in v0.2.0

func DispatchCommand[C Command, R any](ctx context.Context, cmd C) (R, error)

func DispatchQuery

func DispatchQuery[Q Query, R any](ctx context.Context, q Q) (R, error)

func Publish

func Publish[E Event](ctx context.Context, e E) error

func RegisterCommand

func RegisterCommand[C Command, R any](h CommandHandler[C, R])

func RegisterEvent

func RegisterEvent[E Event](h EventHandler[E])

func RegisterQuery

func RegisterQuery[Q Query, R any](h QueryHandler[Q, R])

Types

type Command

type Command interface{}

type CommandHandler

type CommandHandler[C Command, R any] interface {
	Handle(ctx context.Context, cmd C) (R, error)
}

type Event

type Event interface{}

type EventHandler

type EventHandler[E Event] interface {
	Handle(ctx context.Context, e E) error
}

type Query

type Query interface{}

type QueryHandler

type QueryHandler[Q Query, R any] interface {
	Handle(ctx context.Context, q Q) (R, error)
}

Directories

Path Synopsis
example
basic command

Jump to

Keyboard shortcuts

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