Skip to content

Latest commit

 

History

History
291 lines (228 loc) · 8.22 KB

File metadata and controls

291 lines (228 loc) · 8.22 KB

sqlite-wasm-easy - Project Summary

Overview

sqlite-wasm-easy is a zero-configuration wrapper around @sqlite.org/sqlite-wasm that simplifies SQLite database usage in the browser.

✅ What We Built

Core Features

  1. Zero Configuration - Works out of the box with sensible defaults
  2. Fully Configurable - VFS type, PRAGMA settings, logging options
  3. TypeScript First - Complete type safety and IntelliSense
  4. Web Worker Based - Non-blocking database operations
  5. Simple API - Only high-level methods (exec, query, run, transaction)
  6. Type Hints - .table() method for typed access without CRUD methods

Package Structure

sqlite-wasm-easy/
├── src/
│   ├── index.ts                    # Main entry point
│   ├── core/
│   │   ├── database.ts             # Main SQLiteWASM class
│   │   └── defaults.ts             # Default configuration
│   ├── worker/
│   │   └── sqliteWorker.ts         # Web Worker for SQLite operations
│   ├── types/
│   │   └── index.ts                # All TypeScript type definitions
│   └── utils/                      # (Reserved for future utilities)
├── dist/                           # Built output
│   ├── index.js                    # Main bundle (14.5 KB)
│   ├── index.d.ts                  # TypeScript declarations
│   └── worker/
│       └── sqliteWorker.js         # Worker bundle (5 KB)
├── examples/
│   ├── basic.html                  # Basic CRUD example
│   └── typed-schema.html           # TypeScript schema example
├── package.json
├── tsconfig.json
├── vite.config.ts
├── README.md
└── LICENSE

Total bundle size: ~20 KB (gzipped: ~7 KB)

API Design

Configuration Options

All aspects are configurable with sensible defaults:

interface SQLiteWASMConfig {
	filename: string;
	vfs?: {
		type?: 'opfs-sahpool' | 'opfs' | 'memory';
		poolConfig?: {
			initialCapacity?: number; // Default: 3
			clearOnInit?: boolean; // Default: false
			name?: string; // Default: 'sqlite-wasm-pool'
		};
	};
	pragma?: {
		journal_mode?: JournalMode; // Default: 'WAL'
		synchronous?: SynchronousMode; // Default: 'NORMAL'
		temp_store?: TempStoreMode; // Default: 'MEMORY'
		cache_size?: number;
		page_size?: number;
		foreign_keys?: 'ON' | 'OFF';
		[key: string]: any; // Any custom PRAGMA
	};
	worker?: {
		path?: string; // Custom worker path
	};
	logging?: {
		filterSqlTrace?: boolean; // Default: true
		print?: (msg: string) => void;
		printErr?: (msg: string) => void;
	};
}

Core Methods

Only high-level methods (no CRUD):

class SQLiteWASM<Schema = any> {
	ready(): Promise<void>;
	exec(sql: string, params?: any[]): Promise<void>;
	query<T>(sql: string, params?: any[]): Promise<T[]>;
	run(sql: string, params?: any[]): Promise<RunResult>;
	transaction(callback: (tx: Transaction) => Promise<void>): Promise<void>;
	table<K extends keyof Schema>(name: K): TypedTable<Schema[K]>;
	export(): Promise<Uint8Array>;
	import(filename: string, data: Uint8Array): Promise<void>;
	close(): Promise<void>;
	delete(): Promise<void>;
}

TypedTable (Type Hints Only)

interface TypedTable<T> {
	query<R = T>(sql: string, params?: any[]): Promise<R[]>;
	exec(sql: string, params?: any[]): Promise<void>;
	run(sql: string, params?: any[]): Promise<RunResult>;
}

Usage Examples

1. Basic Usage

import { SQLiteWASM } from 'sqlite-wasm-easy';

const db = new SQLiteWASM({ filename: 'myapp.db' });
await db.ready();

await db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
await db.exec('INSERT INTO users (name) VALUES (?)', ['Alice']);

const users = await db.query('SELECT * FROM users');

2. Custom Configuration

const db = new SQLiteWASM({
	filename: 'myapp.db',
	vfs: {
		type: 'opfs-sahpool',
		poolConfig: {
			initialCapacity: 5,
			clearOnInit: false,
			name: 'custom-pool',
		},
	},
	pragma: {
		journal_mode: 'WAL',
		synchronous: 'FULL',
		foreign_keys: 'ON',
	},
	logging: {
		filterSqlTrace: true,
	},
});

3. TypeScript Schema

interface Schema {
	users: { id: number; name: string; email: string };
	posts: { id: number; userId: number; title: string };
}

const db = new SQLiteWASM<Schema>({ filename: 'app.db' });
const usersTable = db.table('users');

// Type-safe queries
const users = await usersTable.query('SELECT * FROM users');
//    ^? { id: number; name: string; email: string }[]

4. Transactions

await db.transaction(async (tx) => {
	await tx.exec('INSERT INTO users (name) VALUES (?)', ['Charlie']);
	await tx.exec('UPDATE users SET active = 1 WHERE id = ?', [1]);
});

Key Differences from Your Current Implementation

Aspect Current (Supersorted) sqlite-wasm-easy
Scope App-specific General-purpose package
Configuration Hardcoded values Fully configurable
Table API Full ORM-like (insert, update, delete, etc.) Type hints only (query, exec, run)
PRAGMA Hardcoded (MEMORY, NORMAL) User configurable
VFS Hardcoded opfs-sahpool User can choose (opfs-sahpool, opfs, memory)
Pool Config Fixed (initialCapacity: 3) Configurable
Console Filtering Always on Optional (configurable)

What's Configurable (vs Hardcoded Before)

VFS Method - User chooses: opfs-sahpool, opfs, or memory ✅ Pool Settings - initialCapacity, clearOnInit, name
PRAGMA Settings - journal_mode, synchronous, temp_store, cache_size, etc.
Logging - filterSqlTrace, custom print/printErr functions
Worker Path - Custom worker location

Build & Distribution

Build Output

  • Main bundle: dist/index.js (14.5 KB, gzipped: 5.5 KB)
  • Worker bundle: dist/worker/sqliteWorker.js (5 KB, gzipped: 1.6 KB)
  • TypeScript declarations: dist/index.d.ts

NPM Package Structure

{
	"name": "sqlite-wasm-easy",
	"main": "./dist/index.js",
	"types": "./dist/index.d.ts",
	"exports": {
		".": "./dist/index.js",
		"./worker": "./dist/worker/sqliteWorker.js"
	}
}

Next Steps

For Development

cd sqlite-wasm-easy
npm install
npm run build        # Build the package
npm run typecheck    # Type checking only

For Testing

  1. Open examples/basic.html in a browser (via dev server)
  2. Open examples/typed-schema.html for TypeScript examples

For Publishing

# Test locally first
npm pack

# Publish to NPM (when ready)
npm publish

Future Enhancements (Optional)

  1. Multi-tab Coordination - Add BroadcastChannel support (from your tabCoordinator)
  2. Migration Helpers - Schema versioning utilities
  3. Batch Operations - Optimize multiple inserts
  4. Query Builder - Optional fluent query API (as separate package)
  5. IndexedDB Fallback - For browsers without OPFS support
  6. React Hooks - useQuery, useMutation helpers
  7. Performance Monitoring - Built-in query timing and logging

Browser Compatibility

✅ Chrome/Edge 102+
✅ Firefox (with OPFS support)
⚠️ Safari (experimental OPFS support)

Dependencies

Peer Dependencies:

  • @sqlite.org/sqlite-wasm: ^3.49.0

Dev Dependencies:

  • typescript: ^5.7.3
  • vite: ^6.1.0

License

MIT License

Notes

  • The package is framework-agnostic (works with React, Vue, Svelte, vanilla JS)
  • No bundler configuration needed (works with Vite, Webpack, Rollup)
  • Fully tree-shakeable ES modules
  • Zero runtime dependencies (except peer dependency on @sqlite.org/sqlite-wasm)

Created: January 22, 2026
Status: ✅ Ready for testing and publishing
Build: ✅ Successful (TypeScript compilation and Vite bundling)