nats

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package nats provides a Hord database driver for the NATS key-value store.

The NATS driver allows interacting with the NATS key-value store, which is a distributed key-value store built on top of the NATS messaging system. To use this driver, import it as follows:

import (
    "github.com/madflojo/hord"
    "github.com/madflojo/hord/nats"
)

Connecting to the Database

Use the Dial() function to create a new client for interacting with the NATS driver.

var db hord.Database
db, err := nats.Dial(nats.Config{})
if err != nil {
    // Handle connection error
}

Initialize database

Hord provides a Setup() function for preparing the database. This function is safe to execute after every Dial().

err := db.Setup()
if err != nil {
    // Handle setup error
}

Database Operations

Hord provides a simple abstraction for working with the NATS driver, with easy-to-use methods such as Get() and Set() to read and write values.

Here are some examples demonstrating common usage patterns for the NATS driver.

// Connect to the NATS database
db, err := nats.Dial(nats.Config{})
if err != nil {
    // Handle connection error
}

err := db.Setup()
if err != nil {
    // Handle setup error
}

// Set a value
err = db.Set("key", []byte("value"))
if err != nil {
    // Handle error
}

// Retrieve a value
value, err := db.Get("key")
if err != nil {
    // Handle error
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// URL specifies the URL to connect to the NATS server. This URL follows the format
	// of `nats://user:pass@example:8222` with supported protocols being `nats`,  `tls`, or `ws` for web sockets.
	URL string

	// Bucket name for the key-value store. If Bucket does not exist on the NATS server side,
	// NATS will automatically create the bucket with the first key creation. Bucket names must adhere
	// to the `^[a-zA-Z0-9_-]+$` regex.
	Bucket string

	// Servers enables connectivity to a cluster of NATS servers. Each entry must follow the NATS URL format.
	Servers []string

	// SkipTLSVerify will disable the TLS hostname checking. Warning, using this setting opens the risk of
	// man-in-the-middle attacks.
	SkipTLSVerify bool

	// TLSConfig allows users to specify TLS settings for connecting to NATS. This is a standard TLS configuration
	// and can be used to configure 2-way TLS for NATS.
	TLSConfig *tls.Config

	// Options extend the connection options available within NATS. NATS has many advanced configuration options;
	// use Options to modify those options.
	Options nats.Options
}

Config represents the configuration for the NATS database connection.

type Database

type Database struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Database is a NATS implementation of the hord.Database interface.

func Dial

func Dial(cfg Config) (*Database, error)

Dial initializes and returns a new NATS database instance.

func (*Database) Close

func (db *Database) Close()

Close closes the NATS database connection and clears all stored data.

func (*Database) Delete

func (db *Database) Delete(key string) error

Delete removes data from the NATS database based on the provided key. It returns an error if the key is invalid.

func (*Database) Get

func (db *Database) Get(key string) ([]byte, error)

Get retrieves data from the NATS database based on the provided key. It returns the data associated with the key or an error if the key is invalid or the data does not exist.

func (*Database) HealthCheck

func (db *Database) HealthCheck() error

HealthCheck performs a health check on the NATS database.

func (*Database) Keys

func (db *Database) Keys() ([]string, error)

Keys retrieves a list of keys stored in the NATS database.

func (*Database) Set

func (db *Database) Set(key string, data []byte) error

Set inserts or updates data in the NATS database based on the provided key. It returns an error if the key or data is invalid.

func (*Database) Setup

func (db *Database) Setup() error

Setup sets up the nats database. This function does nothing for the nats driver.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL