Establishes version control for the personal Logseq DB fork before starting the server-client architecture migration and domain features described in docs/agent-guide and the approved implementation plan. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
57 lines
2.0 KiB
Clojure
57 lines
2.0 KiB
Clojure
(ns logseq.db-sync.test-sql
|
|
(:require [clojure.string :as string]))
|
|
|
|
(defn- js-key [k]
|
|
(cond
|
|
(keyword? k) (string/replace (name k) "-" "_")
|
|
(string? k) k
|
|
:else (str k)))
|
|
|
|
(defn- js-row [m]
|
|
(let [o (js-obj)]
|
|
(doseq [[k v] m]
|
|
(aset o (js-key k) v))
|
|
o))
|
|
|
|
(defn- js-rows [rows]
|
|
(into-array (map js-row rows)))
|
|
|
|
(defn make-sql []
|
|
(let [state (atom {:tx-log {}
|
|
:meta {}})]
|
|
#js {:exec (fn [sql & args]
|
|
(cond
|
|
(string/includes? sql "insert into tx_log")
|
|
(let [[t tx created-at outliner-op] args]
|
|
(swap! state update :tx-log assoc t {:t t
|
|
:tx tx
|
|
:created-at created-at
|
|
:outliner-op outliner-op})
|
|
nil)
|
|
|
|
(string/includes? sql "select t, tx, outliner_op from tx_log")
|
|
(let [since (first args)
|
|
rows (->> (:tx-log @state)
|
|
vals
|
|
(filter (fn [row] (> (:t row) since)))
|
|
(sort-by :t)
|
|
(map (fn [row] {:t (:t row)
|
|
:tx (:tx row)
|
|
:outliner-op (:outliner-op row)})))]
|
|
(js-rows rows))
|
|
|
|
(string/includes? sql "insert into sync_meta")
|
|
(let [[k v] args]
|
|
(swap! state update :meta assoc k v)
|
|
nil)
|
|
|
|
(string/includes? sql "select value from sync_meta")
|
|
(let [k (first args)
|
|
value (get-in @state [:meta k])]
|
|
(if (some? value)
|
|
(js-rows [{:value value}])
|
|
(js-rows [])))
|
|
|
|
:else
|
|
nil))}))
|