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.
For an in-process node, you will additionally need `xtdb.node`, in the `com.xtdb/xtdb-core` artifact.
For a remote node, connect using the `xtdb.api/client` function.
client
(client conn-opts)
Open up a client to a (possibly) remote XTDB node
* `:host`: the hostname or IP address of the database (default: `127.0.0.1`)
* `:port`: the port for the database connection (default: `5432`)
* `:user`: the username to authenticate with
* `:password`: the password to authenticate with
* `:dbname`: the database to connect to (default: `xtdb`)
See `next.jdbc/get-datasource` for more options.
execute-tx
(execute-tx connectable tx-ops)
(execute-tx connectable 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"]]
If the transaction fails - either due to an error or a failed assert, this function will throw.
Otherwise, 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 connectable query+args)
(plan-q connectable query+args 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/connection.
- 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
- `:default-tz`: overrides the default time zone for the query
For example:
(q conn '(from ...))
(q conn ['(fn [a b] (from :foo [a b])) a-value b-value])
(q conn ['#(from :foo [{:a %1, :b %2}]) a-value b-value])
(q conn ['#(from :foo [{:a %} b]) a-value])
(q conn "SELECT foo.id, foo.v FROM foo WHERE foo.id = 'my-foo'")
(q conn ["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 conn '(from ...)
{:snapshot-time #inst "2020-01-02"}))
status
(status connectable)
Returns the status of this node as a map
submit-tx
(submit-tx connectable tx-ops)
(submit-tx connectable 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
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])])))