MongoDB Store
MongoDB store for document-oriented deployments.
The MongoDB store uses Grove ORM with the mongodriver for database access. It stores each entity type in a dedicated collection and uses MongoDB indexes for efficient querying.
Usage
Standalone
import (
"github.com/xraph/grove"
_ "github.com/xraph/grove/drivers/mongodriver"
mongostore "github.com/xraph/relay/store/mongo"
)
db, err := grove.Open("mongo", "mongodb://localhost:27017/relay")
if err != nil {
log.Fatal(err)
}
s := mongostore.New(db)
if err := s.Migrate(ctx); err != nil {
log.Fatal(err)
}
r, err := relay.New(relay.WithStore(s))With Forge extension
When using the Forge extension with Grove, the store is auto-constructed from the DI container based on the driver type:
app.Use(extension.New(
extension.WithGroveDatabase(""), // uses the default grove.DB
))Or via YAML config:
extensions:
relay:
grove_database: ""The extension detects the mongo driver automatically and constructs the correct store.
Collections
The store uses five collections, all prefixed with relay_:
| Collection | Purpose |
|---|---|
relay_event_types | Event type catalog definitions |
relay_endpoints | Webhook endpoint registrations |
relay_events | Inbound event records |
relay_deliveries | Delivery queue entries |
relay_dlq | Dead letter queue entries |
Migrations
Migrate() creates collections with JSON Schema validation (auto-generated from Grove model structs) and applies indexes. Key indexes include:
- Event types: unique index on
name, indexes ongroup_nameandcreated_at - Endpoints: compound indexes on
(tenant_id, enabled)and(tenant_id, created_at) - Events: compound index on
(tenant_id, type, created_at), sparse unique index onidempotency_key - Deliveries: compound index on
(state, next_attempt_at)for efficient dequeue, plus indexes onendpoint_idandevent_id - DLQ: compound index on
(tenant_id, failed_at), index onendpoint_id
Internals
| Aspect | Detail |
|---|---|
| Driver | Grove ORM with mongodriver (mongo-driver v2) |
| Migrations | Grove migrations with JSON Schema validation + indexes |
| Dequeue | FindOneAndUpdate with state filter |
| Transactions | MongoDB single-document atomicity |
| Ping | db.Ping(ctx) |
| Close | Closes the Grove connection |
Grove Migrations
The store exports a Migrations group for use with Grove's migration orchestrator. This enables tracked, versioned migrations across all stores in your application:
import mongostore "github.com/xraph/relay/store/mongo"
// mongostore.Migrations is a migrate.Group for the relay mongo store.
// Register it with the grove migration orchestrator for coordinated migrations.When to use
- When your infrastructure is MongoDB-centric.
- Document-oriented data models where JSON flexibility is preferred.
- Horizontal scaling via MongoDB sharding.