-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
66 lines (62 loc) · 2.05 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
const config = {
locateFile: (filename) => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.10.3/${filename}`,
};
let database = null;
const init = async function (config) {
const SQL = await initSqlJs(config);
database = new SQL.Database();
};
init(config);
_hyperscript.addCommand("db", function (parser, runtime, tokens) {
if (!tokens.matchToken("db")) return;
const queryToken = parser.requireElement("stringLike", tokens);
let preparedValuesToken;
if (tokens.matchToken("with")) {
preparedValuesToken = parser.requireElement("expression", tokens);
}
return {
args: [queryToken, preparedValuesToken],
async op(context, query, prepared) {
let results = database.exec(query, prepared);
context.result = results;
return runtime.findNext(this);
},
};
});
_hyperscript.addCommand("table", function (parser, runtime, tokens) {
if (!tokens.matchToken("table")) return;
const expr = parser.requireElement("expression", tokens);
return {
args: [expr],
async op(context, value) {
if (value.length == 0) {
context.result = "No results found.";
} else {
context.result = "";
for (let h = 0; h < value.length; h++) {
let queryResult = value[h];
let table = document.createElement("table");
let head = queryResult.columns;
let thead = table.createTHead();
let theadRow = thead.insertRow();
for (let i = 0; i < head.length; i++) {
let th = document.createElement("th");
th.textContent = head[i];
theadRow.appendChild(th);
}
let tbody = table.createTBody();
for (let i = 0; i < queryResult.values.length; i++) {
let row = queryResult.values[i];
let rowEl = tbody.insertRow();
for (let j = 0; j < row.length; j++) {
let td = rowEl.insertCell();
td.textContent = row[j];
}
}
context.result += table.outerHTML;
}
}
return runtime.findNext(this);
},
};
});