Skip to content

Commit

Permalink
CLI: Support passing arbitrary query inputs as EDN (#714)
Browse files Browse the repository at this point in the history
* only configure stderr logging outside Babashka

* parse CLI input args as EDN

* delete attr-ref db during test cleanup

* restore previous formatting

* document example of EDN-parsed input arg

---------

Co-authored-by: Coby Tamayo <[email protected]>
  • Loading branch information
acobster and Coby Tamayo authored Oct 1, 2024
1 parent ddff1ac commit 79bb943
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
11 changes: 11 additions & 0 deletions bb/resources/native-image-tests/run-native-image-tests
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,15 @@ fi
./dthk query '[:find ?e . :where [?e :name ?n]]' db:$CONFIG --format cbor >> /tmp/test
./dthk query '[:find ?i :in $ ?i . :where [?e :name ?n]]' db:$CONFIG cbor:/tmp/test # => 1

# test arbitrary :in[puts] as positional args
QUERY_OUT=`./dthk query '[:find (pull ?e [*]) . :in $ ?name :where [?e :name ?name]]' db:$CONFIG '"Judea"'`
if [ "$QUERY_OUT" = '{:db/id 100001, :name "Judea"}' ]
then
echo "Positional input test successful."
else
echo "Exception: Query did not return correct value."
exit 1
fi

./dthk delete-database $CONFIG
./dthk delete-database $ATTR_REF_CONFIG
9 changes: 8 additions & 1 deletion doc/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,15 @@ value(s), whereas the `db:<file>` argument to `query` comes after the query
value, mirroring the Clojure API. As an added benefit, this also allows passing
multiple db configuration files prefixed with `db:` for joining over arbitrary
many databases or data files with "edn:" or "json:". Everything non-prefixed is
read in as `edn` and passed to the query engine as well.
read in as `edn` and passed to the query engine as well:

```bash
$ dthk query '[:find ?e . :in $ ?name :where [?e :name ?name]]' db:myconfig.edn '"Linus"'
123
```

When passing strings as EDN, make sure to enclose double quotes as part of the
command-line arg value. Otherwise it will be parsed as a symbol.

Provided the filestore is configured with `{:in-place? true}` you can even write
to the same database without a dedicated daemon from different shells:
Expand Down
3 changes: 1 addition & 2 deletions src/datahike/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@
nil
input->db)]
res
(throw (ex-info "Input format not know." {:type :input-format-not-known
:input s}))))
(edn/read-string s)))

(defn report [format out]
(case format
Expand Down

0 comments on commit 79bb943

Please sign in to comment.