tonsky / datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
Eclipse Public License 1.0
5.45k stars 304 forks source link

Referencing other entities #10

Closed frankiesardo closed 10 years ago

frankiesardo commented 10 years ago

I apologise if it's just me, but by reading the docs it's not very clear how to reference other entities inside datascript.

[
 {:db/id -10
  :name "C1"
  :value "foo"} 
 {:db/id -11
  :name "C2"
  :value "bar"}      
 {:db/id -1
  :name  "E"
  :components [-10, -11]}
]

How do I reference the generated ids 10 and 11 inside the components vector?

tonsky commented 10 years ago

Yeah, I forgot about that case :( Maybe we should go with something like Datomic’s tempids or deduce value type from schema. Will think of something for the next version

yusefnapora commented 10 years ago

Have you given this any more thought? It seems like specifying the value type in the schema has the advantage that you could return referenced entities in the d/entity function, similar to Datomic. So, in frankiesardo's example, you could do

(def e (d/entity db 1))
(get-in e [:components 0 :name]) ; => "foo"

Of course, d/entity would have to be rewritten to support that. But it seems like for that to be possible, you would want the type to be in the schema.

tonsky commented 10 years ago

I’ll probably go with the schema, and leave negative ids as they works right now, without tempid wrapper. Entity to follow references is a little bit harder because there may be cycles so we need lazy entities for this to work properly. It’s on the roadmap, but not on the closest one.

tonsky commented 10 years ago

Check out test.datascript/test-resolve-eid-refs for usage example