-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (68 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import mustache from "mustache";
import { createSchema } from "./lib/db_schema.js";
import { createClient, createDatabase, WQ } from "./lib/db_setup.js";
import { findQueryCalls } from "./lib/mustache_util.js";
async function findQuery(client, queryId) {
const result = await WQ.limit(1)
.select("v:body")
.and(
WQ.triple(queryId, "type", "scm:Query"),
WQ.triple(queryId, "scm:Query.body", "v:body")
)
.execute(client)
.catch(console.log);
if (result && result.bindings && result.bindings[0]) {
return result.bindings[0].body["@value"];
}
}
async function renderTopic(client, docId) {
const result = await WQ.limit(1)
.select("v:body")
.and(
WQ.triple(docId, "type", "scm:Topic"),
WQ.triple(docId, "scm:Topic.body", "v:body")
)
.execute(client)
.catch(console.log);
if (!result.bindings.length) {
console.log(`Couldn't find topic ${docId}`);
return;
}
const body = result.bindings[0].body["@value"];
const view = {};
for (let { token, query, args, error } of findQueryCalls(body)) {
if (error) {
console.log(error);
} else {
const code = await findQuery(client, `doc:query.${query}`);
const fun = eval(`(${code})`);
const { defaultField, output } = await fun(client, ...(args || []));
view[token] = output[defaultField];
}
}
const output = mustache.render(body, view);
console.log("INPUT");
console.log(body);
console.log();
console.log("OUTPUT");
console.log(output);
}
async function run() {
const client = createClient();
await client.connect().catch(console.log);
await createDatabase(client, true);
await createSchema(client);
await WQ.insert("me", "Person")
.property("Person.firstName", "Emmanuel")
.property("Person.lastName", "Oga")
.execute(client, "First doc!")
.catch(console.log);
await WQ.insert("wq1", "Topic")
.property("Topic.body", `<div>A very cool document from {{person("doc:me")}}.</div>`)
.execute(client, "A document with a query.")
.catch(console.log);
await renderTopic(client, "wq1").catch(console.log);
}
run().then(() => {
console.log("Done.");
});