Last active
October 13, 2023 23:39
-
-
Save briansunter/b7843ada6b8460ba61dd32644d5ecbbe to your computer and use it in GitHub Desktop.
nbb-logseq-typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns logseq.bb-tasks.nbb.cached-db | |
"Handles nbb side of cached-db functionality. File path and cache implementation | |
encapsulated in this ns" | |
(:require [datascript.transit :as dt] | |
[datascript.core :as d] | |
[logseq.graph-parser.cli :as gp-cli] | |
[logseq.graph-parser :as graph-parser] | |
[clojure.string :as string] | |
[logseq.db.rules :as rules] | |
[clojure.edn :as edn] | |
["fs" :as fs])) | |
(def ^:private cache-file | |
"Cache file for storing transit db" | |
".cached-db-transit.json") | |
(defn ^:api read-db | |
"Reads db from cache file" | |
[] | |
(dt/read-transit-str (fs/readFileSync cache-file))) | |
(defn write-db | |
"Writes cache db file given the graph directory. First time writing the cache | |
can take long for large graphs since all files are parsed. Subsequent writes | |
are quick as the cache only needs to update changed files." | |
[dir changed-files] | |
(if (fs/existsSync cache-file) | |
(let [old-conn (d/conn-from-db (read-db)) | |
files (->> changed-files | |
;; Can remove if the graph-parser filters with frontend.util.fs/ignored-path? | |
(remove #(string/includes? % "logseq/bak")) | |
(map #(hash-map :file/path % | |
:file/content (str (fs/readFileSync %))))) | |
delete-blocks (fn [db file-page file-path uuid-blocks] | |
(into (graph-parser/get-blocks-to-delete db file-page file-path uuid-blocks) | |
;; Delete page to allow for page properties to be redefined | |
;; Assumes no page rename for now | |
[[:db.fn/retractEntity [:block/name (:block/name file-page)]]])) | |
conn (:conn (gp-cli/parse-graph dir | |
{:conn old-conn | |
:files files | |
:verbose true | |
:parse-file-options {:delete-blocks-fn delete-blocks}}))] | |
(println "Updating cache file" cache-file) | |
(fs/writeFileSync cache-file (dt/write-transit-str @conn))) | |
(let [conn (:conn (gp-cli/parse-graph dir {:verbose false}))] | |
(println "Writing cache file" cache-file) | |
(fs/writeFileSync cache-file (dt/write-transit-str @conn)))) | |
nil) | |
(defn- main [graph-dir query-str] | |
(let [cache-exists? (fs/existsSync cache-file) | |
conn (if cache-exists? | |
(d/conn-from-db (read-db)) | |
(do | |
(let [conn (:conn (gp-cli/parse-graph graph-dir {:verbose false}))] | |
(write-db graph-dir []) | |
conn))) | |
query* (edn/read-string query-str) | |
query (into query* [:in '$ '%]) ;; assumes no :in are in queries | |
results (map first (apply d/q query @conn [(vals rules/query-dsl-rules)]))] | |
(clj->js results))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { loadFile } from "@logseq/nbb-logseq"; | |
import { fileURLToPath } from "url"; | |
import { dirname, resolve } from "path"; | |
const __dirname = fileURLToPath(dirname(import.meta.url)); | |
const logseqPath = resolve(__dirname, "../logseq"); | |
const cachedDbPath = resolve(__dirname, "cached_db.cljs"); | |
const main = await loadFile(cachedDbPath); | |
const pagesQuery = `[:find (pull ?p [*]) | |
:where | |
[?p :block/properties ?pr] | |
[(get ?pr :public) ?t] | |
[(= true ?t)] | |
[?p :block/created-at] | |
[?p :block/updated-at]]`; | |
const result = main.root(logseqPath, pagesQuery); | |
console.log(result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module '@logseq/nbb-logseq' { | |
export function loadString(input: string): Promise<any>; | |
export function loadFile(input: string): Promise<any>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment