How to use a dynamic attribute? #444
Replies: 2 comments 2 replies
-
|
I don’t think there’s a way to do this with collection. If you import it into datascript database, you can certainly do it, kind of like you described. You’ll need to add :db.cardinality/many to schema to :children, otherwise it should work. But it directly on collection, it probably wouldn’t |
Beta Was this translation helpful? Give feedback.
-
|
Dynamic attributes in DataScript — depending on what you mean by "dynamic," there are a few different approaches: If you mean attributes not defined at schema initialization: (d/transact! conn [{:db/id 1
:my.dynamic/attribute "value"}])The limitation: without schema, you can't use reverse lookups, unique constraints, or cardinality-many. You also can't use If you mean attributes with dynamic keys (computed attribute names): (let [attr-name (keyword "user" computed-suffix)]
(d/transact! conn [{:db/id entity-id
attr-name value}]))This works in DataScript — keywords are first-class values and can be composed at runtime. If you mean EAV-style open attributes: If you mean pulling attributes whose names you don't know upfront: (d/pull db ['*] entity-id) ; returns all attributesWhat's the specific use case — entity metadata with dynamic keys, user-defined fields, or something else? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to query an AST created with
clojure.tools.analyzer, where nodes look something like{:op :if :children [:condition :then :else] :condition {...} :then {...} :else {...}}I want to create a
childrule, and I tried doingHowever this doesn't seem to work - no results from the query. How can I achieve what I want?
Beta Was this translation helpful? Give feedback.
All reactions