-
-
Notifications
You must be signed in to change notification settings - Fork 19
Open
Labels
enhancementNew feature or requestNew feature or request
Description
📋 Synopsis
Build a Real-time plugin for GoBetterAuth that uses PostgreSQL Logical Replication to stream changes. Unlike a standard listener, this engine will integrate with GORM’s Schema to automatically decode binary WAL (Write-Ahead Log) data into user-defined GORM Models/Structs before pushing them to the Event Bus.
🏗️ Technical Requirements
- Dedicated Replication Connection:
- Extract the
DSNfrom the existing GORM connection. - Establish a separate, long-lived
pgconnin replication mode (outside the GORM pool).
- GORM Schema Mapping:
- Use
db.Statement.Parse(model)to cache table-to-struct metadata. - Map Postgres
RelationIDto GORMschema.Schema.
- The Decoder (Binary -> GORM Struct):
- Implement a parser that uses GORM Field Types (OIDs) to cast values correctly.
- Support GORM-specific types:
gorm.DeletedAt,UUID, andJSONBtags.
- Automatic Identity Management:
- Provide a utility to run
ALTER TABLE ... REPLICA IDENTITY FULLfor tables registered for Real-time.
✅ Acceptance Criteria
- Model Registration: Developers can register a GORM model for real-time (e.g.,
auth.Realtime.Register(&User{})). - Automatic Decoding: The Event Bus receives the actual Go Struct populated with data, not just raw strings.
- Action Parity: Correctly handles
INSERT(New data),UPDATE(Diff data), andDELETE(Primary Key). - Resilience: If the replication connection drops, it automatically resumes from the last confirmed LSN (Log Sequence Number).
- No Pool Contamination: The replication worker must not consume or block connections from the main GORM
*sql.DBpool.
🛠️ Implementation Tasks
- [ ] Replication Driver: Setup
pglogreplto establish the slot and publication. - [ ] Schema Registry: Create a thread-safe map that links Postgres
RelationIDto*schema.Schema. - [ ] GORM-Type Mapper: Write the
castByOIDlogic specifically for GORM’sDataType(e.g., mappingtimetotime.Time). - [ ] Event Bus Publisher: Create a specialized event payload:
type RealtimeEvent struct {
Model string // e.g. "User"
Action string // "INSERT"
Old interface{} // Struct before change (if Identity Full)
New interface{} // Struct after change
}- [ ] Middleware/Helper: Add
auth.Realtime.Enable(db)to handle SQL-side setup.
📝 Notes for GORM Integration
- REPLICA IDENTITY: Remind users that for
UPDATEandDELETEto show the full struct, they must enableREPLICA IDENTITY FULLon that table. - Performance: Use a background goroutine for the replication loop so it doesn't hang the main application startup.
The above is subject to change.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request