Fork me on GitHub
#datascript
<
2017-02-28
>
qqq03:02:21

in datascript, when using d/q, is there a way to get the first result instead of a hash set ?

rauh07:02:45

@qqq (d/q '[:find ?e . :where ...]). Note the single dot after ?e

qqq07:02:01

@rauh: got it working; where is this documented?

qqq07:02:12

[there's probably other tricks I want to learn]

rauh07:02:13

In datomic query syntax

qqq07:02:52

hmm, I geuss it's at:

find-spec                  = ':find' (find-rel | find-coll | find-tuple | find-scalar)
find-rel                   = find-elem+
find-coll                  = [find-elem '...']
find-scalar                = find-elem '.

qqq09:02:19

part of my struggle was: (1) datascript says "we're similar to datomic" (2) datascript doesn't fully detail how the query langauges differ (3) so when reading datomic query syntax, it's never clear whether it also applies to datascript

Niki09:02:53

- not, not-join, or and or-join not supported - Custom aggregate functions are called via aggregate keyword - custom query functions and aggregates should be passed as source instead of being referenced by symbol

qqq09:02:54

does this mean datomic/query syntax and datascript/query syntax are identical (minus the features datascript does not support) ?

Niki09:02:08

as far as I know, yes

qqq09:02:44

cool thanks

qqq09:02:07

while you're here; are you aware of any tutorial for datascript + webgl for gui? I've tried google but not found anything yet

Niki09:02:38

no, but I think you can easily combine tutorials on WebGL with datascript

qqq12:02:03

how do I pull out entire entities (with the entity id, and all attribute/value pairs as a map) instead of variables?

qqq12:02:11

the :find ... syntax seems to only pull out those particular variables

qqq12:02:14

I want the entire entity

misha12:02:12

just

(->> @conn
  (ds/q '[:find [?e ...] :where []])
  (ds/pull-many @conn pull-pattern))

misha12:02:53

or

(->> @conn
  (ds/q '[:find [?e ...] :where []])
  (map #(ds/entity @conn %)))

qqq13:02:27

one more dumb question: how do I convert an entity to a map? (assoc (ds/entity ...) :kw ... ) ^^ this fails due to "NO protoco method IAssociative .-assoc deined for type datascript.impl.entity/Entity ...)

qqq13:02:39

so how do I convert ehse Entity to a map so I can assocaite to update them to insert them via d/transact! ?

misha13:02:29

I don't use entities at all (might be missing out on laziness, but oh well), but this question is asked a lot in #datomic as well.

Niki14:02:26

@qqq don’t transact full entities back

Niki14:02:32

transact what have changed only

Niki14:02:08

(ds/q '[:find [(pull ?e [*]) ...] :where ...] db)

Niki14:02:19

you can put pull in query too

qqq22:02:26

I'm looking at http://docs.datomic.com/transactions.html . Suppose I want to add an entity with 3 attributes. Instead of doing

[db/add e a1 v1]
[db/add e a2 v2]
[db/add e a3 v3]
I there a way to add
e, {a1 v1, a2 v2, a3 v3}
?

misha22:02:54

(ds/transact conn {a1 v1 a2 v2})
or, if id is known
(ds/transact conn {:db/id id a1 v1 a2 v2})
or, if id is unknown, but you want to get it from transaction:
(ds/transact conn {:db/id (ds/tempid :db.part/user) a1 v1 a2 v2})

misha22:02:01

and then get generated eventual id from (:temp-ids tx-report)

(let [tid (ds/tempid :db.part/user)
      tx-report (ds/transact! conn {:db/id tid :a1 :v1 :a2 :v2})]
  (get-in tx-report [:temp-ids tid]))