Db not syncing after transaction

  1. Create a connection and a db:
    (def conn (d/connect client {:db-name “users”}))
    (def db (d/db conn))

  2. Add a user:
    (d/transact conn {:tx-data [{:user/name “Some name”}]}))

  3. Query for user:
    (d/q '[:find ?ent
    :in $ ?name
    :where [?ent :user/name ?name]]
    db, “Some name”)))

User is not found, unless I redo (def db (d/db conn))

Is this the expected behavior?

Yep this is expected behavior. In order to issue a query against a Datomic database, you must retrieve the current database value. A database value is the state of the database at a given point in time. You can issue as many queries against that database value as you want, they will always return the same results. This is part of the value add of Datomic :).

You can always get the current DB in your query by doing the following:

(d/q '[:find ?ent
:in $ ?name
:where [?ent :user/name ?name]]
(d/db conn), “Some name”)))
1 Like

In addition to what @jaret said, it’s often advantageous to use the :db-after provided by d/transact fn return map, when a flow of logic needs to transact data and run queries on the result of that data

2 Likes

Thank you, it became a little clearer :slight_smile: