@framers/sql-storage-adapter 0.1.0 → 0.3.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.
Files changed (127) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +241 -363
  3. package/dist/adapters/__tests__/indexedDbAdapter.spec.d.ts +2 -0
  4. package/dist/adapters/__tests__/indexedDbAdapter.spec.d.ts.map +1 -0
  5. package/dist/adapters/__tests__/indexedDbAdapter.spec.js +137 -0
  6. package/dist/adapters/__tests__/indexedDbAdapter.spec.js.map +1 -0
  7. package/dist/adapters/baseStorageAdapter.d.ts +1 -1
  8. package/dist/adapters/baseStorageAdapter.d.ts.map +1 -1
  9. package/dist/adapters/betterSqliteAdapter.d.ts +27 -1
  10. package/dist/adapters/betterSqliteAdapter.d.ts.map +1 -1
  11. package/dist/adapters/betterSqliteAdapter.js +81 -16
  12. package/dist/adapters/betterSqliteAdapter.js.map +1 -1
  13. package/dist/adapters/capacitorSqliteAdapter.d.ts +1 -1
  14. package/dist/adapters/capacitorSqliteAdapter.d.ts.map +1 -1
  15. package/dist/adapters/capacitorSqliteAdapter.js +27 -4
  16. package/dist/adapters/capacitorSqliteAdapter.js.map +1 -1
  17. package/dist/adapters/indexedDbAdapter.d.ts +189 -0
  18. package/dist/adapters/indexedDbAdapter.d.ts.map +1 -0
  19. package/dist/adapters/indexedDbAdapter.js +367 -0
  20. package/dist/adapters/indexedDbAdapter.js.map +1 -0
  21. package/dist/adapters/postgresAdapter.d.ts +7 -1
  22. package/dist/adapters/postgresAdapter.d.ts.map +1 -1
  23. package/dist/adapters/postgresAdapter.js +19 -2
  24. package/dist/adapters/postgresAdapter.js.map +1 -1
  25. package/dist/adapters/sqlJsAdapter.d.ts +1 -1
  26. package/dist/adapters/sqlJsAdapter.d.ts.map +1 -1
  27. package/dist/adapters/sqlJsAdapter.js +16 -2
  28. package/dist/adapters/sqlJsAdapter.js.map +1 -1
  29. package/dist/adapters/supabase.d.ts +3 -3
  30. package/dist/adapters/supabase.d.ts.map +1 -1
  31. package/dist/adapters/supabase.js +11 -7
  32. package/dist/adapters/supabase.js.map +1 -1
  33. package/dist/agentos/AgentOSStorageAdapter.d.ts +320 -0
  34. package/dist/agentos/AgentOSStorageAdapter.d.ts.map +1 -0
  35. package/dist/agentos/AgentOSStorageAdapter.js +406 -0
  36. package/dist/agentos/AgentOSStorageAdapter.js.map +1 -0
  37. package/dist/agentos/index.d.ts +7 -0
  38. package/dist/agentos/index.d.ts.map +1 -0
  39. package/dist/agentos/index.js +7 -0
  40. package/dist/agentos/index.js.map +1 -0
  41. package/dist/{types → core/contracts}/context.d.ts +2 -2
  42. package/dist/core/contracts/context.d.ts.map +1 -0
  43. package/dist/core/contracts/context.js.map +1 -0
  44. package/dist/{types → core/contracts}/events.d.ts +1 -1
  45. package/dist/core/contracts/events.d.ts.map +1 -0
  46. package/dist/core/contracts/events.js.map +1 -0
  47. package/dist/{types → core/contracts}/extensions.d.ts +3 -3
  48. package/dist/core/contracts/extensions.d.ts.map +1 -0
  49. package/dist/core/contracts/extensions.js.map +1 -0
  50. package/dist/{types.d.ts → core/contracts/index.d.ts} +1 -1
  51. package/dist/core/contracts/index.d.ts.map +1 -0
  52. package/dist/{types.js → core/contracts/index.js} +1 -1
  53. package/dist/core/contracts/index.js.map +1 -0
  54. package/dist/core/contracts/limitations.d.ts.map +1 -0
  55. package/dist/core/contracts/limitations.js.map +1 -0
  56. package/dist/{database.d.ts → core/database.d.ts} +16 -3
  57. package/dist/core/database.d.ts.map +1 -0
  58. package/dist/{database.js → core/database.js} +39 -8
  59. package/dist/core/database.js.map +1 -0
  60. package/dist/{resolver.d.ts → core/resolver.d.ts} +4 -4
  61. package/dist/core/resolver.d.ts.map +1 -0
  62. package/dist/{resolver.js → core/resolver.js} +24 -8
  63. package/dist/core/resolver.js.map +1 -0
  64. package/dist/{utils → features/backup}/cloudBackup.d.ts +1 -1
  65. package/dist/features/backup/cloudBackup.d.ts.map +1 -0
  66. package/dist/{utils → features/backup}/cloudBackup.js +2 -2
  67. package/dist/features/backup/cloudBackup.js.map +1 -0
  68. package/dist/{utils → features/migrations}/dataExport.d.ts +1 -1
  69. package/dist/features/migrations/dataExport.d.ts.map +1 -0
  70. package/dist/features/migrations/dataExport.js.map +1 -0
  71. package/dist/{utils → features/migrations}/dataImport.d.ts +1 -1
  72. package/dist/features/migrations/dataImport.d.ts.map +1 -0
  73. package/dist/features/migrations/dataImport.js.map +1 -0
  74. package/dist/{utils → features/migrations}/migration.d.ts +2 -2
  75. package/dist/features/migrations/migration.d.ts.map +1 -0
  76. package/dist/{utils → features/migrations}/migration.js +6 -6
  77. package/dist/features/migrations/migration.js.map +1 -0
  78. package/dist/{utils → features/sync}/syncManager.d.ts +11 -5
  79. package/dist/features/sync/syncManager.d.ts.map +1 -0
  80. package/dist/{utils → features/sync}/syncManager.js +40 -10
  81. package/dist/features/sync/syncManager.js.map +1 -0
  82. package/dist/index.d.ts +12 -11
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +18 -19
  85. package/dist/index.js.map +1 -1
  86. package/dist/{utils → shared}/parameterUtils.d.ts +1 -1
  87. package/dist/shared/parameterUtils.d.ts.map +1 -0
  88. package/dist/shared/parameterUtils.js.map +1 -0
  89. package/dist/types/index.d.ts +11 -0
  90. package/dist/types/index.d.ts.map +1 -0
  91. package/dist/types/index.js +11 -0
  92. package/dist/types/index.js.map +1 -0
  93. package/package.json +22 -6
  94. package/dist/database.d.ts.map +0 -1
  95. package/dist/database.js.map +0 -1
  96. package/dist/resolver.d.ts.map +0 -1
  97. package/dist/resolver.js.map +0 -1
  98. package/dist/types/context.d.ts.map +0 -1
  99. package/dist/types/context.js.map +0 -1
  100. package/dist/types/events.d.ts.map +0 -1
  101. package/dist/types/events.js.map +0 -1
  102. package/dist/types/extensions.d.ts.map +0 -1
  103. package/dist/types/extensions.js.map +0 -1
  104. package/dist/types/limitations.d.ts.map +0 -1
  105. package/dist/types/limitations.js.map +0 -1
  106. package/dist/types.d.ts.map +0 -1
  107. package/dist/types.js.map +0 -1
  108. package/dist/utils/cloudBackup.d.ts.map +0 -1
  109. package/dist/utils/cloudBackup.js.map +0 -1
  110. package/dist/utils/dataExport.d.ts.map +0 -1
  111. package/dist/utils/dataExport.js.map +0 -1
  112. package/dist/utils/dataImport.d.ts.map +0 -1
  113. package/dist/utils/dataImport.js.map +0 -1
  114. package/dist/utils/migration.d.ts.map +0 -1
  115. package/dist/utils/migration.js.map +0 -1
  116. package/dist/utils/parameterUtils.d.ts.map +0 -1
  117. package/dist/utils/parameterUtils.js.map +0 -1
  118. package/dist/utils/syncManager.d.ts.map +0 -1
  119. package/dist/utils/syncManager.js.map +0 -1
  120. /package/dist/{types → core/contracts}/context.js +0 -0
  121. /package/dist/{types → core/contracts}/events.js +0 -0
  122. /package/dist/{types → core/contracts}/extensions.js +0 -0
  123. /package/dist/{types → core/contracts}/limitations.d.ts +0 -0
  124. /package/dist/{types → core/contracts}/limitations.js +0 -0
  125. /package/dist/{utils → features/migrations}/dataExport.js +0 -0
  126. /package/dist/{utils → features/migrations}/dataImport.js +0 -0
  127. /package/dist/{utils → shared}/parameterUtils.js +0 -0
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Framers
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Framers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,363 +1,241 @@
1
- # SQL Storage Adapter
2
-
3
- [![npm version](https://img.shields.io/npm/v/@framers/sql-storage-adapter.svg)](https://www.npmjs.com/package/@framers/sql-storage-adapter)
4
- [![CI](https://github.com/wearetheframers/sql-storage-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/wearetheframers/sql-storage-adapter/actions/workflows/ci.yml)
5
- [![codecov](https://codecov.io/gh/wearetheframers/sql-storage-adapter/branch/master/graph/badge.svg)](https://codecov.io/gh/wearetheframers/sql-storage-adapter)
6
- [![TypeScript](https://img.shields.io/badge/TypeScript-5.6-blue.svg)](https://www.typescriptlang.org/)
7
-
8
- > One SQL interface for Node.js, browsers, and mobile apps. Write your database code once, run it anywhere.
9
-
10
- **[Documentation](https://wearetheframers.github.io/sql-storage-adapter/)** | **[GitHub](https://github.com/wearetheframers/sql-storage-adapter)** | **[NPM](https://www.npmjs.com/package/@framers/sql-storage-adapter)**
11
-
12
- ## Why?
13
-
14
- Build apps that work across platforms without rewriting database code:
15
- - 🖥️ **Desktop** (Electron) - fast local storage with better-sqlite3
16
- - 🌐 **Web** - in-browser SQL with sql.js (WebAssembly)
17
- - 📱 **Mobile** (Capacitor/React Native) - native SQLite
18
- - ☁️ **Server** - PostgreSQL or SQLite
19
-
20
- ```typescript
21
- import { createDatabase } from '@framers/sql-storage-adapter';
22
-
23
- // Auto-picks the best adapter for your environment
24
- const db = await createDatabase();
25
-
26
- // Same code everywhere
27
- await db.run('INSERT INTO users (name) VALUES (?)', ['Alice']);
28
- const user = await db.get('SELECT * FROM users WHERE id = ?', [1]);
29
- ```
30
-
31
- ## Installation
32
-
33
- ```bash
34
- npm install @framers/sql-storage-adapter
35
-
36
- # Install adapters you need:
37
- npm install better-sqlite3 # Desktop (Node.js/Electron)
38
- npm install pg # PostgreSQL (servers)
39
- npm install @capacitor-community/sqlite # Mobile (Capacitor)
40
- # sql.js included - no extra install needed
41
- ```
42
-
43
- ## Quick Start
44
-
45
- ```typescript
46
- import { createDatabase } from '@framers/sql-storage-adapter';
47
-
48
- // Create database (auto-detects best option)
49
- const db = await createDatabase();
50
-
51
- // Create table
52
- await db.exec(`
53
- CREATE TABLE IF NOT EXISTS todos (
54
- id INTEGER PRIMARY KEY,
55
- task TEXT NOT NULL,
56
- done INTEGER DEFAULT 0
57
- )
58
- `);
59
-
60
- // Insert data
61
- await db.run('INSERT INTO todos (task) VALUES (?)', ['Buy groceries']);
62
-
63
- // Query data
64
- const todos = await db.all('SELECT * FROM todos WHERE done = 0');
65
-
66
- // Clean up
67
- await db.close();
68
- ```
69
-
70
- ## Core API
71
-
72
- ```typescript
73
- // Execute SQL (no results)
74
- await db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
75
-
76
- // Run mutation (INSERT/UPDATE/DELETE)
77
- const result = await db.run('INSERT INTO users (name) VALUES (?)', ['Alice']);
78
- console.log(result.lastInsertRowid); // Auto-increment ID
79
-
80
- // Get single row
81
- const user = await db.get<User>('SELECT * FROM users WHERE id = ?', [1]);
82
-
83
- // Get all rows
84
- const users = await db.all<User>('SELECT * FROM users');
85
-
86
- // Transactions (automatic commit/rollback)
87
- await db.transaction(async (tx) => {
88
- await tx.run('INSERT INTO users (name) VALUES (?)', ['Bob']);
89
- await tx.run('INSERT INTO posts (user_id, title) VALUES (?, ?)', [1, 'Hello']);
90
- });
91
-
92
- // Batch operations (if supported)
93
- if (db.capabilities.has('batch')) {
94
- await db.batch([
95
- { statement: 'INSERT INTO users (name) VALUES (?)', parameters: ['Alice'] },
96
- { statement: 'INSERT INTO users (name) VALUES (?)', parameters: ['Bob'] }
97
- ]);
98
- }
99
-
100
- // Close connection
101
- await db.close();
102
- ```
103
-
104
- ## Adapter Selection
105
-
106
- Auto-detection order:
107
-
108
- ```
109
- 🖥️ Node.js: better-sqlite3 → sql.js
110
- 🌐 Browser: sql.js
111
- 📱 Mobile: capacitor-sqlite → sql.js
112
- ☁️ Server: postgres better-sqlite3 → sql.js
113
- ```
114
-
115
- Override with specific adapter:
116
-
117
- ```typescript
118
- // Use PostgreSQL explicitly
119
- const db = await createDatabase({
120
- url: 'postgresql://localhost/mydb'
121
- });
122
-
123
- // Use SQLite explicitly
124
- const db = await createDatabase({
125
- file: './app.db'
126
- });
127
-
128
- // Custom priority
129
- const db = await createDatabase({
130
- priority: ['postgres', 'better-sqlite3'],
131
- postgres: { connectionString: process.env.DATABASE_URL },
132
- filePath: './fallback.db'
133
- });
134
- ```
135
-
136
- ## Auto-Sync & Offline Support
137
-
138
- Build offline-first apps with automatic cloud sync:
139
-
140
- ```typescript
141
- import { createSyncManager } from '@framers/sql-storage-adapter';
142
-
143
- const manager = await createSyncManager({
144
- primary: './app.db', // Local SQLite
145
- remote: process.env.DATABASE_URL, // Cloud PostgreSQL
146
- sync: {
147
- mode: 'auto', // Auto-sync after writes
148
- conflictStrategy: 'last-write-wins'
149
- }
150
- });
151
-
152
- // Work offline
153
- await manager.db.run('INSERT INTO tasks (title) VALUES (?)', ['Buy milk']);
154
-
155
- // Sync manually when needed
156
- const result = await manager.sync();
157
- console.log(`Synced ${result.recordsSynced} records`);
158
- ```
159
-
160
- **Sync Modes:**
161
- - `manual` - Call `sync()` explicitly (default)
162
- - `auto` - Syncs after writes (debounced)
163
- - `periodic` - Syncs every N seconds
164
- - `realtime` - Syncs immediately on every write
165
- - `on-reconnect` - Syncs when network returns
166
-
167
- **Conflict Strategies:**
168
- - `last-write-wins` - Newest timestamp wins (default)
169
- - `local-wins` - Local always wins
170
- - `remote-wins` - Server is authority
171
- - `merge` - Custom merge function
172
- - `keep-both` - Duplicate records
173
-
174
- 📖 **[Complete Offline Sync Guide](./guides/OFFLINE_SYNC.md)** - 8 real-world examples with mobile optimization
175
-
176
- ## Cloud Backups
177
-
178
- Automatic S3-compatible backups (AWS S3, Cloudflare R2, MinIO):
179
-
180
- ```typescript
181
- import { S3Client } from '@aws-sdk/client-s3';
182
- import { createCloudBackupManager } from '@framers/sql-storage-adapter';
183
-
184
- const s3 = new S3Client({
185
- region: 'us-east-1',
186
- credentials: {
187
- accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
188
- secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!
189
- }
190
- });
191
-
192
- const manager = createCloudBackupManager(db, s3, 'my-backups', {
193
- interval: 3600000, // Hourly backups
194
- maxBackups: 24, // Keep 24 backups
195
- options: { compression: 'gzip' }
196
- });
197
-
198
- manager.start(); // Auto-backup every hour
199
-
200
- // Manual backup
201
- await manager.backupNow();
202
-
203
- // Restore
204
- await manager.restore('backups/my-database-2024-01-15.json.gz');
205
- ```
206
-
207
- ## Data Migration
208
-
209
- Export, import, and migrate between adapters:
210
-
211
- ```typescript
212
- import {
213
- migrateLocalToSupabase,
214
- exportAsJSON,
215
- importFromJSON
216
- } from '@framers/sql-storage-adapter';
217
-
218
- // Export to JSON
219
- const backup = await exportAsJSON(db, {
220
- tables: ['users', 'posts'],
221
- includeSchema: true
222
- });
223
-
224
- // Import from JSON
225
- await importFromJSON(db, backup, {
226
- dropTables: true,
227
- batchSize: 100
228
- });
229
-
230
- // Migrate SQLite PostgreSQL
231
- const result = await migrateLocalToSupabase(sqliteDb, postgresDb, {
232
- verify: true,
233
- onConflict: 'replace'
234
- });
235
-
236
- console.log(`Migrated ${result.rowsImported} rows in ${result.duration}ms`);
237
- ```
238
-
239
- ## PostgreSQL Remote Connections
240
-
241
- Connect to hosted databases (AWS RDS, Heroku, Supabase, etc.):
242
-
243
- ```typescript
244
- import { connectDatabase } from '@framers/sql-storage-adapter';
245
-
246
- // Auto-detect from DATABASE_URL
247
- const db = await connectDatabase(process.env.DATABASE_URL);
248
-
249
- // Or configure explicitly
250
- const db = await connectDatabase({
251
- host: 'db.example.com',
252
- database: 'myapp',
253
- user: 'dbuser',
254
- password: process.env.DB_PASSWORD,
255
- ssl: true,
256
- max: 20 // Connection pool size
257
- });
258
-
259
- // Same API as SQLite!
260
- const users = await db.all('SELECT * FROM users');
261
- ```
262
-
263
- 📖 **[PostgreSQL Remote Guide](./guides/POSTGRES_REMOTE_CONNECTION.md)** - Cloud provider examples & SSL config
264
-
265
- ## Adapter Comparison
266
-
267
- | Adapter | Best For | Speed | Size | Concurrent |
268
- |---------|----------|-------|------|------------|
269
- | **PostgreSQL** | Production servers | ⚡⚡⚡ | N/A | ✅ Yes |
270
- | **better-sqlite3** | Desktop apps | ⚡⚡⚡ | 6 MB | ❌ No |
271
- | **SQL.js** | Browsers | ⚡ | 2.3 MB | ❌ No |
272
- | **Capacitor** | Mobile apps | ⚡⚡⚡ | ~1 MB | ❌ No |
273
-
274
- ## TypeScript Support
275
-
276
- Full type safety with generics:
277
-
278
- ```typescript
279
- interface User {
280
- id: number;
281
- name: string;
282
- email: string;
283
- }
284
-
285
- // Type-safe queries
286
- const user = await db.get<User>('SELECT * FROM users WHERE id = ?', [1]);
287
- // user: User | null
288
-
289
- const users = await db.all<User>('SELECT * FROM users');
290
- // users: User[]
291
-
292
- // Runtime introspection
293
- const context = db.context;
294
- console.log('Adapter:', db.kind);
295
- console.log('Capabilities:', Array.from(db.capabilities));
296
- console.log('Max connections:', context.getLimitations().maxConnections);
297
- ```
298
-
299
- ## Event Monitoring
300
-
301
- Track queries and performance:
302
-
303
- ```typescript
304
- db.events.on('query:error', (event) => {
305
- console.error('Query failed:', event.statement, event.error);
306
- });
307
-
308
- db.events.on('performance:slow-query', (event) => {
309
- if (event.duration > 1000) {
310
- console.warn(`Slow query (${event.duration}ms):`, event.statement);
311
- }
312
- });
313
-
314
- db.events.on('transaction:rollback', (event) => {
315
- console.warn('Transaction rolled back:', event.error);
316
- });
317
- ```
318
-
319
- ## Examples
320
-
321
- Real-world usage in `examples/`:
322
- - `basic-usage.ts` - Getting started
323
- - `remote-postgres.ts` - Cloud database connections
324
- - `electron-app/` - Desktop app with better-sqlite3
325
- - `browser-extension/` - Chrome extension with SQL.js
326
- - `full-stack/` - Shared code frontend/backend
327
- - `offline-sync.ts` - Auto-sync patterns
328
- - `testing/` - Unit testing strategies
329
-
330
- ## FAQ
331
-
332
- **Q: Which adapter should I use?**
333
- Use `createDatabase()` - it auto-picks the best adapter for your environment.
334
-
335
- **Q: Can I switch adapters later?**
336
- Yes. Use migration functions to move data between adapters.
337
-
338
- **Q: Is this production-ready?**
339
- Yes. Built on battle-tested libraries (pg, better-sqlite3, sql.js). The adapter layer adds <1% overhead.
340
-
341
- **Q: What about schema migrations?**
342
- Use any migration tool: [`node-pg-migrate`](https://github.com/salsita/node-pg-migrate), [`prisma`](https://www.prisma.io/), or raw SQL.
343
-
344
- **Q: How do I handle conflicts in sync?**
345
- Use `conflictStrategy`: `last-write-wins` (default), `local-wins`, `remote-wins`, `merge`, or `keep-both`.
346
-
347
- **Q: Can I sync only on WiFi?**
348
- Yes. Use `mode: 'manual'` and call `sync()` when `isOnWiFi === true`. See [Offline Sync Guide](./guides/OFFLINE_SYNC.md#1-mobile-app-with-wifi-only-sync).
349
-
350
- ## Documentation
351
-
352
- - **[API Documentation](https://wearetheframers.github.io/sql-storage-adapter/)** - Complete TypeDoc reference
353
- - **[Offline Sync Guide](./guides/OFFLINE_SYNC.md)** - Comprehensive sync patterns
354
- - **[PostgreSQL Guide](./guides/POSTGRES_REMOTE_CONNECTION.md)** - Remote database setup
355
- - **[Architecture](./ARCHITECTURE.md)** - Design decisions
356
-
357
- ## Contributing
358
-
359
- See [CONTRIBUTING.md](./CONTRIBUTING.md)
360
-
361
- ## License
362
-
363
- MIT © [The Framers](https://frame.dev)
1
+ # SQL Storage Adapter
2
+
3
+ <p align="center">
4
+ <a href="https://frame.dev" target="_blank" rel="noopener">
5
+ <picture>
6
+ <source media="(prefers-color-scheme: dark)" srcset="./logos/frame-square-blue-dark-no-tagline.svg">
7
+ <source media="(prefers-color-scheme: light)" srcset="./logos/frame-square-blue-no-tagline.svg">
8
+ <img src="./logos/frame-square-blue-no-tagline.svg" alt="Frame.dev" width="200">
9
+ </picture>
10
+ </a>
11
+ </p>
12
+
13
+ [![npm version](https://img.shields.io/npm/v/@framers/sql-storage-adapter.svg?logo=npm&label=npm)](https://www.npmjs.com/package/@framers/sql-storage-adapter)
14
+ [![CI](https://github.com/framersai/sql-storage-adapter/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/framersai/sql-storage-adapter/actions/workflows/ci.yml)
15
+ [![codecov](https://codecov.io/gh/framersai/sql-storage-adapter/branch/master/graph/badge.svg)](https://codecov.io/gh/framersai/sql-storage-adapter)
16
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.6-blue.svg)](https://www.typescriptlang.org/)
17
+ [![License](https://img.shields.io/npm/l/@framers/sql-storage-adapter.svg)](./LICENSE)
18
+
19
+ > Cross-platform SQL access for Node.js, web, and native runtimes with automatic adapter selection and consistent APIs.
20
+
21
+ The SQL Storage Adapter provides a single, ergonomic interface over SQLite (native and WASM), PostgreSQL, Capacitor, IndexedDB, and in-memory stores. It handles adapter discovery, capability detection, and advanced features like cloud backups so you can focus on your application logic.
22
+
23
+ **🆕 NEW:** Full IndexedDB support for browser-native, offline-first web apps!
24
+
25
+ ---
26
+
27
+ - [Features](#features)
28
+ - [Installation](#installation)
29
+ - [Quick Start](#quick-start)
30
+ - [AgentOS Integration](#agentos-integration)
31
+ - [Adapter Matrix](#adapter-matrix)
32
+ - [Configuration & Resolution](#configuration--resolution)
33
+ - [Platform Strategy](#platform-strategy)
34
+ - [CI, Releases, and Badges](#ci-releases-and-badges)
35
+ - [Contributing](#contributing)
36
+ - [License](#license)
37
+
38
+ ## Features
39
+
40
+ - **Auto-detected adapters** – `createDatabase()` inspects environment signals and picks the best backend (native SQLite, PostgreSQL, Capacitor, sql.js, **IndexedDB**, memory, etc.).
41
+ - **Capability-aware API** – consistent CRUD, transactions, batching, and event hooks across adapters with runtime capability introspection.
42
+ - **🆕 IndexedDB** – Browser-native persistence with sql.js for full SQL support in PWAs and offline-first apps.
43
+ - **Cloud backups & migrations** – built-in backup manager with compression, retention policies, and restore helpers plus migration utilities.
44
+ - **Portable packaging** – optional native dependencies; falls back to pure TypeScript/WASM adapters when native modules are unavailable.
45
+ - **AgentOS-first** – Pre-configured schema, typed queries, and auto-detection for AgentOS deployments.
46
+ - **CI-first design** Vitest coverage, Codecov integration, and GitHub Actions workflows for linting, testing, releasing, and npm publish/tag automation.
47
+
48
+ ## Installation
49
+
50
+ ```bash
51
+ # Core package
52
+ npm install @framers/sql-storage-adapter
53
+
54
+ # Install only the adapters you plan to use
55
+ npm install better-sqlite3 # Native SQLite for Node/Electron
56
+ npm install pg # PostgreSQL
57
+ npm install @capacitor-community/sqlite # Capacitor / mobile
58
+ npm install sql.js # WASM SQLite (auto-included for IndexedDB)
59
+ # IndexedDB uses sql.js (no extra install needed)
60
+ ```
61
+
62
+ > Windows users: ensure the Visual Studio Build Tools (C++ workload) are installed before adding `better-sqlite3`. On Linux, install `python3`, `build-essential`, and `libssl-dev` prior to `npm install`.
63
+
64
+ > Note: If `better-sqlite3` cannot be required, install native build tools before `npm install`, ensure your Node version matches available prebuilt binaries, or fall back to `sql.js` or `indexeddb` by setting `STORAGE_ADAPTER=sqljs` or `STORAGE_ADAPTER=indexeddb`.
65
+
66
+ ## Quick Start
67
+
68
+ ```typescript
69
+ import { createDatabase } from '@framers/sql-storage-adapter';
70
+
71
+ async function main() {
72
+ // Automatically selects the best adapter for the current runtime
73
+ const db = await createDatabase();
74
+
75
+ await db.exec(`
76
+ CREATE TABLE IF NOT EXISTS todos (
77
+ id INTEGER PRIMARY KEY,
78
+ task TEXT NOT NULL,
79
+ done INTEGER DEFAULT 0
80
+ )
81
+ `);
82
+
83
+ await db.run('INSERT INTO todos (task) VALUES (?)', ['Ship cross-platform builds']);
84
+ const items = await db.all<{ id: number; task: string; done: number }>('SELECT * FROM todos');
85
+ console.log(items);
86
+
87
+ await db.close();
88
+ }
89
+
90
+ main().catch((error) => {
91
+ console.error('Database bootstrap failed', error);
92
+ process.exit(1);
93
+ });
94
+ ```
95
+
96
+ ## AgentOS Integration
97
+
98
+ ### Recommended: `createAgentOSStorage()`
99
+
100
+ The easiest way to use sql-storage-adapter with AgentOS is the **AgentOS-first API**:
101
+
102
+ ```typescript
103
+ import { createAgentOSStorage } from '@framers/sql-storage-adapter/agentos';
104
+ import { AgentOS } from '@agentos/core';
105
+
106
+ // Auto-detects platform (web, electron, capacitor, node, cloud)
107
+ const storage = await createAgentOSStorage({
108
+ platform: 'auto', // Detects best adapter
109
+ persistence: true,
110
+ });
111
+
112
+ const agentos = new AgentOS();
113
+ await agentos.initialize({
114
+ storageAdapter: storage.getAdapter(), // 🆕 New field in AgentOSConfig
115
+ // ... other config
116
+ });
117
+ ```
118
+
119
+ **Features:**
120
+ - Auto-creates AgentOS tables (conversations, sessions, personas, telemetry)
121
+ - ✅ Platform detection (web → IndexedDB, electron → better-sqlite3, etc.)
122
+ - ✅ Typed query builders for common operations
123
+ - Graceful degradation (e.g., IndexedDB → sql.js fallback)
124
+
125
+ ### Platform-Specific Examples
126
+
127
+ ```typescript
128
+ // Web (Browser): Uses IndexedDB
129
+ const webStorage = await createAgentOSStorage({ platform: 'web' });
130
+
131
+ // Desktop (Electron): Uses better-sqlite3
132
+ const desktopStorage = await createAgentOSStorage({ platform: 'electron' });
133
+
134
+ // Mobile (Capacitor): Uses native SQLite
135
+ const mobileStorage = await createAgentOSStorage({ platform: 'capacitor' });
136
+
137
+ // Cloud (Node): Uses PostgreSQL
138
+ const cloudStorage = await createAgentOSStorage({
139
+ platform: 'cloud',
140
+ postgres: { connectionString: process.env.DATABASE_URL }
141
+ });
142
+ ```
143
+
144
+ See [Platform Strategy Guide](./docs/PLATFORM_STRATEGY.md) for detailed pros/cons and architecture.
145
+
146
+ ## Adapter Matrix
147
+
148
+ | Adapter | Package | Ideal for | Pros | Considerations |
149
+ | --- | --- | --- | --- | --- |
150
+ | **🆕 `indexeddb`** | bundled | **Browsers, PWAs** | Browser-native, async, 50MB-1GB+ quota, SQL via sql.js, offline-first | IndexedDB quotas vary, WASM overhead for SQL |
151
+ | `better-sqlite3` | `better-sqlite3` | Node/Electron, CLI, CI | Native performance, transactional semantics, WAL support | Needs native toolchain; version must match Node ABI |
152
+ | `postgres` | `pg` | Hosted or on-prem PostgreSQL | Connection pooling, rich SQL features, cloud friendly | Requires `DATABASE_URL`/credentials |
153
+ | `sqljs` | bundled | Browsers, serverless edge, native fallback | Pure WASM, no native deps | Write performance limited vs native, optional persistence |
154
+ | `capacitor` | `@capacitor-community/sqlite` | Mobile (iOS/Android, Capacitor) | Native SQLite on mobile, encryption | Requires Capacitor runtime |
155
+ | `memory` | built-in | Unit tests, storybooks, constrained sandboxes | Zero dependencies, instant startup | Non-durable, single-process only |
156
+
157
+ ### Platform Priorities
158
+
159
+ | Platform | Primary Adapter | Fallback | Use Case |
160
+ |----------|----------------|----------|----------|
161
+ | **Web (Browser)** | IndexedDB | sql.js | PWAs, offline-first web apps |
162
+ | **Electron (Desktop)** | better-sqlite3 | sql.js | Desktop apps, dev tools |
163
+ | **Capacitor (Mobile)** | capacitor | IndexedDB | iOS/Android native apps |
164
+ | **Node.js** | better-sqlite3 | Postgres, sql.js | CLI tools, local servers |
165
+ | **Cloud (Serverless)** | Postgres | better-sqlite3 | Multi-tenant SaaS, APIs |
166
+
167
+ ## Configuration & Resolution
168
+
169
+ - `resolveStorageAdapter` inspects:
170
+ - explicit options (`priority`, `type`, adapter configs),
171
+ - environment variables (`STORAGE_ADAPTER`, `DATABASE_URL`),
172
+ - runtime hints (Capacitor detection, browser globals, IndexedDB availability).
173
+ - Adapters are attempted in priority order until one opens successfully; a `StorageResolutionError` includes the full failure chain.
174
+ - Provide `priority: ['indexeddb', 'sqljs']` for browser bundles or tests where native modules shouldn't load.
175
+ - Use `createCloudBackupManager` for S3-compatible backups with gzip compression and retention limits.
176
+
177
+ ### IndexedDB-Specific Config
178
+
179
+ ```typescript
180
+ import { IndexedDbAdapter } from '@framers/sql-storage-adapter';
181
+
182
+ const adapter = new IndexedDbAdapter({
183
+ dbName: 'my-app-db', // IndexedDB database name
184
+ storeName: 'sqliteDb', // Object store name
185
+ autoSave: true, // Auto-save to IndexedDB after writes
186
+ saveIntervalMs: 5000, // Batch writes every 5s
187
+ });
188
+
189
+ await adapter.open();
190
+ ```
191
+
192
+ **Key Features:**
193
+ - Transactions (via sql.js)
194
+ - Persistence (IndexedDB)
195
+ - Export/import (Uint8Array SQLite file format)
196
+ - ✅ Auto-save with batching (reduce IDB overhead)
197
+
198
+ ## Platform Strategy
199
+
200
+ See [**PLATFORM_STRATEGY.md**](./docs/PLATFORM_STRATEGY.md) for a comprehensive guide on:
201
+ - Graceful degradation patterns
202
+ - Platform-specific pros/cons
203
+ - Performance benchmarks
204
+ - Offline-first architectures
205
+ - AgentOS-specific recommendations
206
+
207
+ **TL;DR:** Use IndexedDB for web, better-sqlite3 for desktop, capacitor for mobile, Postgres for cloud.
208
+
209
+ ## CI, Releases, and Badges
210
+
211
+ - GitHub Actions workflows:
212
+ - `ci.yml` runs lint, tests, and coverage on every branch.
213
+ - `release.yml` publishes new versions to npm, tags the commit (`vX.Y.Z`), and creates/updates the GitHub Release when `CHANGELOG.md` and `package.json` bump the version.
214
+ - Check badge health whenever builds fail:
215
+ - CI badge should be green for `master`.
216
+ - Codecov badge updates after coverage reports upload.
217
+ - npm badge reflects the latest published version.
218
+ - Manual verification commands:
219
+ ```bash
220
+ npm info @framers/sql-storage-adapter version
221
+ pnpm --filter sql-storage-adapter test
222
+ pnpm --filter sql-storage-adapter build
223
+ ```
224
+ - See [RELEASING.md](./RELEASING.md) for the automated release flow, required secrets (`NPM_TOKEN`), and manual fallback steps.
225
+
226
+
227
+
228
+ ## Contributing
229
+
230
+ - Read [CONTRIBUTING.md](./CONTRIBUTING.md) for coding standards, lint/test commands, and pull request guidelines.
231
+ - Architecture notes live in [ARCHITECTURE.md](./ARCHITECTURE.md); API docs can be regenerated with `pnpm --filter sql-storage-adapter run docs`.
232
+
233
+ ## License
234
+
235
+ [MIT](./LICENSE)
236
+
237
+ ---
238
+
239
+ <p align="center">
240
+ Built and maintained by <a href="https://frame.dev" target="_blank" rel="noopener">Frame.dev</a>
241
+ </p>