chishiki-sqlite
v0.8.0
Published
chrome extension - sqlite opfs integration
Readme
SQLite Package
This package provides SQLite with OPFS (Origin Private File System) support for the Chrome extension.
Features
- SQLite database with OPFS persistence
- Worker-based implementation for OPFS VFS
- React hooks for easy integration
- Client API for database operations
Usage
In a React Component
import { useSqliteOpfs } from '@extension/sqlite';
const MyComponent = () => {
const {
isReady,
db,
exec,
openDatabase,
closeDatabase,
} = useSqliteOpfs();
const handleQuery = async () => {
const result = await exec('SELECT * FROM table;');
console.log(result);
};
return (
<div>
{isReady && <button onClick={handleQuery}>Run Query</button>}
</div>
);
};Direct Client Usage
import { SqliteWorkerClient } from '@extension/sqlite';
const client = new SqliteWorkerClient();
await client.init();
await client.open('mydb.sqlite3');
const result = await client.exec('SELECT * FROM table;');
await client.close();Requirements
- OPFS support (Chrome 109+, Edge 109+)
- Background service worker (for worker context)
- Storage permissions in manifest
API
useSqliteOpfs()
React hook for SQLite operations.
Returns:
isReady: boolean - WASM initializeddb: boolean - Database is opendbFilename: string - Current database filenameopfsFiles: Array - List of OPFS filesopfsSupported: boolean - OPFS availablepersistent: boolean - Using persistent storagestorage: 'opfs' | 'jsstorage' | 'memory' - Storage typeopenDatabase(): Function - Open databasecloseDatabase(): Function - Close databaseexec(): Function - Execute SQLrunSampleSchema(): Function - Seed sample datalistOpfsFiles(): Function - List OPFS filesdeleteOpfsFile(): Function - Delete file
SqliteWorkerClient
Direct client for SQLite operations.
Methods:
init(): Initialize SQLiteopen(filename): Open databaseclose(): Close databaseexec(sql): Execute SQLseed(): Seed sample schemaflush(): Flush to disklistOpfs(): List OPFS filesdeleteOpfs(filename): Delete filedispose(): Cleanup
