Save definitions and generate the SDK

Create definitions/dune/houses.dust with these forms. The path below definitions names the dune/houses namespace. This step saves only the forms that the next two steps call. A later step adds more forms to this file. Select DUST in the format selector when you copy the forms into this file.

edn657 B
[
  #invoke [
      "defmutation"
      #local "atomics-found"
      [#local "name"]
      {:schema {:name #local "string?"}
       :patch  {:_house {:house true
                         :name  #local "name"}}}]
  #invoke [
      "defquery"
      #local "houses"
      []
      {:where   [
         [#local ?house {:house true}]
         [
           #local "schema"
           {:name     #local "string?"
            :unsealed [#local "optional" [#local "maybe" #local "boolean?"]]}
           #local ?house]]
       :project {:?house {:name     true
                          :unsealed true}}}]
  #invoke ["export" #local "atomics-found" #local "houses"]]

The atomics-found mutation

defmutation defines a mutation, which is a function that writes to the database. The name comes first, then the parameter vector [name], then a body map. The patch member of the body gives the facts to write. A key that starts with _ is a temporary name for a new entity, so _house makes a new house.

The map of _house holds the tag <house> and the name field. A tag is a field with the value true, so <house> is the same as {house true}. One tag group can hold more than one tag, and it can be in the same map as other fields. The name field gets the value of the name parameter.

The schema member checks each entity that the patch writes, after the patch. Here, the name field must be a string. The SDK reads this schema to find the type of the parameter that fills the field. Thus AtomicsFound accepts a Go string. Without a schema or a contract, a generated parameter has the type Value.

The houses query

defquery defines a query. The empty vector [] shows that the query has no parameters. The where member contains the clauses that select entities. The clause [?house <house>] binds ?house to each entity with the house tag.

The clause [schema {name string? unsealed [optional [maybe boolean?]]} ?house] checks each house. Its name must be a string. Its unsealed field must be missing or a boolean. A later step adds the unsealed tag to a house when its atomics unseal. A house without this tag keeps its atomics sealed.

The project member {?house <name unsealed>} gives the name and unsealed fields of each ?house as one result row. The schema clause also gives the types of these fields to the SDK, so each generated row has the fields Name string and Unsealed *bool. Unsealed is nil when the house does not have the tag.

The export form

export names the definitions that the SDK makes available. The SDK generates a method for each exported mutation and query. A definition that export does not name has no SDK method.

The DUST database reference gives the full syntax of these forms.

Generate the SDK

Create seed/main.go with this program. It opens the database with the SDK plugin enabled and saves the files in one transaction. The plugin generates the SDK after the definitions commit.

go565 B
package main

import (
	"fmt"
	"log"

	"stardust"
)

func main() {
	db, err := stardust.OpenConfig(stardust.Config{
		Plugins: &stardust.Plugins{
			Stdlib:   true,
			Database: &stardust.DatabaseSettings{Path: "dune.db"},
			SDKs: &stardust.SDKSettings{
				Go: &stardust.GoSDKSettings{Directory: "./sdk"},
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	response, err := db.UpdateDefinitionsFromDir("./definitions")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Updated %d namespace(s); generated ./sdk\n", len(response.Namespaces))
}

Run go run ./seed from the module directory. It creates dune.db and the sdk directory.