web-sqlite-js 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -100
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,11 +36,10 @@ Designed to be truly effortless, it allows you to get a high-performance relatio
|
|
|
36
36
|
- [Quick start](#quick-start)
|
|
37
37
|
- [Setup HTTP headers](#setup-http-headers)
|
|
38
38
|
- [Usage](#usage)
|
|
39
|
-
- [Debug mode](#debug-mode)
|
|
40
39
|
- [Transactions](#transactions)
|
|
41
|
-
- [
|
|
42
|
-
- [
|
|
43
|
-
- [
|
|
40
|
+
- [Schema Migrations](#schema-migrations)
|
|
41
|
+
- [Debug mode](#debug-mode)
|
|
42
|
+
- [Structured Logging](#structured-logging)
|
|
44
43
|
|
|
45
44
|
## Features
|
|
46
45
|
|
|
@@ -49,10 +48,8 @@ Designed to be truly effortless, it allows you to get a high-performance relatio
|
|
|
49
48
|
- **Concurrency Safe**: Built-in mutex ensures safe, sequential execution of commands.
|
|
50
49
|
- **Type-Safe**: Written in TypeScript with full type definitions.
|
|
51
50
|
- **Transactions**: Supports atomic transactions with automatic rollback on error.
|
|
52
|
-
- **
|
|
53
|
-
- **
|
|
54
|
-
- **Database Events** (v2.0.0): Listen to database open/close events for UI synchronization.
|
|
55
|
-
- **Database Registry** (v2.0.0): Prevents duplicate database opens with automatic tracking.
|
|
51
|
+
- **Schema Migrations**: Built-in versioning system for database schema changes.
|
|
52
|
+
- **Structured Logging**: Subscribe to SQL execution logs via `onLog()`.
|
|
56
53
|
|
|
57
54
|
## Quick start
|
|
58
55
|
|
|
@@ -286,7 +283,60 @@ await db.transaction(async (tx) => {
|
|
|
286
283
|
});
|
|
287
284
|
```
|
|
288
285
|
|
|
289
|
-
##
|
|
286
|
+
## Schema Migrations
|
|
287
|
+
|
|
288
|
+
Manage database schema changes across releases using the built-in versioning system. Define releases with migration SQL and optional seed data.
|
|
289
|
+
|
|
290
|
+
### Basic Usage
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
const db = await openDB("myapp", {
|
|
294
|
+
releases: [
|
|
295
|
+
{
|
|
296
|
+
version: "1.0.0",
|
|
297
|
+
migrationSQL: `
|
|
298
|
+
CREATE TABLE users (
|
|
299
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
300
|
+
name TEXT NOT NULL,
|
|
301
|
+
email TEXT UNIQUE
|
|
302
|
+
);
|
|
303
|
+
`,
|
|
304
|
+
seedSQL: `
|
|
305
|
+
INSERT INTO users (name, email) VALUES
|
|
306
|
+
('Alice', 'alice@example.com'),
|
|
307
|
+
('Bob', 'bob@example.com');
|
|
308
|
+
`,
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
version: "1.1.0",
|
|
312
|
+
migrationSQL: `
|
|
313
|
+
ALTER TABLE users ADD COLUMN created_at TEXT DEFAULT (datetime('now'));
|
|
314
|
+
`,
|
|
315
|
+
},
|
|
316
|
+
],
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
// Database is now at version 1.1.0 with all migrations applied
|
|
320
|
+
const users = await db.query("SELECT * FROM users");
|
|
321
|
+
console.log(users);
|
|
322
|
+
// Output: [{ id: 1, name: 'Alice', email: 'alice@example.com', created_at: '...' }, ...]
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### How It Works
|
|
326
|
+
|
|
327
|
+
1. **Version Tracking**: Each release has a semantic version (e.g., "1.0.0")
|
|
328
|
+
2. **Automatic Migration**: When opening a database, new releases are applied in order
|
|
329
|
+
3. **Hash Verification**: Migration SQL is hashed to prevent tampering
|
|
330
|
+
4. **OPFS Storage**: Each version is stored as a separate file (`1.0.0.sqlite3`, `1.1.0.sqlite3`)
|
|
331
|
+
|
|
332
|
+
### Best Practices
|
|
333
|
+
|
|
334
|
+
- **Use Semantic Versioning**: Follow `MAJOR.MINOR.PATCH` format
|
|
335
|
+
- **Idempotent Migrations**: Each migration should handle re-runs safely
|
|
336
|
+
- **Test Migrations**: Always test migrations on a clean database
|
|
337
|
+
- **Incremental Changes**: Keep migrations focused on single schema changes
|
|
338
|
+
|
|
339
|
+
## Structured Logging
|
|
290
340
|
|
|
291
341
|
Subscribe to structured log events for monitoring, debugging, and analytics. The `onLog()` API allows you to capture SQL execution details, errors, and application events.
|
|
292
342
|
|
|
@@ -330,97 +380,6 @@ const cancel2 = db.onLog((log) => {
|
|
|
330
380
|
});
|
|
331
381
|
```
|
|
332
382
|
|
|
333
|
-
---
|
|
334
|
-
|
|
335
|
-
## Global Database Access (v2.0.0)
|
|
336
|
-
|
|
337
|
-
Access opened databases from anywhere in your application without imports. The `window.__web_sqlite` global namespace provides direct references to all opened database instances.
|
|
338
|
-
|
|
339
|
-
```typescript
|
|
340
|
-
// Open database in module A
|
|
341
|
-
const db = await openDB("app");
|
|
342
|
-
|
|
343
|
-
// In module B (no import needed):
|
|
344
|
-
const db = window.__web_sqlite.databases["app.sqlite3"];
|
|
345
|
-
const users = await db.query("SELECT * FROM users");
|
|
346
|
-
|
|
347
|
-
// List all opened databases
|
|
348
|
-
console.log(Object.keys(window.__web_sqlite.databases));
|
|
349
|
-
// Output: ["app.sqlite3", "users.sqlite3"]
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
**Use Cases**:
|
|
353
|
-
|
|
354
|
-
- **DevTools Integration**: Access databases from browser console for debugging
|
|
355
|
-
- **Cross-Module Communication**: Share database state without prop drilling
|
|
356
|
-
- **Debugging**: Inspect and query databases directly from DevTools console
|
|
357
|
-
|
|
358
|
-
**Browser Console Example**:
|
|
359
|
-
|
|
360
|
-
```javascript
|
|
361
|
-
// From browser DevTools console:
|
|
362
|
-
window.__web_sqlite.databases["app.sqlite3"]
|
|
363
|
-
.query("SELECT * FROM users")
|
|
364
|
-
.then((users) => console.table(users));
|
|
365
|
-
```
|
|
366
|
-
|
|
367
|
-
---
|
|
368
|
-
|
|
369
|
-
## Database Events (v2.0.0)
|
|
370
|
-
|
|
371
|
-
Subscribe to database open/close events for UI synchronization and monitoring. The `onDatabaseChange()` API notifies you when databases are opened or closed.
|
|
372
|
-
|
|
373
|
-
```typescript
|
|
374
|
-
// Subscribe to database changes
|
|
375
|
-
const unsubscribe = window.__web_sqlite.onDatabaseChange((event) => {
|
|
376
|
-
if (event.action === "opened") {
|
|
377
|
-
console.log(`Database opened: ${event.dbName}`);
|
|
378
|
-
updateDatabaseList(event.databases);
|
|
379
|
-
} else if (event.action === "closed") {
|
|
380
|
-
console.log(`Database closed: ${event.dbName}`);
|
|
381
|
-
updateDatabaseList(event.databases);
|
|
382
|
-
}
|
|
383
|
-
console.log("Current databases:", event.databases);
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
// Open a database
|
|
387
|
-
await openDB("app");
|
|
388
|
-
// Output: Database opened: app.sqlite3
|
|
389
|
-
// Output: Current databases: ["app.sqlite3"]
|
|
390
|
-
|
|
391
|
-
// Open another database
|
|
392
|
-
await openDB("users");
|
|
393
|
-
// Output: Database opened: users.sqlite3
|
|
394
|
-
// Output: Current databases: ["app.sqlite3", "users.sqlite3"]
|
|
395
|
-
|
|
396
|
-
// Close first database
|
|
397
|
-
await window.__web_sqlite.databases["app.sqlite3"].close();
|
|
398
|
-
// Output: Database closed: app.sqlite3
|
|
399
|
-
// Output: Current databases: ["users.sqlite3"]
|
|
400
|
-
|
|
401
|
-
// Unsubscribe when done
|
|
402
|
-
// unsubscribe();
|
|
403
|
-
```
|
|
404
|
-
|
|
405
|
-
**Event Structure**:
|
|
406
|
-
|
|
407
|
-
```typescript
|
|
408
|
-
interface DatabaseChangeEvent {
|
|
409
|
-
action: "opened" | "closed"; // What happened
|
|
410
|
-
dbName: string; // Which database (normalized name)
|
|
411
|
-
databases: string[]; // All currently opened database names
|
|
412
|
-
}
|
|
413
|
-
```
|
|
414
|
-
|
|
415
|
-
**Use Cases**:
|
|
416
|
-
|
|
417
|
-
- **DevTools Panels**: Show active databases in browser DevTools
|
|
418
|
-
- **UI Updates**: Refresh database list when databases open/close
|
|
419
|
-
- **Monitoring**: Track database lifecycle for debugging
|
|
420
|
-
- **Multi-Window Sync**: Coordinate database access across browser windows
|
|
421
|
-
|
|
422
|
-
---
|
|
423
|
-
|
|
424
383
|
## Star History
|
|
425
384
|
|
|
426
385
|
<p align="left">
|