|
1 | 1 | (ns datascript.datafy |
2 | 2 | (:require [clojure.core.protocols :as cp] |
3 | 3 | [datascript.pull-api :as dp] |
4 | | - [clojure.string :as str])) |
| 4 | + [datascript.db :as db] |
| 5 | + [datascript.impl.entity :as e])) |
5 | 6 |
|
6 | | -(declare datafy-entity) |
7 | 7 | (declare datafy-entity-seq) |
8 | 8 |
|
9 | | -(defn- attr<->rattr [attr] |
10 | | - (keyword (namespace attr) |
11 | | - (if (str/starts-with? (name attr) "_") |
12 | | - (subs (name attr) 1) |
13 | | - (str "_" (name attr))))) |
14 | | - |
15 | 9 | (defn- pull-pattern [ref-rattrs] |
16 | 10 | (into ["*"] ref-rattrs)) |
17 | 11 |
|
18 | | - |
19 | | -(defn- navize-entity [db-val entity] |
| 12 | +(defn- navize-pulled-entity [db-val pulled-entity] |
20 | 13 | (let [ref-attrs (:db.type/ref (:rschema db-val)) |
21 | | - ref-rattrs (set (map attr<->rattr ref-attrs)) |
22 | | - many-attrs (:db.cardinality/many (:rschema db-val)) |
23 | | - pull-pattern (into ["*"] ref-rattrs)] |
24 | | - (with-meta entity |
| 14 | + ref-rattrs (set (map db/reverse-ref ref-attrs)) |
| 15 | + many-attrs (:db.cardinality/many (:rschema db-val))] |
| 16 | + (with-meta pulled-entity |
25 | 17 | {`cp/nav (fn [coll k v] |
26 | 18 | (cond |
27 | 19 | (or (and (many-attrs k) (ref-attrs k)) |
28 | 20 | (ref-rattrs k)) |
29 | | - (datafy-entity-seq db-val |
30 | | - (dp/pull-many db-val pull-pattern (mapv :db/id v))) |
| 21 | + (datafy-entity-seq db-val v) |
31 | 22 | (ref-attrs k) |
32 | | - (datafy-entity db-val (dp/pull db-val pull-pattern (:db/id v))) |
| 23 | + (e/entity db-val (:db/id v)) |
33 | 24 | :else v))}))) |
34 | 25 |
|
35 | | -(defn- navize-entity-seq [db-val entities] |
| 26 | +(defn- navize-pulled-entity-seq [db-val entities] |
36 | 27 | (with-meta entities |
37 | 28 | {`cp/nav (fn [coll k v] |
38 | | - (datafy-entity db-val v))})) |
39 | | - |
40 | | -(defn- datafy-entity [db-val entity] |
41 | | - (with-meta entity |
42 | | - {`cp/datafy (fn [entity] |
43 | | - (navize-entity db-val entity))})) |
| 29 | + (e/entity db-val (:db/id v)))})) |
44 | 30 |
|
45 | 31 | (defn- datafy-entity-seq [db-val entities] |
46 | 32 | (with-meta entities |
47 | | - {`cp/datafy (fn [entities] (navize-entity-seq db-val entities))})) |
48 | | - |
49 | | -(defn pull |
50 | | - "Same as `datascript.core/pull` but the returned map implements `datafy/nav` (See [https://clojure.github.io/clojure/clojure.datafy-api.html](https://clojure.github.io/clojure/clojure.datafy-api.html). |
51 | | -
|
52 | | - This can be used to navigate through the database following ref fields. |
53 | | -
|
54 | | - Usage: |
55 | | - ``` |
56 | | - (pull db [:ref, :name] 1) |
57 | | - ; => {:db/id 1, |
58 | | - ; :name \"Child\", |
59 | | - ; :ref {:db/id 2}} |
60 | | - (nav (datafy (pull db [:ref, :name] 1)) |
61 | | - :ref |
62 | | - {:db/id 2}) |
63 | | - ; => {:db/id 2, |
64 | | - ; :name \"Parent\", |
65 | | - ; :_ref {:db/id 1}} |
66 | | - ``` |
67 | | - " |
68 | | - [db selector eid] |
69 | | - (datafy-entity db (dp/pull db selector eid))) |
70 | | - |
71 | | -(defn pull-many |
72 | | - "Same as [[pull]] but accepts sequence of ids and returns sequence of maps that implements `datafy/nav`." |
73 | | - [db selector eids] |
74 | | - (datafy-entity-seq db (dp/pull-many db selector eids))) |
| 33 | + {`cp/datafy (fn [entities] (navize-pulled-entity-seq db-val entities))})) |
| 34 | + |
| 35 | +(extend-protocol cp/Datafiable |
| 36 | + datascript.impl.entity.Entity |
| 37 | + (datafy [this] |
| 38 | + (let [db (.-db this) |
| 39 | + ref-attrs (:db.type/ref (:rschema db)) |
| 40 | + ref-rattrs (set (map db/reverse-ref ref-attrs)) |
| 41 | + pull-pattern (into ["*"] ref-rattrs)] |
| 42 | + (navize-pulled-entity db (dp/pull db pull-pattern (:db/id this)))))) |
0 commit comments