0.0.16 • Published 12 months ago

sqlite-idb v0.0.16

Weekly downloads
-
License
MIT
Repository
github
Last release
12 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

12 months ago

0.0.15

12 months ago

0.0.14

12 months ago

0.0.13

12 months ago

0.0.12

12 months ago

0.0.11

12 months ago

0.0.10

12 months ago

0.0.9

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago