(ns eql-hodur | |
(:require [edn-query-language.core :as eql] | |
[clojure.spec.alpha :as s] | |
[datascript.core :as ds])) | |
(defn add-prop | |
[prop {:keys [children] | |
:as node}] | |
(if (contains? node :children) | |
(assoc node | |
:children (cond-> (mapv (partial add-prop prop) | |
children) | |
(not (contains? (set (map :dispatch-key children)) | |
prop)) (conj {:type :prop | |
:dispatch-key prop | |
:key prop}))) | |
node)) | |
(s/fdef add-prop | |
:args (s/cat :prop (s/and :edn-query-language.ast/dispatch-key | |
:edn-query-language.ast/key) | |
:node :edn-query-language.ast/node) | |
:ret :edn-query-language.ast/node) | |
(defn hodur-pull->ast | |
[{nsk :type/kebab-case-name | |
:field/keys [kebab-case-name | |
_parent | |
type]}] | |
(if _parent | |
{:type :root | |
:children (for [el _parent] | |
(hodur-pull->ast (assoc el :type/kebab-case-name nsk)))} | |
(let [k (keyword (name nsk) | |
(name kebab-case-name))] | |
(if (-> type :field/_parent) | |
(let [ast (hodur-pull->ast type)] | |
(assoc ast | |
:type :join | |
:query (eql/ast->query ast) | |
:key k | |
:dispatch-key k)) | |
{:type :prop | |
:key k | |
:dispatch-key k})))) | |
(defn hodur-entity-full-pattern | |
[{::keys [hodur-db hodur-pull-limit]} ident] | |
(->> (ds/pull hodur-db [:type/kebab-case-name | |
:field/kebab-case-name | |
{:field/type hodur-pull-limit} | |
{:field/_parent hodur-pull-limit}] | |
ident) | |
hodur-pull->ast | |
(add-prop :db/id) | |
eql/ast->query)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment