Fork me on GitHub
#datomic
<
2023-12-26
>
itaied06:12:48

hey all, new to datomic. I'm playing around with transactions and queries, and wondered whether I can store a list of references (cardinality many) and keep the order of the items? For example, inserting the following:

{:item/id 1
 :item/name "name-1"
 :item/children [{:item/id 2 :item/name "name-2"}
                 {:item/id 3 :item/name "name-3"}]
When querying
(d/q '[:find ?name
         :in $ ?id
         :where
         [?e :item/id ?id]
         [?e :item/children ?children]
         [?children :item/name ?name]]
       (d/db conn)
       1)
the children return out of order. what are the options to keep the order?

souenzzo09:12:59

btw, you can have it sorted by "the date that the child was added as a children"

(sort-by first (d/q '[:find ?inst ?name
                      :in $ ?id
                      :where
                      [?e :item/id ?id]
                      [?e :item/children ?children ?tx]
                      [?children :item/name ?name]
                      [?tx :db/txInstant ?inst]]
                 (d/db conn)
                 1))
note that you can also easily sort-by "the that that the child was created"
[?e :item/children ?children]
                      [?children :item/name ?name ?tx]
                      [?tx :db/txInstant ?inst]

itaied09:12:14

it's not date related

itaied09:12:48

@U0510KXTU does it change the structure of my schema? Now the children have an intermediate layer when running queries.

steveb8n10:12:55

Yes, you'll need to unwrap on read and vice versa. I'm guessing you are storing a tree structure. I can vouch for this technique because we use it with diatomic cloud

itaied10:12:46

yes exactly. but what are the benefits of using this technique instead of adding index to my entity? Wrapping my entities means changing of my schema structure

steveb8n10:12:02

That's an option too. We prefer to keep the ordering separate from the domain data. By comparing these options you are at least aware of designs that work

Aviv10:12:23

Hey all, how can I execute functions like the built-in in RDBMS (like sort, group by). I understand that we can execute it in the app (execute sort on the result set), but is it a best practice? Doesn’t it imply fetching the whole dataset? How can I execute a query like: find the top 5 most popular posts (`like` column).

souenzzo11:12:44

> but is it a best practice? In datomic peer/on-prem/Ions, yes. Just use any function to sort and it should perform great, once everything works in-memory etc. In datomic cloud/client, no. IDK the current state of sorting in clinet/cloud libraries.

🙏 1
itaied12:12:46

and what about limit? is it the same (performance wise) to run the query with :limit as to run take n?

souenzzo17:12:04

Used datomic for 5 years across 4 projects. I never had to spend time thinking about limit the results or anything like that. I had performance issues. Usually because of too-much pull *, or some wired :where ordering.

🙌 1