|
| 1 | +# @op-engineering/op-sqlite-node |
| 2 | + |
| 3 | +Node.js adapter for the `@op-engineering/op-sqlite` API using `better-sqlite3`. |
| 4 | + |
| 5 | +This package provides the same TypeScript API as the React Native version but runs on Node.js, allowing you to share database logic between your React Native app and Node.js services. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +The API is identical to the React Native version, so you can share code between platforms: |
| 10 | + |
| 11 | +```typescript |
| 12 | +import { open } from '@op-engineering/op-sqlite-node'; |
| 13 | + |
| 14 | +// Open a database |
| 15 | +const db = open({ |
| 16 | + name: 'mydb.sqlite', |
| 17 | + location: './data' // optional, defaults to current directory |
| 18 | +}); |
| 19 | + |
| 20 | +// Execute queries synchronously |
| 21 | +const result = db.executeSync('SELECT * FROM users WHERE id = ?', [1]); |
| 22 | +console.log(result.rows); |
| 23 | + |
| 24 | +// Or asynchronously |
| 25 | +const asyncResult = await db.execute('SELECT * FROM users'); |
| 26 | +console.log(asyncResult.rows); |
| 27 | + |
| 28 | +// Use transactions |
| 29 | +await db.transaction(async (tx) => { |
| 30 | + await tx.execute('INSERT INTO users (name) VALUES (?)', ['John']); |
| 31 | + await tx.execute('INSERT INTO posts (user_id, title) VALUES (?, ?)', [1, 'Hello']); |
| 32 | +}); |
| 33 | + |
| 34 | +// Execute batch operations |
| 35 | +await db.executeBatch([ |
| 36 | + ['CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)'], |
| 37 | + ['INSERT INTO users (name) VALUES (?)', ['Alice']], |
| 38 | + ['INSERT INTO users (name) VALUES (?)', ['Bob']], |
| 39 | +]); |
| 40 | + |
| 41 | +// Close the database |
| 42 | +db.close(); |
| 43 | +``` |
| 44 | + |
| 45 | +## API Differences from React Native |
| 46 | + |
| 47 | +While the API surface is identical, there are some behavioral differences: |
| 48 | + |
| 49 | +### Supported Features |
| 50 | +- ✅ `open()` - Open database with name and location |
| 51 | +- ✅ `openV2()` - Open database with full path |
| 52 | +- ✅ `execute()` / `executeSync()` - Query execution |
| 53 | +- ✅ `executeRaw()` / `executeRawSync()` - Raw array results |
| 54 | +- ✅ `executeBatch()` - Batch operations in transaction |
| 55 | +- ✅ `transaction()` - Transaction support |
| 56 | +- ✅ `prepareStatement()` - Prepared statements |
| 57 | +- ✅ `attach()` / `detach()` - Database attachment |
| 58 | +- ✅ `loadFile()` - Load and execute SQL files |
| 59 | +- ✅ `loadExtension()` - SQLite extensions (if enabled in better-sqlite3) |
| 60 | +- ✅ `close()` / `delete()` - Database management |
| 61 | + |
| 62 | +### Not Supported / Limited |
| 63 | +- ❌ `openRemote()` - LibSQL remote connections (use libsql client directly) |
| 64 | +- ❌ `openSync()` - LibSQL sync functionality (use libsql client directly) |
| 65 | +- ❌ `sync()` - LibSQL sync method |
| 66 | +- ❌ `reactiveExecute()` - Reactive queries (React Native specific) |
| 67 | +- ❌ `updateHook()` - Update hooks (not supported by better-sqlite3) |
| 68 | +- ❌ `setReservedBytes()` / `getReservedBytes()` - SQLCipher specific |
| 69 | +- ⚠️ `encryptionKey` - Not supported (use @journeyapps/sqlcipher instead) |
| 70 | +- ⚠️ `executeWithHostObjects()` - Falls back to regular execute |
0 commit comments