Datascript 101 - Chapter 315 Jun 2016 clojurescript clojure
We’re going to do some pretty nifty stuff, first lets change the data around a bit, lets do some users instead of cars.
We setup a schema which will help us identify users with a lookup-ref
[:user/id <id>'], then we have a
:user/parent attribute which will point to other refs (other users in our case) and will be an array. We also insert some dummy data.
Level 3 - Querying
First lets try to pull out the IDs of all the users we know of
The first thing to notice here is the data is layed out in a pretty weird format, a set of 1-tuples? what the …
What if I query two attributes?
Ok, nice. This kind of output is called relations (may be shows you how data is related?). We can use special syntax to return other things:
[?e ...] part says that return me the output as a vector. Unsurprisingly, you can only query one attribute at a time like this (pull helps solves this, but that’s for another day).
What if I am expecting only one item to be returned (or can I coerce the output to only return one item?)
?e ., the period of
?e. Here the output was coerced into a “scalar” (single value), doesn’t really make much sense here, but sometimes you’re expecting to find a single thing and this is a nice way of avoiding an
These are some of the variations I’ve found useful. I think there are more (e.g. tuples), but I haven’t really encountered them yet, so that’ll have to wait.
That’s all for now. Took a while to get this post up. Work work work :/.