datascript 2022-01-07

Would anyone here have any pointers as to the best way to interop with R. I have a cljs implementation of an R coxph model and I need to write a coverage test that checks the cljs against the R code?

Hey, I have no idea what would actually work best in your situation, but I did just discover this possibility of calling graaljs (e.g. using compiled cljs) from this Truffle-based R-Graal implementation: https://github.com/oracle/fastr/blob/1ce6f36bec5c5f8520ab0f0f330f9dee23ed7c55/documentation/user/Interoperability.md Out of interest, are you using DataScript in the cljs as the underlying engine for the regression model? Or is it just one part of the picture?

You could also consider, as a first pass, avoiding interop entirely. Might not be worth it. Could either shell out to R from CLJS or have separate R and CLJS programs that write the results of the test expressions to disk and then have a third program examine and compare the results.

✔️ 1
➕ 1

hello! I’m trying to load in a serialized db using the JS API and can’t get it to work. I have the database in EDN format. I’ve tried several things, but this is the latest:

let ds = require('datascript');
let edn = require('jsedn');

let d = require('fs').readFileSync('./persona.edn', 'utf8');
let data = edn.parse(d);

let conn = ds.conn_from_datoms(
  edn.toJS(edn.atPath(data, ':datoms')),
  edn.toJS(edn.atPath(data, ':schema'))
);

let query = '[:find ?id :where [?b :block/uid ?id]]';
let result = ds.q(query, conn);

console.log(result);
conn is created fine but when running the query I get [object Object] is not ISeqable. Looking at the code, it doesn’t seem to run js->clj on the value of the datom, is that the problem? I can’t find any examples for doing this through JS

i think you need to get the current db from the conn

let result = ds.q(query, ds.db(conn));

translated to clojure:

(def result (ds/q query @conn))

nice! that does get a lot closer

it runs but the query errors with Cannot compare :block/parents to :block/refs. I know this might be specific to my db, but that query works from the place I exported the db from. I’m not referencing :block/parents at all — any ideas where I should look?

might be the schema

i'm getting similar errors with pretty basic data/schema

it works when i use string keys

let ds = require('datascript');
let edn = require('jsedn');

let d = require('fs').readFileSync('./persona.edn', 'utf8');
let data = edn.parse(d);

// let schema = edn.toJS(edn.atPath(data, ':schema'));
let schema = {"person/id": {":db/unique": ":db.unique/identity"}};

//let datoms = edn.toJS(edn.atPath(data, ':datoms'));
let datoms = [[1, "person/id", 1],
              [1, "person/name", "Will"]];

let conn = ds.conn_from_datoms(
    datoms, schema
);

let query = '[:find ?v :where [?p "person/id" ?v]]';
let result = ds.q(query, ds.db(conn));

console.log(result);

hmm i see. datascript does not convert strings to keywords other than ds-specific strings like :db/id and schema definition keywords

jsedn toJS stringifies keywords.

so you'll need to wrap your keyword attrs you're loading from disk in quotes in your query

i.e.

let query = '[:find ?id :where [?b ":block/uid" ?id]]';

not the nicest but unless you're going to read the EDN in as actual CLJS data yourself that might be as good as you get

also not sure what will happen if you try and round trip the db, i.e. make changes to the db and then write it back out to disk

very nice that works, thank you!

I don’t need changes, my project is just read-only for this 🙂

I think the key their is they're parsing the query string using edn.parse

so that turns it into JS data. then datascript reads that JS data, and treats the attributes it finds as strings

so in your case that would work well because you're parsing the datums into JS objs and then you could parse the query string as well

yeah, thanks! I’ve tried it though and something is weird. why do that pass query as an array? d.q([query], …)

I’ve tried let result = ds.q(edn.parse(query), ds.db(conn)); which gives No protocol method ICounted.-count defined

and if I wrap it in an array, this.ja.charAt is not a function

yeah that is weird. if you note in that example they do not wrap the query string in a vector, it's just kws and variables

maybe that example is just out of date

it’s from 2016, so likely