datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
lilactown 2019-05-12T16:31:41.025700Z

I keep seeing { :aliases { :db/cardinality :db.cardinality/many } } as a schema in examples. what does it do?

lilactown 2019-05-12T16:32:40.026100Z

I also see transactions with negative :db/id. why is that?

lilactown 2019-05-12T16:35:53.026700Z

also, I know that a lot of the core library operates on an atom “connection”. are these changes executed synchronously?

lilactown 2019-05-12T16:36:07.027100Z

I want to pretty much ignore the atom and use my own state container

lilactown 2019-05-12T16:58:22.027700Z

OK, so it sounds like the alias is being defined as an attribute which is cardinality many. got it.

lilactown 2019-05-12T16:59:24.028100Z

it also sounds like negative db/id’s are used as temporary IDs?

2019-05-13T14:56:57.039300Z

Yes. You can use negative ids or arbitrary strings. All matching ids will resolve to the same real id inside single transaction

ncg 2019-05-12T17:24:42.028900Z

That is precisely the case. You can use negative IDs within your transaction to establish relationships, DataScript will fill-in the correct ones.

lilactown 2019-05-12T17:44:33.029500Z

I have another newbie question. I’m playing around with moving parts of my application to DataScript

lilactown 2019-05-12T17:45:11.030100Z

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"}]}]

2019-05-13T14:55:37.039100Z

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)

👍 1
lilactown 2019-05-13T15:03:17.040700Z

thanks!

lilactown 2019-05-12T17:45:45.030700Z

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

lilactown 2019-05-12T17:47:45.031300Z

thinking about this now: perhaps it would be better to model it as a cardinality-many of history, and query for the latest one

lilactown 2019-05-12T17:47:51.031500Z

gotta figure out how to preserver order..

ncg 2019-05-12T23:09:14.033900Z

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.