SDK generation plugin
The sdks plugin generates a language-specific SDK from the database's
exported definitions. This release supports Go.
Configuration
Enable the plugin and set the output directory:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 00 | 54 52 4F 4E | magic TRON |
| 04 | 4C 70617468 | txt "path" |
| 09 | 6C 6170702E6462 | txt "app.db" |
| 10 | 0F 0A 04000000 0900 0000 | map leaf, size 10: @4 → @9 |
| 1A | 9C 6469726563746F 7279 | txt "directory" |
| 24 | 5C 2E2F73646B | txt "./sdk" |
| 2A | 0F 0A 1A000000 2400 0000 | map leaf, size 10: @1A → @24 |
| 34 | 2C 676F | txt "go" |
| 37 | 0F 0A 34000000 2A00 0000 | map leaf, size 10: @34 → @2A |
| 41 | 8C 64617461626173 65 | txt "database" |
| 4A | 0F 0A 41000000 1000 0000 | map leaf, size 10: @41 → @10 |
| 54 | 4C 73646B73 | txt "sdks" |
| 59 | 0F 0A 54000000 3700 0000 | map leaf, size 10: @54 → @37 |
| 63 | 07 0E 00500000 4A00 0000 59000000 | map branch, size 14, depth 0 (root), slots 12, 14: @4A → @59 |
| 71 | 7C 706C7567696E73 | txt "plugins" |
| 79 | 0F 0A 71000000 6300 0000 | map leaf, size 10: @71 → @63 |
| 83 | 79000000 00000000 | root → @79; previous: none |
| Setting | Requirement | Description |
|---|---|---|
sdks.go | Optional | Enables the Go generator. |
sdks.go.directory | Required when go is enabled; nonempty string | Directory for generated Go source; relative paths are resolved from the process working directory. |
With no language configured, sdks does not generate files. There is no default
output directory. Select stdlib
and database separately if the definitions
or application need them.
An enabled SDK is generated when the database opens and refreshed after definitions
are updated. The output directory contains sdk.go and schemas/schemas.go.
Go configuration
The Go binding can express the same configuration with Go structs:
config := stardust.Config{
Plugins: &stardust.Plugins{
Stdlib: true,
Database: &stardust.DatabaseSettings{Path: "app.db"},
SDKs: &stardust.SDKSettings{
Go: &stardust.GoSDKSettings{Directory: "./sdk"},
},
},
}
db, err := stardust.OpenConfig(config)SDK generation
The configured SDK directory contains these generated files:
sdk.gocontains namespace structs, exported values and methods for exported functions.schemas/schemas.gocontains types for exported schemas.
For example, an exported function add in namespace spice/models produces
this database method:
result, err := db.SpiceModels.Add(20, 22)Fixed arities with simple contracts use Go types. Definitions without a
specific type use sdk.Value, with a closed set of Stardust value variants.
Schema shapes produce Go structs, and other schemas produce Go aliases.
The generated structs contain the schema's declared fields. Literal string,
integer, float, boolean, bytes, list, and map exports become fields on their namespace struct.
Other exported values, including computed values, use sdk.Value.
The generated methods cover exported defquery, defmutation, and defn
forms, and exported def forms whose value is an inline fn. A query with a
simple named find list or a field projection also generates a row struct. A
schema clause or result contract can give the fields their Go types. Generated schema types cover exported
defschema forms. Function names can use ASCII letters, digits, and
_ - ? ! * + < > = ..
Mutation methods return a fixed result with the transaction and created IDs. Mutations that create entities for query rows return slices of IDs. For a checked mutation, a parameter assigned directly to a schema-constrained field uses that field's Go type.
Using the generated Go SDK
Import the generated SDK from your Go module. With the spice/models/add
function saved in app.db, open the database and call its generated method:
db, err := sdk.Open(sdk.Config{Plugins: &sdk.Plugins{
Stdlib: true,
Database: &sdk.DatabaseSettings{Path: "app.db"},
}})
if err != nil {
log.Fatal(err)
}
defer db.Close()
result, err := db.SpiceModels.Add(20, 22)
if err != nil {
log.Fatal(err)
}
fmt.Println(result) // 42Query and mutation methods
Export defquery and defmutation declarations to generate Go methods.
For example, save these forms in spice/models:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 000 | 54 52 4F 4E | magic TRON |
| 004 | 6C 236C6F63616C | txt "#local" |
| 00B | 7C 76657273696F6E | txt "version" |
| 013 | 0F 0A 04000000 0B00 0000 | map leaf, size 10: @4 → @B |
| 01D | 3C 646566 | txt "def" |
| 021 | 02 03000000000000 00 | i64 3 |
| 02A | 0E 15 00 0700 030000 00 1D000000 130000 00 21000000 | array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @1D → @13 → @21 |
| 03F | 7C 23696E766F6B65 | txt "#invoke" |
| 047 | 0F 0A 3F000000 2A00 0000 | map leaf, size 10: @3F → @2A |
| 051 | 6C 236C6F63616C | txt "#local" |
| 058 | 6C 637265617465 | txt "create" |
| 05F | 0F 0A 51000000 5800 0000 | map leaf, size 10: @51 → @58 |
| 069 | 6C 236C6F63616C | txt "#local" |
| 070 | 4C 6E616D65 | txt "name" |
| 075 | 0F 0A 69000000 7000 0000 | map leaf, size 10: @69 → @70 |
| 07F | 0E 0D 00 0100 010000 00 75000000 | array leaf, size 13, shift 0, bitmap 0001, count 1 (root): @75 |
| 08C | 6C 236C6F63616C | txt "#local" |
| 093 | 7C 737472696E673F | txt "string?" |
| 09B | 0F 0A 8C000000 9300 0000 | map leaf, size 10: @8C → @93 |
| 0A5 | 4C 6E616D65 | txt "name" |
| 0AA | 0F 0A A5000000 9B00 0000 | map leaf, size 10: @A5 → @9B |
| 0B4 | 6C 236C6F63616C | txt "#local" |
| 0BB | 4C 6E616D65 | txt "name" |
| 0C0 | 0F 0A B4000000 BB00 0000 | map leaf, size 10: @B4 → @BB |
| 0CA | 4C 6E616D65 | txt "name" |
| 0CF | 0F 0A CA000000 C000 0000 | map leaf, size 10: @CA → @C0 |
| 0D9 | 5C 5F6974656D | txt "_item" |
| 0DF | 0F 0A D9000000 CF00 0000 | map leaf, size 10: @D9 → @CF |
| 0E9 | 5C 7061746368 | txt "patch" |
| 0EF | 0F 0A E9000000 DF00 0000 | map leaf, size 10: @E9 → @DF |
| 0F9 | 6C 736368656D61 | txt "schema" |
| 100 | 0F 0A F9000000 AA00 0000 | map leaf, size 10: @F9 → @AA |
Show all 82 nodes
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 10A | 07 0E 01040000 EF00 0000 00010000 | map branch, size 14, depth 1, slots 0, 10: @EF → @100 |
| 118 | 07 0A 80000000 0A01 0000 | map branch, size 10, depth 0 (root), slots 7: @10A |
| 122 | BC 6465666D757461 74696F6E | txt "defmutation" |
| 12E | 0E 19 00 0F00 040000 00 22010000 5F0000 00 7F000000 180100 00 | array leaf, size 25, shift 0, bitmap 000F, count 4 (root): @122 → @5F → @7F → @118 |
| 147 | 7C 23696E766F6B65 | txt "#invoke" |
| 14F | 0F 0A 47010000 2E01 0000 | map leaf, size 10: @147 → @12E |
| 159 | 6C 236C6F63616C | txt "#local" |
| 160 | 6C 70656F706C65 | txt "people" |
| 167 | 0F 0A 59010000 6001 0000 | map leaf, size 10: @159 → @160 |
| 171 | 0E 09 00 0000 000000 00 | array leaf, size 9, shift 0, bitmap 0000, count 0 (root) |
| 17A | 6C 236C6F63616C | txt "#local" |
| 181 | 6C 736368656D61 | txt "schema" |
| 188 | 0F 0A 7A010000 8101 0000 | map leaf, size 10: @17A → @181 |
| 192 | 6C 236C6F63616C | txt "#local" |
| 199 | 7C 737472696E673F | txt "string?" |
| 1A1 | 0F 0A 92010000 9901 0000 | map leaf, size 10: @192 → @199 |
| 1AB | 4C 6E616D65 | txt "name" |
| 1B0 | 0F 0A AB010000 A101 0000 | map leaf, size 10: @1AB → @1A1 |
| 1BA | 6C 236C6F63616C | txt "#local" |
| 1C1 | 2C 3F65 | txt "?e" |
| 1C4 | 0F 0A BA010000 C101 0000 | map leaf, size 10: @1BA → @1C1 |
| 1CE | 0E 15 00 0700 030000 00 88010000 B00100 00 C4010000 | array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @188 → @1B0 → @1C4 |
| 1E3 | 0E 0D 00 0100 010000 00 CE010000 | array leaf, size 13, shift 0, bitmap 0001, count 1 (root): @1CE |
| 1F0 | 4C 6E616D65 | txt "name" |
| 1F5 | 09 | bit true |
| 1F6 | 0F 0A F0010000 F501 0000 | map leaf, size 10: @1F0 → @1F5 |
| 200 | 2C 3F65 | txt "?e" |
| 203 | 0F 0A 00020000 F601 0000 | map leaf, size 10: @200 → @1F6 |
| 20D | 5C 7768657265 | txt "where" |
| 213 | 0F 0A 0D020000 E301 0000 | map leaf, size 10: @20D → @1E3 |
| 21D | 7C 70726F6A656374 | txt "project" |
| 225 | 0F 0A 1D020000 0302 0000 | map leaf, size 10: @21D → @203 |
| 22F | 07 0E 02040000 1302 0000 25020000 | map branch, size 14, depth 0 (root), slots 1, 10: @213 → @225 |
| 23D | 8C 64656671756572 79 | txt "defquery" |
| 246 | 0E 19 00 0F00 040000 00 3D020000 670100 00 71010000 2F0200 00 | array leaf, size 25, shift 0, bitmap 000F, count 4 (root): @23D → @167 → @171 → @22F |
| 25F | 7C 23696E766F6B65 | txt "#invoke" |
| 267 | 0F 0A 5F020000 4602 0000 | map leaf, size 10: @25F → @246 |
| 271 | 6C 236C6F63616C | txt "#local" |
| 278 | 7C 76657273696F6E | txt "version" |
| 280 | 0F 0A 71020000 7802 0000 | map leaf, size 10: @271 → @278 |
| 28A | 6C 236C6F63616C | txt "#local" |
| 291 | 6C 637265617465 | txt "create" |
| 298 | 0F 0A 8A020000 9102 0000 | map leaf, size 10: @28A → @291 |
| 2A2 | 6C 236C6F63616C | txt "#local" |
| 2A9 | 6C 70656F706C65 | txt "people" |
| 2B0 | 0F 0A A2020000 A902 0000 | map leaf, size 10: @2A2 → @2A9 |
| 2BA | 6C 6578706F7274 | txt "export" |
| 2C1 | 0E 19 00 0F00 040000 00 BA020000 800200 00 98020000 B00200 00 | array leaf, size 25, shift 0, bitmap 000F, count 4 (root): @2BA → @280 → @298 → @2B0 |
| 2DA | 7C 23696E766F6B65 | txt "#invoke" |
| 2E2 | 0F 0A DA020000 C102 0000 | map leaf, size 10: @2DA → @2C1 |
| 2EC | 0E 19 00 0F00 040000 00 47000000 4F0100 00 67020000 E20200 00 | array leaf, size 25, shift 0, bitmap 000F, count 4 (root): @47 → @14F → @267 → @2E2 |
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 305 | EC020000 00000000 | root → @2EC; previous: none |
The generated namespace exposes the literal scalar export and both methods:
fmt.Println(db.SpiceModels.Version) // 3
_, err := db.SpiceModels.Create("Ada")
if err != nil {
log.Fatal(err)
}
people, err := db.SpiceModels.People()
if err != nil {
log.Fatal(err)
}
fmt.Println(people) // [{Ada}]Subscribing to query results
Each exported defquery also generates a SubscribeTo method with the same
parameters. It returns a *stardust.Subscription of the query's row type.
Events returns an iterator over the results, and Next waits for one
result. The first result is the current result.
After each commit that changes a field that the query reads, or the namespace
that defines the query, the subscription delivers a new result. Each result is
complete. When a newer result arrives before Next takes the previous one,
the newer result replaces it:
sub, err := db.SpiceModels.SubscribeToPeople()
if err != nil {
log.Fatal(err)
}
defer sub.Close()
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for event, err := range sub.Events(ctx) {
if err != nil {
return // context.Canceled after cancel
}
fmt.Println(event.Result) // [{Ada}]
}
}()
// ... write to the database ...
cancel()
wg.Wait()An error from Next is the last step of Events. Close cancels the
subscription. After Close, Next returns
stardust.ErrSubscriptionClosed. When the database closes, Next returns
stardust.ErrClosed. When ctx ends, Next returns its error and the
subscription continues. Without a generated SDK, use
stardust.Subscribe[T](db, "spice/models", "people").
Reading reactor stats
A saved defreactor runs inside each write that changes a field it reads or
writes. The generated SDK has no method for a reactor. Read the facts that it
writes with a query. db.Reactors() returns a []stardust.ReactorStats with
each saved reactor and its work since the database opened:
stats, err := db.Reactors()
if err != nil {
log.Fatal(err)
}
for _, reactor := range stats {
fmt.Printf("%s/%s: %d runs, %d changes\n",
reactor.Namespace, reactor.Name, reactor.Runs, reactor.Changes)
}LastTx is the last transaction that the reactor changed. Failures counts
the writes that it caused to fail, and LastFailure holds the last message.
LastTx and LastFailure are nil until they have a value.
Updating definitions from a directory
On an open *stardust.Database, save the DUST files in a directory as
namespaces. For example, definitions/people/models.dust supplies the forms
for people/models:
response, err := db.UpdateDefinitionsFromDir("./definitions")
if err != nil {
log.Fatal(err)
}
for _, namespace := range response.Namespaces {
fmt.Println(namespace.Path, namespace.Revision)
}The files are saved together in one transaction. Removing a file does not
delete its previously saved namespace. If the SDK plugin is
enabled, it regenerates the SDK after the transaction commits. A
*stardust.DefinitionsError with Outcome set to "committed" means the
definitions were saved but a plugin could not complete its update.
Using the released Go binding
The release archive includes the stardust Go module in abi/bindings/go and
the native libraries in abi/lib. In your application module, point Go at the
extracted release. For example, on Linux x86-64, set STARDUST_RELEASE to the
absolute path of the extracted archive, then run:
go mod edit -require=stardust@v0.0.0 \
-replace=stardust="$STARDUST_RELEASE/abi/bindings/go"The released binding links to the native library in abi/lib on Linux
x86-64 and ARM64. The generated SDK imports this binding as stardust.
Using the Go ABI binding
Import the released binding to send requests directly:
import "stardust"Decode creates a Document. Submit accepts request Documents and returns
their UUIDs. Poll returns response Documents. Close each document when you no longer need it.
A poll wait of -1 waits for a response:
config, err := stardust.Decode(`{workers 1 plugins <stdlib database>}`)
if err != nil {
log.Fatal(err)
}
defer config.Close()
db, err := stardust.Open(config)
if err != nil {
log.Fatal(err)
}
defer db.Close()
request, err := stardust.Decode(`{method "eval" params {code (+ 20 22)}}`)
if err != nil {
log.Fatal(err)
}
defer request.Close()
ids, err := db.Submit(request)
if err != nil {
log.Fatal(err)
}
responses, err := db.Poll(-1)
if err != nil {
for index := range responses {
if closeErr := responses[index].Close(); closeErr != nil {
log.Print(closeErr)
}
}
log.Fatal(err)
}
fmt.Println(ids[0]) // Match this request with the response causation-id.
for index := range responses {
response := responses[index]
text, err := response.Encode()
if err != nil {
log.Fatal(err)
}
fmt.Print(text) // The response has result 42 and UUID message fields.
message, err := response.Message()
if err != nil {
log.Fatal(err)
}
fmt.Println(message.CausationID == ids[0])
if err := response.Close(); err != nil {
log.Fatal(err)
}
}For a saved function, the binding also offers Call and typed CallAs.
Use the same handle for function calls:
result, err := stardust.CallAs[int64](db, "spice/models", "add", 20, 22)Generated calls match their responses by causation ID. Poll raw responses concurrently with large submissions so the bounded queues can make progress.