Public
Edited
Sep 7, 2023
1 fork
16 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// a sample query result set
r1 = [
{
TrackId: 1,
Name: "For Those About To Rock (We Salute You)",
Composer: "Angus Young, Malcolm Young, Brian Johnson",
UnitPrice: 0.99
},
{
TrackId: 2,
Name: "Balls to the Wall",
Composer: null,
UnitPrice: 0.99
},
{
TrackId: 3,
Name: "Fast As a Shark",
Composer: "F. Baltes, S. Kaufman, U. Dirkscneider & W. Hoffman",
UnitPrice: 0.99
}
]
Insert cell
// a sample query result
r1[0]
Insert cell
Insert cell
Insert cell
Insert cell
// a sample query result set with a columns property
r2 = Object.assign(
[
{
TrackId: 1,
Name: "For Those About To Rock (We Salute You)",
Composer: "Angus Young, Malcolm Young, Brian Johnson",
UnitPrice: 0.99
},
{
TrackId: 2,
Name: "Balls to the Wall",
Composer: null,
UnitPrice: 0.99
},
{
TrackId: 3,
Name: "Fast As a Shark",
Composer: "F. Baltes, S. Kaufman, U. Dirkscneider & W. Hoffman",
UnitPrice: 0.99
}
],
{
columns: [
"TrackId",
"Name",
"Composer",
"UnitPrice"
]
}
)
Insert cell
// a sample columns array
r2.columns
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// a sample table set schema
d1 = [
{name: "albums"},
{name: "sqlite_sequence"},
{name: "artists"},
{name: "customers"},
{name: "employees"},
{name: "genres"},
{name: "invoices"},
{name: "invoice_items"},
{name: "media_types"},
{name: "playlists"},
{name: "playlist_track"},
{name: "tracks"},
{name: "sqlite_stat1"}
]
Insert cell
// a sample table set schema with an explicit schema property
d2 = [
{schema: "db1", name: "albums"},
{schema: "db1", name: "sqlite_sequence"},
{schema: "db1", name: "artists"},
{schema: "db1", name: "customers"},
{schema: "db1", name: "employees"},
{schema: "db1", name: "genres"},
{schema: "db1", name: "invoices"},
{schema: "db1", name: "invoice_items"},
{schema: "db1", name: "media_types"},
{schema: "db1", name: "playlists"},
{schema: "db1", name: "playlist_track"},
{schema: "db1", name: "tracks"},
{schema: "db1", name: "sqlite_stat1"}
]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// a sample column set schema
t1 = [
{name: "TrackId", type: "integer", nullable: false, databaseType: "INTEGER"},
{name: "Name", type: "string", nullable: false, databaseType: "NVARCHAR(200)"},
{name: "AlbumId", type: "integer", nullable: true, databaseType: "INTEGER"},
{name: "MediaTypeId", type: "integer", nullable: false, databaseType: "INTEGER"},
{name: "GenreId", type: "integer", nullable: true, databaseType: "INTEGER"},
{name: "Composer", type: "string", nullable: true, databaseType: "NVARCHAR(220)"},
{name: "Milliseconds", type: "integer", inullable: false, databaseType: "INTEGER"},
{name: "Bytes", type: "integer", nullable: true, databaseType: "INTEGER"},
{name: "UnitPrice", type: "number", nullable: false, databaseType: "NUMERIC(10,2)"}
]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// a sample query with one parameter
db.query(`SELECT * FROM tracks WHERE AlbumId = ?`, [AlbumId])
Insert cell
AlbumId = 1
Insert cell
Insert cell
// a sample query with no parameters
db.query(`SELECT * FROM tracks WHERE AlbumId = 1`)
Insert cell
Insert cell
// a sample query that is aborted on invalidation
{
const abortController = new AbortController();
invalidation.then(() => abortController.abort());
return db.query(`SELECT * FROM tracks WHERE AlbumId = 1`, {
signal: abortController.signal
});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function sqliteQueryTag(strings, ...params) {
return [strings.join("?"), params];
}
Insert cell
Insert cell
q1 = sqliteQueryTag`SELECT * FROM tracks WHERE AlbumId = ${AlbumId}`
Insert cell
db.query(...q1)
Insert cell
Insert cell
Insert cell
db.sql`SELECT * FROM tracks WHERE AlbumId = ${AlbumId}`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {db} from "@observablehq/sqlite"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more