📋 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
DSN from the existing GORM connection.
- Establish a separate, long-lived
pgconn in replication mode (outside the GORM pool).
- GORM Schema Mapping:
- Use
db.Statement.Parse(model) to cache table-to-struct metadata.
- Map Postgres
RelationID to GORM schema.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, and JSONB tags.
- Automatic Identity Management:
- Provide a utility to run
ALTER TABLE ... REPLICA IDENTITY FULL for tables registered for Real-time.
✅ Acceptance Criteria
🛠️ Implementation Tasks
- [ ] Replication Driver: Setup
pglogrepl to establish the slot and publication.
- [ ] Schema Registry: Create a thread-safe map that links Postgres
RelationID to *schema.Schema.
- [ ] GORM-Type Mapper: Write the
castByOID logic specifically for GORM’s DataType (e.g., mapping time to time.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
UPDATE and DELETE to show the full struct, they must enable REPLICA IDENTITY FULL on 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.
📋 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
DSNfrom the existing GORM connection.pgconnin replication mode (outside the GORM pool).db.Statement.Parse(model)to cache table-to-struct metadata.RelationIDto GORMschema.Schema.gorm.DeletedAt,UUID, andJSONBtags.ALTER TABLE ... REPLICA IDENTITY FULLfor tables registered for Real-time.✅ Acceptance Criteria
auth.Realtime.Register(&User{})).INSERT(New data),UPDATE(Diff data), andDELETE(Primary Key).*sql.DBpool.🛠️ Implementation Tasks
pglogreplto establish the slot and publication.RelationIDto*schema.Schema.castByOIDlogic specifically for GORM’sDataType(e.g., mappingtimetotime.Time).auth.Realtime.Enable(db)to handle SQL-side setup.📝 Notes for GORM Integration
UPDATEandDELETEto show the full struct, they must enableREPLICA IDENTITY FULLon that table.The above is subject to change.