Skip to content

Expose SQLiteDatabaseClient #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ export default [
node(),
terser({
output: {preamble: copyright},
mangle: {reserved: ["RequireError"]}
mangle: {
reserved: [
"FileAttachment",
"RequireError",
"SQLiteDatabaseClient",
"ZipArchive",
"ZipArchiveEntry"
]
}
})
],
output: {
Expand Down
6 changes: 2 additions & 4 deletions src/fileAttachment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {require as requireDefault} from "d3-require";
import sqlite, {SQLiteDatabaseClient} from "./sqlite.js";
import {SQLiteDatabaseClient} from "./sqlite.js";
import jszip from "./zip.js";

async function remote_fetch(file) {
Expand Down Expand Up @@ -53,9 +53,7 @@ class AbstractFile {
});
}
async sqlite() {
const [SQL, buffer] = await Promise.all([sqlite(requireDefault), this.arrayBuffer()]);
const db = new SQL.Database(new Uint8Array(buffer));
return new SQLiteDatabaseClient(db);
return SQLiteDatabaseClient.open(remote_fetch(this));
}
async zip() {
const [JSZip, buffer] = await Promise.all([jszip(requireDefault), this.arrayBuffer()]);
Expand Down
3 changes: 2 additions & 1 deletion src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import now from "./now.js";
import Promises from "./promises/index.js";
import resolve from "./resolve.js";
import requirer from "./require.js";
import SQLite from "./sqlite.js";
import SQLite, {SQLiteDatabaseClient} from "./sqlite.js";
import svg from "./svg.js";
import tex from "./tex.js";
import vegalite from "./vegalite.js";
Expand All @@ -24,6 +24,7 @@ export default Object.assign(function Library(resolver) {
Mutable: () => Mutable,
Plot: () => require("@observablehq/[email protected]/dist/plot.umd.min.js"),
SQLite: () => SQLite(require),
SQLiteDatabaseClient: () => SQLiteDatabaseClient,
_: () => require("[email protected]/lodash.min.js"),
d3: () => require("[email protected]/dist/d3.min.js"),
dot: () => require("@observablehq/[email protected]/dist/graphviz.min.js"),
Expand Down
13 changes: 13 additions & 0 deletions src/sqlite.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {require as requireDefault} from "d3-require";

export default async function sqlite(require) {
const sql = await require("[email protected]/dist/sql-wasm.js");
return sql({locateFile: file => `https://cdn.jsdelivr.net/npm/[email protected]/dist/${file}`});
Expand All @@ -9,6 +11,10 @@ export class SQLiteDatabaseClient {
_db: {value: db}
});
}
static async open(source) {
const [SQL, buffer] = await Promise.all([sqlite(requireDefault), Promise.resolve(source).then(load)]);
return new SQLiteDatabaseClient(new SQL.Database(buffer));
}
async query(query, params) {
return await exec(this._db, query, params);
}
Expand All @@ -34,6 +40,13 @@ export class SQLiteDatabaseClient {
}
}

function load(source) {
return typeof source === "string" ? fetch(source).then(load)
: source instanceof Response || source instanceof Blob ? source.arrayBuffer().then(load)
: source instanceof ArrayBuffer ? new Uint8Array(source)
: source;
}

async function exec(db, query, params) {
const [result] = await db.exec(query, params);
if (!result) return [];
Expand Down
1 change: 1 addition & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test("new Library returns a library with the expected keys", async t => {
"Plot",
"Promises",
"SQLite",
"SQLiteDatabaseClient",
"_",
"d3",
"dot",
Expand Down