Skip to content
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

Expose SQLite.export(). #1751

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 19 additions & 2 deletions docs/lib/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {SQLiteDatabaseClient} from "npm:@observablehq/sqlite";

The easiest way to construct a SQLite database client is to declare a [`FileAttachment`](../files) and then call `file.sqlite` to load a SQLite file. This returns a promise. (Here we rely on [implicit await](../reactivity#promises).)

```js echo
```js run=false
const db = FileAttachment("chinook.db").sqlite();
```

Expand All @@ -28,7 +28,7 @@ const db = SQLiteDatabaseClient.open(FileAttachment("chinook.db"));

Using `FileAttachment` means that referenced files are automatically copied to `dist` during build, and you can even generate SQLite files using [data loaders](../data-loaders). But if you want to “hot” load a live file from an external server, pass a string to `SQLiteDatabaseClient.open`:

```js run=false
```js echo
const db = SQLiteDatabaseClient.open("https://static.observableusercontent.com/files/b3711cfd9bdf50cbe4e74751164d28e907ce366cd4bf56a39a980a48fdc5f998c42a019716a8033e2b54defdd97e4a55ebe4f6464b4f0678ea0311532605a115");
```

Expand Down Expand Up @@ -71,3 +71,20 @@ There’s also `db.queryRow` for just getting a single row.
```js echo
db.queryRow(`SELECT sqlite_version()`)
```

The entire database can be exported as a binary array with `db.export`:

```js echo
const dbAsBinary = db.export();
```

which can be loaded back into SQLite as noted previously:

```js echo
const db2 = await SQLiteDatabaseClient.open(dbAsBinary);
const companies = db2.query('SELECT Company FROM customers WHERE Company IS NOT NULL ORDER BY Company');
```

```js
Inputs.table(companies)
```
3 changes: 3 additions & 0 deletions src/client/stdlib/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export class SQLiteDatabaseClient {
queryTag(strings, ...params) {
return [strings.join("?"), params];
}
export() {
return this._db.export(this._db);
}
}

Object.defineProperty(SQLiteDatabaseClient.prototype, "dialect", {
Expand Down