Official Go client for the ONQL database server.
go get github.com/ONQL/onqlclient-gogo get github.com/ONQL/onqlclient-go@v0.1.0go get github.com/ONQL/onqlclient-go@mainpackage main
import (
"fmt"
"log"
onql "github.com/ONQL/onqlclient-go"
)
type User struct {
ID string `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
client, err := onql.Connect("localhost", 5656)
if err != nil { log.Fatal(err) }
defer client.Close()
if _, err := client.Insert("mydb", "users",
User{ID: "u1", Name: "John", Age: 30}); err != nil {
log.Fatal(err)
}
var users []User
if _, err := client.Onql("mydb.users[age>18]", &users); err != nil {
log.Fatal(err)
}
fmt.Println(users)
client.Update("mydb", "users",
map[string]any{"age": 31},
client.Build("mydb.users[id=$1].id", "u1"))
client.Delete("mydb", "users", "", onql.WithIDs([]string{"u1"}))
}Creates and returns a connected client.
Sends a raw request frame and waits for the response.
Closes the connection.
On top of raw SendRequest, the client exposes convenience methods that build
the standard payload envelopes for Insert / Update / Delete / Onql and
unwrap the server's {error, data} response automatically.
db is passed explicitly to Insert / Update / Delete. Onql takes a
fully-qualified ONQL expression (which already includes the db name), so no
separate db argument is needed.
query arguments are ONQL expression strings, e.g.
mydb.users[id="u1"].id. Use client.Build(template, values...) to
substitute $1, $2, ....
Insert a single record.
client.Insert("mydb", "users", map[string]any{
"id": "u1", "name": "John", "age": 30,
})Update records matching query (or the explicit IDs supplied via WithIDs).
| Option | Default | Description |
|---|---|---|
WithProtopass(string) |
"default" |
Proto-pass profile |
WithIDs([]string) |
[] |
Explicit record IDs (alternative to query) |
// Via ONQL query
client.Update("mydb", "users",
map[string]any{"age": 31},
client.Build("mydb.users[id=$1].id", "u1"))
// Via explicit IDs
client.Update("mydb", "users",
map[string]any{"age": 31}, "",
onql.WithIDs([]string{"u1"}))Delete records matching query (or WithIDs).
client.Delete("mydb", "users",
client.Build("mydb.users[id=$1].id", "u1"))
client.Delete("mydb", "users", "", onql.WithIDs([]string{"u1"}))Run a raw ONQL query. If out is non-nil, the decoded data field is
unmarshalled into it (pass a pointer to a struct or slice).
| Option | Default | Description |
|---|---|---|
WithProtopass(string) |
"default" |
Proto-pass profile |
WithContext(key, values) |
"", [] |
Context key / values |
var users []User
_, err := client.Onql("mydb.users[age>18]", &users)Replace $1, $2, … placeholders with values. Strings are double-quoted;
numeric and boolean values are inlined verbatim.
q := client.Build("mydb.users[name=$1 and age>$2]", "John", 18)
// -> mydb.users[name="John" and age>18]
var rows []User
_, err := client.Onql(q, &rows)<request_id>\x1E<keyword>\x1E<payload>\x04
\x1E— field delimiter\x04— end-of-message marker
MIT