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 ¶
Database is a NATS implementation of the hord.Database interface.
func (*Database) Close ¶
func (db *Database) Close()
Close closes the NATS database connection and clears all stored data.
func (*Database) Delete ¶
Delete removes data from the NATS database based on the provided key. It returns an error if the key is invalid.
func (*Database) Get ¶
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 ¶
HealthCheck performs a health check on the NATS database.