diff --git a/docs/lib/sqlite.md b/docs/lib/sqlite.md index b5259410d..e4c414f2c 100644 --- a/docs/lib/sqlite.md +++ b/docs/lib/sqlite.md @@ -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(); ``` @@ -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"); ``` @@ -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) +``` diff --git a/src/client/stdlib/sqlite.js b/src/client/stdlib/sqlite.js index ac22e9471..75985ad45 100644 --- a/src/client/stdlib/sqlite.js +++ b/src/client/stdlib/sqlite.js @@ -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", {