@faustt/sqlite
Sqlite for browsers. TypeScript support. Supports vite.
Description
This package provides sqlite 3 for browsers. It is based on the (semi?) official NPM package, but makes it more compatible with Vite and easier to use.
Installation
Add as devDependency
$ npm install -D @faustt/sqlite # using NPM
$ yarn add -D @faustt/sqlite # using Yarn
$ pnpm install -D @faustt/sqlite # using PNPM
Modify vite.config.ts
This adds required headers (Cross-Origin-Opener-Policy
and Cross-Origin-Embedder-Policy
) to the vite dev server or preview server. Note that you are required to set those headers yourself in your host setup.
import { defineConfig } from "vite";
import { extendConfig } from "@faustt/sqlite/vite";
+ import { extendConfig } from "@faustt/sqlite/vite";
// https://vitejs.dev/config/
- export default defineConfig({
+ export default extendConfig(
+ defineConfig({
plugins: [svelte()],
- });
+ })
+ );
Open a database
import { Database } from "@faustt/sqlite";
const database = await Database.open(); // in-memory
const database = await Database.open("data.db"); // stored in OPFS (origin private file system)
await database.exec(`--sql
CREATE TABLE user (
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
id TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL
)
`);
const date = new Date();
await database.insert("user", {
created_at: date,
updated_at: date,
id: "6c2200a4-0c1c-45fc-8c8f-19732c1ff76a",
name: "Test 1",
});
const users = await database.fetch("SELECT * FROM user");