|
1238 | 1238 | (raise "Bad attribute type: " attr ", expected keyword or string" |
1239 | 1239 | {:error :transact/syntax, :attribute attr}))) |
1240 | 1240 |
|
| 1241 | +(defn resolve-tuple-refs [db a vs] |
| 1242 | + (mapv |
| 1243 | + (fn [a v] |
| 1244 | + (if (and (ref? db a) (sequential? v)) ;; lookup-ref |
| 1245 | + (entid-strict db v) |
| 1246 | + v)) |
| 1247 | + (-> db -schema (get a) :db/tupleAttrs) vs)) |
| 1248 | + |
1241 | 1249 | (defn+ ^number entid [db eid] |
1242 | 1250 | {:pre [(db? db)]} |
1243 | 1251 | (cond |
|
1250 | 1258 | (let [[attr value] eid] |
1251 | 1259 | (cond |
1252 | 1260 | (not= (count eid) 2) |
1253 | | - (raise "Lookup ref should contain 2 elements: " eid |
1254 | | - {:error :lookup-ref/syntax, :entity-id eid}) |
| 1261 | + (raise "Lookup ref should contain 2 elements: " eid |
| 1262 | + {:error :lookup-ref/syntax, :entity-id eid}) |
| 1263 | + |
1255 | 1264 | (not (is-attr? db attr :db/unique)) |
1256 | | - (raise "Lookup ref attribute should be marked as :db/unique: " eid |
1257 | | - {:error :lookup-ref/unique, :entity-id eid}) |
| 1265 | + (raise "Lookup ref attribute should be marked as :db/unique: " eid |
| 1266 | + {:error :lookup-ref/unique, :entity-id eid}) |
| 1267 | + |
| 1268 | + (tuple? db attr) |
| 1269 | + (let [value' (resolve-tuple-refs db attr value)] |
| 1270 | + (-> (-datoms db :avet attr value' nil nil) first :e)) |
| 1271 | + |
1258 | 1272 | (nil? value) |
1259 | | - nil |
| 1273 | + nil |
| 1274 | + |
1260 | 1275 | :else |
1261 | | - (-> (-datoms db :avet attr value nil nil) first :e))) |
| 1276 | + (-> (-datoms db :avet attr value nil nil) first :e))) |
1262 | 1277 |
|
1263 | 1278 | #?@(:cljs [(array? eid) (recur db (array-seq eid))]) |
1264 | 1279 |
|
|
1838 | 1853 | (allocate-eid v resolved) |
1839 | 1854 | (update ::value-tempids assoc resolved v))] |
1840 | 1855 | (recur report' es))) |
| 1856 | + |
| 1857 | + (and |
| 1858 | + (or (= op :db/add) (= op :db/retract)) |
| 1859 | + (not (::internal (meta entity))) |
| 1860 | + (tuple? db a) |
| 1861 | + (not= v (resolve-tuple-refs db a v))) |
| 1862 | + (recur report (cons [op e a (resolve-tuple-refs db a v)] entities)) |
1841 | 1863 |
|
1842 | 1864 | (tempid? e) |
1843 | 1865 | (let [upserted-eid (when (is-attr? db a :db.unique/identity) |
|
1861 | 1883 | (raise "Conflicting upsert: " e " resolves to " upserted-eid " via " entity |
1862 | 1884 | {:error :transact/upsert}))) |
1863 | 1885 |
|
1864 | | - (and (not (::internal (meta entity))) |
| 1886 | + (and |
| 1887 | + (not (::internal (meta entity))) |
1865 | 1888 | (tuple? db a)) |
1866 | 1889 | ;; allow transacting in tuples if they fully match already existing values |
1867 | 1890 | (let [tuple-attrs (get-in db [:schema a :db/tupleAttrs])] |
|
0 commit comments