0.0.16 • Published 10 months ago

sqlite-idb v0.0.16

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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.

0.0.16

10 months ago

0.0.15

10 months ago

0.0.14

10 months ago

0.0.13

10 months ago

0.0.12

10 months ago

0.0.11

10 months ago

0.0.10

10 months ago

0.0.9

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago

0.0.0

10 months ago