sqlite-idb
v0.0.16
Published
sqlite with wasm binding using indexedDB as a data store
Downloads
5
Readme
Usage
npm i sqlite-idb
import { Sqlite, version } from "sqlite-idb";
// When using Vite
// import wasmUrl from 'sqlite-idb/lib/sqlite/wasm/wa-sqlite-async.wasm?url';
const main = async () => {
const { sql, createFunction } = await Sqlite({
idbName: "myIndexedDB",
dbName: "myDb",
// uncompressed: true, // Out out of IndexedDB compression
locateFile: (file) => {
return `https://esm.sh/sqlite-idb@${version}/lib/sqlite/wasm/${file}`;
},
// When using Vite
// locateFile: () => wasmUrl,
});
// query1 has type Record<string, ColumnType>[]
const query1 = await sql.rows`SELECT name, id FROM users`;
// query2 has type ColumnType>[][]
const query2 = await sql({ raw: true }).rows`SELECT id FROM users`;
for await (const statement of sql.multiple`SELECT 1; SELECT * from users;`) {
for (const row of statement) {
console.log(row);
}
}
createFunction("add_nums", (a, b) => a + b);
const added = await sql.first`SELECT add_nums(1, 2) as sum;`;
};
You can also import existing sqlite databases:
import { Sqlite } from "sqlite-idb";
const main = async () => {
const { sql, importDatabase } = await Sqlite(/* ... */);
const response = await fetch("/db.sqlite");
if (response.ok && response.body) {
await importDatabase(response.body);
}
console.log(await sql.rows`SELECT * FROM users`);
};
💙 This package was templated with
create-typescript-app
.