Skip to content

SQLite #212

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 6 commits into from
May 25, 2021
Merged

SQLite #212

merged 6 commits into from
May 25, 2021

Conversation

mbostock
Copy link
Member

This adds SQL.js to the recommended libraries, exposed as SQLite. In addition, it adds a fileAttachment.sqlite() method which returns a SQLiteDatabaseClient: an implementation of the Observable DatabaseClient interface backed by SQLite.

@mbostock mbostock requested a review from visnup May 22, 2021 16:11
src/sqlite.js Outdated
}

async function table(data, options) {
const Inputs = await requireDefault("@observablehq/[email protected]/dist/inputs.umd.min.js");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not wild about duplicating the pinned version here (ideally, it’d be managed in yarn.lock) and also it would be better to use the Library’s require rather than requireDefault. We have the same issue with fileAttachment.csv, so I figured this would be acceptable as a starting point, but at some point we have to figure out how to plumb the Library’s custom require through the FileAttachment function and then into the SQLiteDatabaseClient.

return sql({locateFile: file => `https://cdn.jsdelivr.net/npm/[email protected]/dist/${file}`});
}

export class SQLiteDatabaseClient {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it going to be odd that this database client doesn't extend the worker's DatabaseClient function, prototype-chain-wise, even though it implements the same interface? It only occurred to me because queryRow matches exactly the worker's implementation and could be re-used here without copying.

I was thinking alternatively, this implementation could move to the worker and it could accept a file attachment for the constructor: DatabaseClient(FileAttachment("db.sqlite")). Though, there's a lot of assumption going into that expression since nothing references "sqlite" statically.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DatabaseClient isn’t currently part of the standard library, so I’ll need to move parts of it up here to make that work. I’ll take a crack at it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about some more and I don’t think this is worth the effort. It’s not easy to move the DatabaseClient class into the standard library because it hard-codes the known implementations, all of which currently require coordination with the editor (since they are remote databases that require a database connector, not an in-memory local database like SQLite).

src/sqlite.js Outdated

async function table(data, options) {
const Inputs = await requireDefault("@observablehq/[email protected]/dist/inputs.umd.min.js");
return Inputs.table(data, options);
Copy link
Member

@visnup visnup May 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn’t yet, but I can.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to go the other way and port the current DatabaseClient implementation into the standard library (i.e., generate a basic HTML table rather than use Inputs.table). That’s at least consistent. I suppose we could still expose an abstract DatabaseClient here in the standard library to share some code with the other implementations, but I don’t think it’s urgent that we do this so I’d rather not, for now.

@mbostock mbostock requested a review from visnup May 24, 2021 21:19
@mbostock mbostock merged commit c780dab into main May 25, 2021
@mbostock mbostock deleted the mbostock/sqlite branch May 25, 2021 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants