From 79bb94354ccf74df94439e1037045afbf51496c2 Mon Sep 17 00:00:00 2001 From: Coby Tamayo Date: Mon, 30 Sep 2024 21:50:21 -0700 Subject: [PATCH] CLI: Support passing arbitrary query inputs as EDN (#714) * 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 --- .../native-image-tests/run-native-image-tests | 11 +++++++++++ doc/cli.md | 9 ++++++++- src/datahike/cli.clj | 3 +-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/bb/resources/native-image-tests/run-native-image-tests b/bb/resources/native-image-tests/run-native-image-tests index 14f67a545..fb0f05cef 100755 --- a/bb/resources/native-image-tests/run-native-image-tests +++ b/bb/resources/native-image-tests/run-native-image-tests @@ -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 diff --git a/doc/cli.md b/doc/cli.md index 3b75bfa5c..8d5c581e0 100644 --- a/doc/cli.md +++ b/doc/cli.md @@ -42,8 +42,15 @@ value(s), whereas the `db:` 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: diff --git a/src/datahike/cli.clj b/src/datahike/cli.clj index 9454eea30..f92708421 100644 --- a/src/datahike/cli.clj +++ b/src/datahike/cli.clj @@ -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