Documentation
¶
Overview ¶
Package nats provides NATS JetStream KeyValue cursor storage for MongoDB pagination. Enables distributed cursor sharing across application instances with automatic expiration.
Example:
kv, _ := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{
Bucket: "cursors",
TTL: time.Hour,
})
storage := nats.New(kv)
result, _ := mongo.ListCursor(ctx, coll, mongo.WithListCursorStorage(storage))
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Storage ¶
type Storage struct {
*kvstore.JSONStorage
natsbase.Base
}
Storage is a NATS JetStream KeyValue implementation of mongotools.CursorStorage. This implementation is suitable for production deployments with multiple instances where cursor sharing across instances is required.
Features:
- Distributed: Cursors shared across all application instances
- Automatic expiration: NATS KeyValue TTL handles cursor cleanup
- Persistent: Survives application restarts (if JetStream is persistent)
- Thread-safe: NATS handles concurrent access
- Configurable bucket name: Allows multiple isolated cursor storages
Example usage:
nc, _ := nats.Connect(nats.DefaultURL)
js, _ := jetstream.New(nc)
kv, _ := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{
Bucket: "cursors",
TTL: 1 * time.Hour,
})
storage := nats.New(kv)
result, err := mongotools.ListCursor(ctx, collection,
mongotools.WithListCursorStorage(storage),
mongotools.WithListCursorLimit(50),
)
func New ¶
New creates a new NATS JetStream KeyValue cursor storage.
The KeyValue bucket should be created by the caller with desired configuration (TTL, storage type, replicas, etc.).
Parameters:
- kv: NATS JetStream KeyValue bucket for storing cursor metadata
Returns:
- *Storage: Ready to use storage instance
Example:
nc, _ := nats.Connect(nats.DefaultURL)
js, _ := jetstream.New(nc)
kv, _ := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{
Bucket: "cursors",
TTL: 1 * time.Hour,
Storage: jetstream.FileStorage,
})
storage := nats.New(kv)