In the last section, we made a HTTP request that created a 404. We'll use the trace of that HTTP request to build an endpoint, a technique we call Trace Driven Development.
- Hit the plus (+) button in the 404 section of the sidebar. This creates a HTTP endpoint with the HTTP verb (POST) and path (/test) set from the request that creates the 404.
- Hover over the white dot on the left hand side. Here, you can see the full body of the incoming trace from when you posted to the endpoint from the REPL, including the body.
- Let's save the
requestbody to a variable by typing
let data = request.body. As you can see, the autocomplete knows the field names of
requestfrom the trace, and will complete them for you.
- Let's put this in a datastore. Like we saw with the
HttpClientlibrary, typing "DB" in the blank will pull up all datastore functions.
In this case, we want
DB::set, which takes three arguments.
- Now let's make our datastore. From the sidebar or the omnibox, create a new
datastore called "Requests", with fields
Dark's datastores are key-value stores; each record has a unique key that is used to store and retrieve the value. For instance, a possible set of records with keys in the above datastore would look like this:
Now that we have a datastore, let's finish our post endpoint.
The first argument is the record we're inserting. This needs to match the schema, and so it needs to include both
time(you cannot insert records that are missing fields). Insert our data from above (seeing the live value to the left) and use the
Date::nowfunction to get the
- We need a unique key for each record that we store. The
DB::generateKeyfunction generates random keys – this is useful when your record has no obvious unique identifier.
- The final argument is the Datastore:
- To call the
DB::setfunction, we first need to run the two functions with side effects (
Date::now) by hitting the play button. After hitting those two play buttons, the
DB::setplay button will be enabled:
- Finally, hitting the play button for
DB::setinserts the record into the datastore. This locks the datastore, preventing us from changing the schema, which we see indicated by the red lock (🔒) icon.