I keep seeing { :aliases { :db/cardinality :db.cardinality/many } }
as a schema in examples. what does it do?
I also see transactions with negative :db/id
. why is that?
also, I know that a lot of the core library operates on an atom “connection”. are these changes executed synchronously?
I want to pretty much ignore the atom and use my own state container
OK, so it sounds like the alias
is being defined as an attribute which is cardinality many. got it.
it also sounds like negative db/id’s are used as temporary IDs?
Yes. You can use negative ids or arbitrary strings. All matching ids will resolve to the same real id inside single transaction
That is precisely the case. You can use negative IDs within your transaction to establish relationships, DataScript will fill-in the correct ones.
I have another newbie question. I’m playing around with moving parts of my application to DataScript
my first attempt is at routing. here’s my initial transaction:
[{:db/id -1
:router/current :tap-list
:router/routes [{:id :tap-list
:label "Taps"}
{:id :inspector
:label "Inspector"}]}]
Instead of route/current at separate entity I usually assign :route/current? true
flag on the route that is currently selected. That way you can look up via [:route/current? true] and get the currently selected route (don’t forget to mark it unique/identity)
thanks!
this is kind of weird, though, because routing is a kind of singleton thing. I’m not sure how to write the transaction to update this yet
thinking about this now: perhaps it would be better to model it as a cardinality-many of history, and query for the latest one
gotta figure out how to preserver order..
I usually model singletons with idents. Idents are implemented via a separate attribute with the {:db/unique :db.unique/identity} property. Nowadays, this comes built-in (https://github.com/tonsky/datascript/blob/master/src/datascript/db.cljc#L24). In your example, you could the replace {:db/id -1} with {:db/ident :router} and use that throughout your app.