Skip to content

Instantly share code, notes, and snippets.

@saurabhsharan
Created February 20, 2022 06:50
Show Gist options
  • Save saurabhsharan/7f65765713a7c96f3e60acd8987d61ba to your computer and use it in GitHub Desktop.
Save saurabhsharan/7f65765713a7c96f3e60acd8987d61ba to your computer and use it in GitHub Desktop.
datascript javascript
const datascript = require("datascript");
const edn = require("jsedn");
var parsed = edn.parse(`
#datascript/DB {:schema {:create/user {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :plugin/id {:db/unique :db.unique/identity}, :node/subpages {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :vc/blocks {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :edit/seen-by {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :window/id {:db/unique :db.unique/identity}, :attrs/lookup {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :node/windows {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :d/v {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :block/clone {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :node/sections {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :harc/v {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :node/title {:db/unique :db.unique/identity}, :block/refs {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :harc/a {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :edit/user {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :block/subpage {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :block/children {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :block/focused-user {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :create/seen-by {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :block/uid {:db/unique :db.unique/identity}, :d/e {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :d/a {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :user/uid {:db/unique :db.unique/identity}, :node/links {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :link/to {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :user/email {:db/unique :db.unique/identity}, :query/results {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :harc/e {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :user/display-page {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :block/parents {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/many}, :graph/name {:db/unique :db.unique/identity}, :block/page {:db/valueType :db.type/ref, :db/cardinality :db.cardinality/one}, :version/id {:db/unique :db.unique/identity}}, :datoms [[1 :version/id "0.0.0" 536870913] [1 :version/nonce "uuidef57ee6f-7b7b-4086-b0e3-a5f2a38693b4" 536870913] [1 :version/upgraded-nonce "uuidef57ee6f-7b7b-4086-b0e3-a5f2a38693b4" 536870914] [2 :version/id "0.9.2" 536870914] [2 :version/nonce "uuid05bfcab2-28cf-4c8d-a26c-94588f700fcf" 536870949] [3 :block/uid "Gp2a3E3G4" 536870915] [3 :user/display-page 4 536870915] [3 :user/uid "XPMgg60H17RHUDNdndx7IHrG3Gd2" 536870915] [4 :block/uid "BO3g8tinP" 536870915] [4 :create/time 1644297504971 536870916] [4 :create/user 3 536870916] [4 :edit/time 1644297504971 536870916] [4 :edit/user 3 536870916] [4 :node/title "Anonymous" 536870916] [5 :block/uid "02-07-2022" 536870917] [5 :create/time 1644297504973 536870917] [5 :create/user 3 536870917] [5 :edit/time 1644297504973 536870917] [5 :edit/user 3 536870917] [5 :log/id 1644297504973 536870917] [5 :node/title "February 7th, 2022" 536870917] [6 :block/children 7 536870919] [6 :block/children 8 536870921] [6 :block/uid "ZZ-zKnc88" 536870918] [6 :create/time 1644297513451 536870918] [6 :create/user 3 536870918] [6 :edit/time 1644297513452 536870918] [6 :edit/user 3 536870918] [6 :node/title "Page 1" 536870918] [7 :block/open true 536870919] [7 :block/order 0 536870919] [7 :block/page 6 536870920] [7 :block/parents 6 536870920] [7 :block/string "Block 1 on Page 1" 536870923] [7 :block/uid "rhD_aem3K" 536870919] [7 :create/time 1644297514008 536870919] [7 :create/user 3 536870919] [7 :edit/time 1644297516109 536870923] [7 :edit/user 3 536870919] [8 :block/children 10 536870931] [8 :block/open true 536870921] [8 :block/order 1 536870921] [8 :block/page 6 536870922] [8 :block/parents 6 536870922] [8 :block/string "Block 2 on Page 1" 536870930] [8 :block/uid "lKih6PS0P" 536870921] [8 :create/time 1644297516102 536870921] [8 :create/user 3 536870921] [8 :edit/time 1644297518570 536870930] [8 :edit/user 3 536870921] [10 :block/children 11 536870936] [10 :block/open true 536870928] [10 :block/order 0 536870931] [10 :block/page 6 536870929] [10 :block/parents 6 536870929] [10 :block/parents 8 536870932] [10 :block/string "Sub-block of Block 2 on Page 1" 536870935] [10 :block/uid "BKc6nA4oz" 536870928] [10 :create/time 1644297518566 536870928] [10 :create/user 3 536870928] [10 :edit/time 1644297523772 536870935] [10 :edit/user 3 536870928] [11 :block/open true 536870933] [11 :block/order 0 536870936] [11 :block/page 6 536870934] [11 :block/parents 6 536870934] [11 :block/parents 8 536870934] [11 :block/parents 10 536870937] [11 :block/refs 8 536870941] [11 :block/string "Sub-block of sub-block of ((lKih6PS0P))" 536870940] [11 :block/uid "C95Xj3C3i" 536870933] [11 :create/time 1644297523768 536870933] [11 :create/user 3 536870933] [11 :edit/time 1644297534280 536870940] [11 :edit/user 3 536870933] [13 :block/children 14 536870944] [13 :block/children 15 536870946] [13 :block/uid "fFtpws2Nb" 536870943] [13 :create/time 1644297540407 536870943] [13 :create/user 3 536870943] [13 :edit/time 1644297540407 536870943] [13 :edit/user 3 536870943] [13 :node/title "Page 2" 536870943] [14 :block/open true 536870944] [14 :block/order 0 536870944] [14 :block/page 13 536870945] [14 :block/parents 13 536870945] [14 :block/string "First block on Page 2" 536870948] [14 :block/uid "l6ALokZie" 536870944] [14 :create/time 1644297540962 536870944] [14 :create/user 3 536870944] [14 :edit/time 1644297542922 536870948] [14 :edit/user 3 536870944] [15 :block/open true 536870946] [15 :block/order 1 536870946] [15 :block/page 13 536870947] [15 :block/parents 13 536870947] [15 :block/string "Second block on Page 2" 536870949] [15 :block/uid "38z-IF47U" 536870946] [15 :create/time 1644297542917 536870946] [15 :create/user 3 536870946] [15 :edit/time 1644297545426 536870949] [15 :edit/user 3 536870946]]}
`);
var jsObj = edn.toJS(parsed);
var schema = jsObj.value[":schema"];
var datoms = jsObj.value[":datoms"];
var db = datascript.init_db(datoms, schema);
var q = "[:find ?e ?n :in $ :where [?e :node/title ?n]]";
var result = datascript.q(q, db);
// This throws:
// ./node_modules/datascript/datascript.js:173
// function ge(a,b){if(a===b)return 0;if(null==a)return-1;if(null==b)return 1;if("number"===typeof a){if("number"===typeof b)return na(a,b);throw Error(["Cannot compare ",v.c(a)," to ",v.c(b)].join(""));}if(null!=a?a.F&2048||n===a.kc||(a.F?0:hb(xc,a)):hb(xc,a))return yc(a,b);if("string"!==typeof a&&!cb(a)&&!0!==a&&!1!==a||ib(a)!==ib(b))throw Error(["Cannot compare ",v.c(a)," to ",v.c(b)].join(""));return na(a,b)}
// Error: Cannot compare :block/uid to :node/title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment