xtdb.api
This namespace is the main public Clojure API to XTDB.
It lives in the `com.xtdb/xtdb-api` artifact - include this in your dependency manager of choice.
To start a node, you will additionally need:
* `xtdb.node`, for an in-process node.
* `xtdb.client`, for a remote client.
execute-tx
(execute-tx node tx-ops)
(execute-tx node tx-ops tx-opts)
Executes a transaction; blocks waiting for the receiving node to index it.
tx-ops: XTQL/SQL style transactions.
[[:put-docs :table {:xt/id "my-id", ...}]
[:delete-docs :table "my-id"]
[:sql "INSERT INTO foo (_id, a, b) VALUES ('foo', ?, ?)"
[0 1]]
[:sql "INSERT INTO foo (_id, a, b) VALUES ('foo', ?, ?)"
[2 3] [4 5] [6 7]]
[:sql "UPDATE foo SET b = 1"]]
Returns a map with details about the submitted transaction, including system-time and tx-id.
opts (map):
- :system-time
overrides system-time for the transaction,
mustn't be earlier than any previous system-time
- :default-tz
overrides the default time zone for the transaction,
should be an instance of java.time.ZoneId
- :authn
a map of user and password if the node requires authentication
plan-q
(plan-q node query)
(plan-q node query opts)
General query execution function for controlling the realized result set.
Returns a reducible that, when reduced (with an initial value), runs the query and yields the result.
`plan-q` returns an IReduceInit object so you must provide an initial value when calling reduce on it.
The main use case for `plan-q` is to stream large results sets without having the entire result set in memory.
A common way to do this is to call run! together with a side-effecting function process-row!
(which could for example write the row to a file):
(run! process-row! (xt/plan-q node ...))
The arguments are the same as for `q`.
q
(q node query)
(q node query opts)
query an XTDB node.
- query: either an XTQL or SQL query.
- opts:
- `:snapshot-time`: see 'Transaction Basis'
- `:current-time`: override wall-clock time to use in functions that require it
- `:args`: arguments to pass to the query.
- `:default-tz`: overrides the default time zone for the query
- `:authn`: authentication options
For example:
(q node '(from ...))
(q node '(from :foo [{:a $a, :b $b}])
{:a a-value, :b b-value})
(q node "SELECT foo.id, foo.v FROM foo WHERE foo.id = 'my-foo'")
(q node ["SELECT foo.id, foo.v FROM foo WHERE foo.id = ?" foo-id])
Please see XTQL/SQL query language docs for more details.
This function returns the results of its query as a vector of maps
Transaction Basis:
In XTDB there are a number of ways to control at what point in time a query is run -
this is done via a snapshot-time basis optionally supplied as part of the query map.
In the case a basis is not provided the query is guaranteed to run sometime after
the latest transaction submitted by this connection/node.
Alternatively a specific snapshot-time can be supplied,
in this case the query will be run exactly at that system-time, ensuring the repeatability of queries.
(q node '(from ...)
{:snapshot-time #inst "2020-01-02"}))
If your node requires authentication you can supply authentication options.
(q node '(from ...)
{:authn {:user "xtdb" :password "xtdb"}})
status
(status node)
(status node opts)
Returns the status of this node as a map,
including details of both the latest submitted and completed tx
Optionally takes a map of options:
- :auth-opts
a map of user and password if the node requires authentication
submit-tx
(submit-tx node tx-ops)
(submit-tx node tx-ops tx-opts)
Writes transactions to the log for processing
tx-ops: XTQL/SQL style transactions.
[[:put-docs :table {:xt/id "my-id", ...}]
[:delete-docs :table "my-id"]
["INSERT INTO foo (_id, a, b) VALUES ('foo', ?, ?)" 0 1]
;; batches
[:sql "INSERT INTO foo (_id, a, b) VALUES ('foo', ?, ?)"
[2 3] [4 5] [6 7]]
"UPDATE foo SET b = 1"]
Returns the tx-id.
opts (map):
- :system-time
overrides system-time for the transaction,
mustn't be earlier than any previous system-time
- :default-tz
overrides the default time zone for the transaction,
should be an instance of java.time.ZoneId
- :authn
a map of user and password if the node requires authentication
template
macro
(template query)
This macro quotes the given query, but additionally allows you to use Clojure's unquote (`~`) and unquote-splicing (`~@`) forms within the quoted form.
Usage:
(defn build-posts-query [{:keys [with-author?]}]
(xt/template (from :posts [{:xt/id id} text
~@(when with-author?
'[author])])))