quix-labs / flash

Go library for managing real-time PostgreSQL changes.
MIT License
45 stars 6 forks source link
event-driven event-sourcing go postgresql realtime

Flash

Flash is a lightweight Go library for managing real-time PostgreSQL changes using event management.

Notes

This library is currently under active development.

Features and APIs may change.

Contributions and feedback are welcome!

Table of Contents

Features

Installation

To install the library, run:

go get -u github.com/quix-labs/flash@main # Actually main is used for development
go get -u github.com/quix-labs/flash/drivers/trigger@main # Show below to for other drivers

# Write your main package

go mod tidy # Actually needed to download nested dependencies - Working on proper pre-install during previous go get

Usage

Here's a basic example of how to use Flash:

package main

import (
    "fmt"
    "github.com/quix-labs/flash"
    "github.com/quix-labs/flash/drivers/trigger"
    "os"
    "os/signal"
)

func main() {
    // Example with listener and client setup
    postsListener, _ := flash.NewListener(&flash.ListenerConfig{Table: "public.posts"})

    postsListener.On(flash.OperationAll, func(event flash.Event) {
        switch typedEvent := event.(type) {
        case *flash.InsertEvent:
            fmt.Printf("insert - new: %+v\n", typedEvent.New)
        case *flash.UpdateEvent:
            fmt.Printf("update - old: %+v - new: %+v\n", typedEvent.Old, typedEvent.New)
        case *flash.DeleteEvent:
            fmt.Printf("delete - old: %+v \n", typedEvent.Old)
        case *flash.TruncateEvent:
            fmt.Printf("truncate \n")
        }
    })

    // Create client
    flashClient, _ := flash.NewClient(&flash.ClientConfig{
        DatabaseCnx: "postgresql://devuser:devpass@localhost:5432/devdb",
        Driver:      trigger.NewDriver(&trigger.DriverConfig{}),
    })
    flashClient.Attach(postsListener)

    // Start listening
    go flashClient.Start()
    defer flashClient.Close()

    // Wait for interrupt signal (Ctrl+C)
    interrupt := make(chan os.Signal, 1)
    signal.Notify(interrupt, os.Interrupt)
    <-interrupt

    fmt.Println("Program terminated.")
}

For more detailed examples, check out the following files:

Advanced Features

1. Configurable Primary Key ⏳

When you define a primary key, instead of receiving an update event when the column changes, you will receive two events:

2. Custom Conditions ✅

You can configure conditions, and if a database row does not match the criteria, you will not receive any event.

In the case of an update:

3. Partial Fields ✅

Ability to listen only to certain columns in your table. If no changes occur in one of these columns, you will not receive any event.

⚠️ Important Notes ⚠️

Some of these features may be incompatible with your driver.

Check drivers/README.md to see if the driver you have chosen supports these features.

Planned Features

The following features are planned for future implementation:

Operator trigger wal_logical
equals
neq
lt
lte
gte
not null
is null ⚠️ using eq + nil ⚠️ using eq + nil

Drivers

You can see all drivers details here

Upgrade from Old Structure

In the previous structure, our project was divided into three distinct sub-modules, making it cumbersome to manage and integrate changes.

We have now merged these sub-modules into a single, unified module: github.com/quix-labs/flash.

This consolidation simplifies the codebase and streamlines development.

Key Changes:

Upgrade Guide

package main

import (
    "github.com/quix-labs/flash"
    "github.com/quix-labs/flash/drivers/trigger"
)

func main() {
    // Instantiation of driver is now required
    driver := trigger.NewDriver(&trigger.DriverConfig{})
    client := flash.NewClient(&flash.ClientConfig{
        Driver: driver,
    })

    // Instead of listeners.NewListener, use flash.NewListener
    listener := flash.NewListener(&flash.ListenerConfig{})

    // Your additional code here
}

Contributing

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Commit your changes.
  4. Push your branch.
  5. Create a pull request.

Credits

License

The MIT License (MIT). Please see License File for more information.