Transactable entities
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
La issue punta a datascript.db/entid in db.cljc e datascript.impl.entity/Entity; inizia leggendo questi percorsi di transaction e Associative. Chiarisci quali semantiche entity-as-ref e assoc sono accettate, quindi definisci una coverage che dimostri che le entità annidate vengono transazionate correttamente e che gli aggiornamenti producono i dati di transaction previsti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
This is a backwards compatible change that would vastly improve DX in my opinion
I've always wondered: why are entities not transactable? I find myself converting entities to maps all the time solely to transact them. This still causes problems when entities nest other entities. So here are a few simple ideas on how entities could be treated in transactions:
1. Entities could be treated as refs in transactions
(def schema
{:user/friends #:db{:valueType :db.type/ref
:cardinality :db.cardinality/many}})
(def ent (d/touch (d/entity @conn 1)))
ent ; eval
;; =>
{:db/id 1
:user/email "[email protected]"
:user/friends #{{:db/id 2} {:db/id 3}}} ; <-- nested entities
Now I convert it to a map
(def ent-map (into {:db/id (:db/id ent)} ent))
ent-map ; eval
;; =>
{:db/id 1
:user/email "[email protected]"
:user/friends #{{:db/id 2} {:db/id 3}}}
;; looks the same but nested entities (under :user/friends) have not been converted
I try to transact it
(d/transact! conn [(assoc ent-map :user/email "[email protected]")])
;; throws:
;; Execution error (ExceptionInfo) at datascript.db/entid (db.cljc:385).
;; Expected number or lookup ref for entity id, got #:db{:id 2}
So I can either dissoc the :user/friends map-entry or convert contained entities to refs
(d/transact! conn [(-> ent-map
(dissoc :genstyle.project/population)
;; OR (update :user/friends #(mapv :db/id %))
(assoc :user/email "[email protected]"))])
We could spare ourselves from this by treating entities as refs in transactions. The database already walks nested data-structures to resolve refs so why not resolve entities as refs, also?
2. Entities to return maps on update
datascript.impl.entity/Entity implements clojure.lang.Associative which currently only throws errors:
clojure.lang.Associative
;; some methods elided
(empty [e] (throw (UnsupportedOperationException.)))
(assoc [e k v] (throw (UnsupportedOperationException.)))
(cons [e [k v]] (throw (UnsupportedOperationException.)))
Instead assoc could return a hashmap
(deftype Entity [db eid touched cache]
;; elided
clojure.lang.Associative
(assoc [e k v]
(let [e-map (cond-> {:db/id eid}
; add other kvals if touched
touched (into e))]
(assoc e-map k v))))
This would also make update possible. Together this means that the change of email to ent from above, could look like this:
(d/transact! conn [(assoc ent :user/email "[email protected]")])
I would've already implemented this for my own projects but unfortunately Clojure (unlike ClojureScript) doesn't allow to overwrite a Type's methods. To achieve this one would have to for DataScript and change the code of datascript.impl.entity/Entity so I wanted to raise the issue here first and see what @tonsky's thoughts are.
This would also unlock a more straightforward use of libraries like meander or specter to walk and update entities.
- Lingua principale
- Clojure
- Stelle
- 5.8k
- Fork
- 318
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di tonsky/datascript
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
tonsky/datascript#498 · 1 commento ·
-
Datascript MCP ServerAperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 10/100
tonsky/datascript#489 ·
-
Stack overflow when transacting :db.type/tupleAttrs with a :db.type/ref attr through :db.fn/callAperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
tonsky/datascript#483 · 2 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
tonsky/datascript#470 · 1 commento ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
tonsky/datascript#441 · 1 commento · 3 reazioni ·
Tutte le issue di tonsky/datascript
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
-
.Needs Triage .Team/UXWest Priority:P3 Type:Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
metabase/metabase#83410 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
contribution welcome good first issue WG adoption
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 91/100
NoahTheDuke/splint#52 · 1 commento ·
-
Setup dependabotAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
software-mansion/starknet-jvm#631 ·