Go SDK for interacting with OstrichDB
Before using this SDK, you need:
- Go 1.23.1 or higher
- The Odin Compiler
- Python 3.x
You also need to have OstrichDB running:
-
Create a
.envfile in root of the project that you are using the SDK in -
In that
.envfile create a new entry WITHOUT a value:OSTRICHDB_JWT= -
Clone or fork the Open-OstrichDB repository For step 3, It is recommended to fork the repo and put it on root of your working project
-
Once the Open-OstrichDB repo is on your machine
cdinto it The following steps assume your CWD is the root of the Open-OstrichDB repo -
Obtain a JWT authentication token by running the JWT creation script:
python3 create_test_jwt.py -
Store the printed value in the
.envfile you created in step 2. -
Start the OstrichDB server:
./scripts/local_run.sh
Import the SDK into your Go project:
import "ostrichdb-go/src/sdk"Every interaction with OstrichDB requires initializing a config and client:
package main
import (
"fmt"
"ostrichdb-go/src/sdk"
)
func main() {
// Initialize configuration (loads JWT from .env)
config := sdk.NewConfigBuilder()
// Create a client with the configuration
client := sdk.NewClientBuilder(config)
}Projects are top-level containers in OstrichDB. If the project already exists, use its existing name:
// Create or reference a project
project := sdk.NewProjectBuilder(client, "myCoolProject")
err := sdk.CreateProject(project)
if err != nil {
fmt.Println("Error creating project:", err)
}Collections (databases) belong to projects:
// Create a collection within the project
collection := sdk.NewCollectionBuilder(project, "myFirstCollection")
err := sdk.CreateCollection(collection)
if err != nil {
fmt.Println("Error creating collection:", err)
}Clusters are groups of related records within a collection:
// Create a cluster
cluster := sdk.NewClusterBuilder(collection, "myCluster")
err := sdk.CreateCluster(cluster)
if err != nil {
fmt.Println("Error creating cluster:", err)
}
// Create a record within the cluster
record := sdk.NewRecordBuilder(cluster, "recordName", "string", "myValue")
err = sdk.CreateRecord(record)
if err != nil {
fmt.Println("Error creating record:", err)
}
// Delete a record
err = sdk.DeleteRecord(record)
if err != nil {
fmt.Println("Error deleting record:", err)
}The SDK requires a .env file in your project root containing your OstrichDB JWT token:
OSTRICHDB_JWT=your_jwt_token_here
See main.go for a complete working example.