This library simplifies interaction with a Postgres database as key-value storage. It streamlines tasks such as storing, retrieving, and deleting data, as well as executing batch insertions and deletions.
Ensure you have Go installed on your system. If not, download it from the official Go website.
To install the library, run the following command in your terminal:
go get -u github.com/kwilteam/postgresKVdb
Begin by initializing the interface with the NewPostgresDB(name, dir string)
function, which returns a new PostgresDB
instance.
Here's how to get started:
package main
import (
"fmt"
"log"
"github.com/tesfayh/postgresKVdb"
)
func main() {
// Initialize the PostgresDB instance
db, err := postgresKVdb.NewPostgresDB("inde-xer:indexer123@localhost:5433", "indexer")
if err != nil {
log.Fatalf("Failed to create a new instance of PostgresDB: %v", err)
}
// Define test data
key := []byte("test_key")
value := []byte("test_value")
// Call the Set method
err = db.Set(key, value)
if err != nil {
log.Fatalf("Failed to set %v", err)
}
// Check if the value is set correctly
result, err := db.Get(key)
if err != nil {
log.Fatalf("Failed to get %v", err)
}
fmt.Printf("The value of the key is: %s\n", result)
}
To configure the library for your Postgres setup, modify the connection parameters in the NewPostgresDB
function call according to your database configuration.
Here's an example:
// Initialize the PostgresDB instance with custom connection parameters
db, err := postgresKVdb.NewPostgresDB("<username>:<password>@<host>:<port>", "<database_name>")
The connection parameters are as follows:
<username>
: Your PostgreSQL username<password>
: Your PostgreSQL password<host>
: The hostname or IP address of your PostgreSQL server<port>
: The port number on which your PostgreSQL server is running<database_name>
: The name of the PostgreSQL database you want to connect to
Errors within the library are handled using Go's built-in error handling mechanism. It's recommended to handle errors returned by library functions gracefully in your code.
Here's an example of error handling:
// Example error handling
result, err := db.Get(key)
if err != nil {
log.Fatalf("Failed to get %v", err)
}
We welcome contributions from the community! If you encounter any bugs, have feature requests, or want to contribute code changes, please follow the guidelines outlined in our Contribution Guidelines.