Flash is a lightweight Go library for managing real-time PostgreSQL changes using event management.
This library is currently under active development.
Features and APIs may change.
Contributions and feedback are welcome!
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
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:
When you define a primary key, instead of receiving an update event when the column changes, you will receive two events:
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:
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.
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.
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 |
You can see all drivers details here
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.
Unified Repository:
The previously separate sub-modules are now combined into one repository. This allows for easier dependency management and a more cohesive development process.
Separate Driver Installation:
While the core functionality is now in one place, the drivers need to be installed separately. This modular approach ensures that you only include what you need, keeping your projects lightweight.
No default driver:
By default, we are previously using trigger driver, to keep user informed, the user require now to instanciate the driver and pass it in ClientConfig
client.NewClient(&type.ClientConfig{})
by flash.NewClient(&flash.ClientConfig{})
listeners.NewListener(types.ListenerConfig{})
by flash.NewListener(&flash.ListenerConfig{})
Driver
in your codebase and pass it to flash.ClientConfig{}
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
}
The MIT License (MIT). Please see License File for more information.