absurder-sql 0.1.23

AbsurderSQL - SQLite + IndexedDB that's absurdly better than absurd-sql
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# Export/Import Guide

**AbsurderSQL's export/import feature enables full database backup, migration, and data portability in the browser.**

Unlike absurd-sql which provides no export/import functionality, AbsurderSQL allows you to:
- Export entire databases to standard SQLite files
- Import databases from SQLite files
- Migrate data between tabs, browsers, or devices
- Create backups for disaster recovery
- Share databases as downloadable files

## Table of Contents

- [Quick Start]#quick-start
- [API Reference]#api-reference
- [Architecture]#architecture
- [Examples]#examples
- [Best Practices]#best-practices
- [Limitations]#limitations
- [Troubleshooting]#troubleshooting

---

## Quick Start

### Basic Export

```javascript
import init, { Database } from '@npiesco/absurder-sql';

await init();
const db = await Database.newDatabase('myapp.db');

// Create some data
await db.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
await db.execute("INSERT INTO users VALUES (1, 'Alice')");

// Export to Uint8Array
const exportedData = await db.exportToFile();

// Download as file
const blob = new Blob([exportedData], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'myapp.db';
a.click();
URL.revokeObjectURL(url);

await db.close();
```

### Basic Import

```javascript
import init, { Database } from '@npiesco/absurder-sql';

await init();

// Get file from user (e.g., via <input type="file">)
const fileInput = document.getElementById('dbFile');
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);

// Import and use immediately (no reopen needed)
let db = await Database.newDatabase('myapp.db');
await db.importFromFile(uint8Array);

// Database is immediately usable after import
const result = await db.execute('SELECT * FROM users');
console.log(result.rows);

await db.close();
```

---

## API Reference

### `Database.exportToFile()`

Exports the entire database to a standard SQLite file format.

**Returns**: `Promise<Uint8Array>` - The exported database as a byte array

**Behavior**:
- Exports all tables, indexes, triggers, and data
- Produces a standard SQLite 3 file compatible with other SQLite tools
- Uses export/import lock to prevent concurrent operations (30 second timeout)
- Respects `DatabaseConfig.max_export_size_bytes` limit (default: 2GB)

**Example**:
```javascript
const exportedData = await db.exportToFile();
console.log(`Exported ${exportedData.length} bytes`);
```

**Throws**:
- `DatabaseError` if database size exceeds configured limit
- `DatabaseError` if export/import lock acquisition times out
- `DatabaseError` if SQLite export operation fails

---

### `Database.importFromFile(fileData: Uint8Array)`

Imports a database from a standard SQLite file, replacing all existing data.

**Parameters**:
- `fileData` - Uint8Array containing SQLite database file

**Returns**: `Promise<void>`

**Behavior**:
- **Destructive operation**: Replaces ALL existing database data
- Temporarily closes and reopens the database connection internally
- Validates SQLite file header before import
- Uses export/import lock to prevent concurrent operations (30 second timeout)
- Clears all caches and forces fresh reload
- Database is immediately usable after import (no reopen needed)

**Example**:
```javascript
let db = await Database.newDatabase('myapp.db');
await db.importFromFile(uint8Array);

// Database is immediately usable after import
const result = await db.execute('SELECT * FROM imported_table');
```

**Throws**:
- `DatabaseError` if file is not a valid SQLite database
- `DatabaseError` if import/export lock acquisition times out
- `DatabaseError` if SQLite import operation fails

---

## Architecture

### Export Process

```
1. Acquire export/import lock (prevents concurrent export/import)
2. Read database size and validate against limit
3. Call SQLite VFS to serialize database to memory
4. Return Uint8Array with complete SQLite file
5. Release lock
```

The exported file is a **standard SQLite 3 database file** that can be:
- Opened with sqlite3 CLI
- Imported into other SQLite tools
- Imported back into AbsurderSQL
- Opened with rusqlite (native Rust)

### Import Process

```
1. Acquire export/import lock (prevents concurrent export/import)
2. Validate SQLite file header
3. Close current database connection
4. Clear all IndexedDB storage for this database
5. Write imported blocks to IndexedDB
6. Clear all caches (LRU, checksums, metadata)
7. Reopen database connection internally
8. Set up fresh metadata (version=1, commit_marker=1)
9. Release lock
```

The database is immediately usable after import completes.

### Locking Mechanism

Export and import operations are mutually exclusive using a lock stored in IndexedDB:

- **Lock Storage**: `{db_name}_export_import_lock` key in IndexedDB
- **Lock Timeout**: 30 seconds
- **Lock Poll Interval**: 100ms
- **Purpose**: Prevents data corruption from concurrent export/import operations

If another operation is in progress, the new operation will wait up to 30 seconds for the lock to be released.

---

## Examples

### Example 1: Export with Progress Tracking

See `examples/export_import.js` - `exportWithErrorHandling()`

```javascript
async function exportWithProgress() {
    const db = await Database.newDatabase('myapp.db');
    
    try {
        const exported = await db.exportToFile();
        const sizeInMB = exported.length / (1024 * 1024);
        
        console.log(`Export size: ${sizeInMB.toFixed(2)} MB`);
        
        if (sizeInMB > 100) {
            console.warn('Large export detected');
        }
        
        // Check storage quota
        const estimate = await navigator.storage.estimate();
        const availableMB = (estimate.quota - estimate.usage) / (1024 * 1024);
        console.log(`Available storage: ${availableMB.toFixed(2)} MB`);
        
        // Download file
        const blob = new Blob([exported], { type: 'application/octet-stream' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = 'backup.db';
        a.click();
        URL.revokeObjectURL(url);
        
    } catch (error) {
        if (error.message.includes('quota')) {
            console.error('Storage quota exceeded');
        } else if (error.message.includes('size')) {
            console.error('Database too large');
        } else {
            console.error('Export failed:', error.message);
        }
    } finally {
        await db.close();
    }
}
```

### Example 2: Import with Validation

See `examples/export_import.js` - `importWithProgress()`

```javascript
async function importWithValidation(file) {
    try {
        console.log(`Importing: ${file.name} (${(file.size / 1024).toFixed(2)} KB)`);
        
        const arrayBuffer = await file.arrayBuffer();
        const uint8Array = new Uint8Array(arrayBuffer);
        
        // Import
        const db = await Database.newDatabase('myapp.db');
        await db.importFromFile(uint8Array);

        // Database is immediately usable after import
        // Verify import
        const result = await db.execute("SELECT name FROM sqlite_master WHERE type='table'");
        console.log('Imported tables:', result.rows.map(r => r.values[0].value));
        
        await db.close();
        
    } catch (error) {
        console.error('Import failed:', error.message);
    }
}
```

### Example 3: Export/Import Roundtrip Test

See `examples/export_import.js` - `validateRoundtrip()`

```javascript
async function testRoundtrip() {
    // Create original database
    const db1 = await Database.newDatabase('original.db');
    await db1.execute('CREATE TABLE test (id INTEGER PRIMARY KEY, data TEXT)');
    await db1.execute("INSERT INTO test VALUES (1, 'alpha'), (2, 'beta')");
    
    // Export
    const exported = await db1.exportToFile();
    await db1.close();
    
    // Import to new database
    const db2 = await Database.newDatabase('imported.db');
    await db2.importFromFile(exported);

    // Verify (no reopen needed)
    const result = await db2.execute('SELECT COUNT(*) as count FROM test');
    const count = result.rows[0].values[0].value;
    
    console.log(count === 2 ? 'Roundtrip PASSED' : 'Roundtrip FAILED');
    
    await db2.close();
}
```

### Example 4: Backup and Restore

See `examples/export_import.js` - `backupRestoreWorkflow()`

```javascript
async function createBackup() {
    const db = await Database.newDatabase('app_data.db');
    
    // Export database
    const backup = await db.exportToFile();
    const timestamp = new Date().toISOString();
    
    // Store backup (could be localStorage, server upload, etc.)
    localStorage.setItem('database_backup', JSON.stringify({
        timestamp,
        size: backup.length,
        data: Array.from(backup)
    }));
    
    await db.close();
    console.log(`Backup created: ${timestamp}`);
}

async function restoreBackup() {
    // Retrieve backup
    const storedBackup = JSON.parse(localStorage.getItem('database_backup'));
    const backupData = new Uint8Array(storedBackup.data);
    
    // Restore database
    const db = await Database.newDatabase('app_data.db');
    await db.importFromFile(backupData);

    // Verify restoration (no reopen needed)
    const result = await db.execute('SELECT COUNT(*) as count FROM users');
    console.log(`Restored ${result.rows[0].values[0].value} records`);
    
    await db.close();
}
```

---

## Best Practices

### 1. Database is Usable Immediately After Import

Import automatically handles connection management internally. No manual reopen is needed:

```javascript
const db = await Database.newDatabase('myapp.db');
await db.importFromFile(data);

// Database is immediately usable
const result = await db.execute('SELECT * FROM imported_table');
```

### 2. Handle Export Size Limits

Configure size limits based on your use case:

```javascript
const config = {
    name: 'large_app.db',
    max_export_size_bytes: 5 * 1024 * 1024 * 1024, // 5GB
    // Or set to null for unlimited (not recommended)
};

const db = await Database.newDatabase(config);
```

Default limit: **2GB** (balances IndexedDB capacity with browser memory limits)

### 3. Validate Files Before Import

```javascript
function validateSQLiteFile(uint8Array) {
    // Check SQLite header magic number
    const header = new TextDecoder().decode(uint8Array.slice(0, 16));
    if (!header.startsWith('SQLite format 3')) {
        throw new Error('Not a valid SQLite database');
    }
    
    // Check file size
    const maxSize = 100 * 1024 * 1024; // 100MB
    if (uint8Array.length > maxSize) {
        throw new Error(`File too large: ${(uint8Array.length / (1024 * 1024)).toFixed(2)}MB`);
    }
}
```

### 4. Use Error Handling

```javascript
try {
    const exported = await db.exportToFile();
} catch (error) {
    if (error.message.includes('timeout')) {
        console.error('Another export/import in progress - please wait');
    } else if (error.message.includes('size')) {
        console.error('Database exceeds size limit');
    } else {
        console.error('Export failed:', error.message);
    }
}
```

### 5. Check Storage Quota Before Large Exports

```javascript
if (navigator.storage && navigator.storage.estimate) {
    const estimate = await navigator.storage.estimate();
    const available = estimate.quota - estimate.usage;
    const dbSize = 50 * 1024 * 1024; // Estimated size
    
    if (dbSize > available) {
        throw new Error('Not enough storage space');
    }
}
```

### 6. Clean Up Resources

```javascript
try {
    const exported = await db.exportToFile();
    const url = URL.createObjectURL(new Blob([exported]));
    // Use URL...
    URL.revokeObjectURL(url); // Always revoke!
} finally {
    await db.close(); // Always close!
}
```

---

## Limitations

### Size Limits

- **Default Maximum**: 2GB per export
  - Configurable via `DatabaseConfig.max_export_size_bytes`
  - Can be set to `null` for unlimited (not recommended)
  - Rationale: Balances IndexedDB capacity (10GB+) with browser memory limits (2-4GB/tab)

- **Browser Memory**: Large exports load entirely into memory
  - For very large databases (>100MB), monitor memory usage
  - Consider periodic backups of smaller databases

### Performance

- **Export Speed**: ~100-500 MB/s (varies by browser and hardware)
- **Import Speed**: ~50-200 MB/s (slower due to IndexedDB writes)
- **Blocking**: Export/import operations block other database operations

### Browser Compatibility

All modern browsers support export/import:

| Browser | Export | Import | Max Tested Size |
|---------|--------|--------|-----------------|
| **Chrome 131** | **[✓]** | **[✓]** | 500MB |
| **Firefox 143** | **[✓]** | **[✓]** | 500MB |
| **Safari 26** | **[✓]** | **[✓]** | 500MB |

### Multi-Tab Coordination

- Only one export or import can run at a time across all tabs
- Operations use a 30-second timeout lock
- If lock acquisition fails, operation throws an error
- Recommendation: Perform exports/imports when no other tabs are active

---

## Troubleshooting

### Error: "Database too large to export"

**Cause**: Database size exceeds `max_export_size_bytes` limit

**Solution**:
```javascript
// Option 1: Increase limit
const config = {
    name: 'myapp.db',
    max_export_size_bytes: 5 * 1024 * 1024 * 1024, // 5GB
};

// Option 2: Remove limit (not recommended)
const config = {
    name: 'myapp.db',
    max_export_size_bytes: null,
};

// Option 3: Reduce database size
await db.execute('VACUUM'); // Reclaim unused space
```

### Error: "Export/import lock acquisition timeout"

**Cause**: Another export/import operation is in progress

**Solution**:
- Wait for the other operation to complete (max 30 seconds)
- Close other tabs that might be performing operations
- Retry after a delay:
  ```javascript
  await new Promise(resolve => setTimeout(resolve, 5000));
  const exported = await db.exportToFile();
  ```

### Error: "Not a valid SQLite database"

**Cause**: Imported file is not a valid SQLite file

**Solution**:
```javascript
// Validate before import
function validateSQLiteFile(data) {
    const header = new TextDecoder().decode(data.slice(0, 16));
    if (!header.startsWith('SQLite format 3')) {
        throw new Error('Invalid SQLite file');
    }
}

validateSQLiteFile(uint8Array);
await db.importFromFile(uint8Array);
```

### Import Completes But Data is Missing

**Cause**: Import failed silently or imported wrong file

**Solution**:
```javascript
const db = await Database.newDatabase('myapp.db');

try {
    await db.importFromFile(data);
    // Verify the import succeeded
    const result = await db.execute("SELECT name FROM sqlite_master WHERE type='table'");
    console.log('Imported tables:', result.rows.map(r => r.values[0].value));
} catch (error) {
    console.error('Import failed:', error.message);
}
```

### Out of Memory During Export

**Cause**: Database too large for available browser memory

**Solution**:
- Close unnecessary tabs
- Use a browser with more available memory
- Split data into smaller databases
- Check available memory:
  ```javascript
  if (performance && performance.memory) {
      const memoryMB = performance.memory.usedJSHeapSize / (1024 * 1024);
      console.log(`Memory used: ${memoryMB.toFixed(2)} MB`);
  }
  ```

### Storage Quota Exceeded

**Cause**: Not enough IndexedDB storage for import

**Solution**:
```javascript
// Check quota before import
const estimate = await navigator.storage.estimate();
const availableMB = (estimate.quota - estimate.usage) / (1024 * 1024);
console.log(`Available: ${availableMB.toFixed(2)} MB`);

if (fileSize > estimate.quota - estimate.usage) {
    // Request persistent storage
    await navigator.storage.persist();
    
    // Or ask user to free up space
    alert('Please free up storage space');
}
```

---

## Interactive Demos

### Full-Featured Demo

`examples/export_import_demo.html` - Complete 4-step wizard:
1. Create sample database with tables, indexes, triggers
2. Export database with progress tracking
3. Import database from file upload
4. Verify imported data integrity

**Run**: Open http://localhost:8080/examples/export_import_demo.html

### Automated Tests

`examples/test_export_import_examples.html` - Validation suite:
- Test 1: Basic Export
- Test 2: Basic Import
- Test 3: Export/Import Roundtrip
- Test 4: Complex Schema Preservation
- Test 5: File Size Validation

**Run**: Open http://localhost:8080/examples/test_export_import_examples.html

### JavaScript Examples

`examples/export_import.js` - 9 production examples:
- `basicExportExample()` - Simple export workflow
- `basicImportExample()` - Simple import workflow
- `exportWithErrorHandling()` - Production error handling
- `importWithProgress()` - Progress tracking
- `validateRoundtrip()` - Data integrity verification
- `validateFileSize()` - Size validation
- `handleConcurrentOperations()` - Concurrency handling
- `multiTabSafetyExample()` - Multi-tab coordination
- `backupRestoreWorkflow()` - Complete backup/restore

---

## Comparison with absurd-sql

| Feature | absurd-sql | AbsurderSQL |
|---------|-----------|-------------|
| **Export to File** | **[X]** Not supported | **[✓]** Full support |
| **Import from File** | **[X]** Not supported | **[✓]** Full support |
| **Database Backup** | **[X]** Not possible | **[✓]** Standard SQLite files |
| **Data Migration** | **[X]** Manual only | **[✓]** Automated |
| **File Portability** | **[X]** Locked in IndexedDB | **[✓]** Standard SQLite format |
| **Multi-Device Sync** | **[X]** Not possible | **[✓]** Export/import workflow |
| **Disaster Recovery** | **[X]** No backups | **[✓]** Full backup/restore |
| **CLI/Tool Integration** | **[X]** No file access | **[✓]** Use sqlite3, DB Browser |

**AbsurderSQL provides complete data portability that absurd-sql cannot offer.**

---

## See Also

- [Multi-Tab Guide]MULTI_TAB_GUIDE.md - Multi-tab coordination
- [Dual Mode Guide]DUAL_MODE.md - Browser + Native persistence
- [Transaction Support]TRANSACTION_SUPPORT.md - Transaction handling
- [Benchmark Results]BENCHMARK.md - Performance metrics

---

## License

AGPL-3.0 - See [LICENSE.md](../LICENSE.md)