|
| 1 | +# PowerSync SQL-JS Adapter |
| 2 | + |
| 3 | +A development package for PowerSync which uses [SQL.js](https://sql.js.org/#/) to provide a pure JavaScript SQLite implementation. |
| 4 | +This eliminates the need for native dependencies and enables seamless development with Expo Go and other JavaScript-only environments. |
| 5 | + |
| 6 | +This adapter is specifically intended to streamline the development workflow and will be much slower than DB adapters that use native dependencies. |
| 7 | +Every write operation triggers a complete rewrite of the entire database file to persistent storage, not just the changed data. |
| 8 | +In addition to the perfomance overheads, this adapter doesn't provide any of the SQLite consistency guarantees - you may end up with missing data or a corrupted database file if the app is killed while writing to the database file. |
| 9 | + |
| 10 | +For production use, when building React Native apps we recommend switching to our [react-native-quick-sqlite](https://www.npmjs.com/package/@journeyapps/react-native-quick-sqlite) or [OP-SQLite](https://www.npmjs.com/package/@powersync/op-sqlite) adapters when making production builds as they give substantially better performance. |
| 11 | + |
| 12 | +## Note: Alpha Release |
| 13 | + |
| 14 | +This package is currently in an alpha release. |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +By default the SQLJS adapter will be in-memory. Read further for persister examples. |
| 19 | + |
| 20 | +```tsx |
| 21 | +import { SQLJSOpenFactory } from '@powersync/adapter-sql-js'; |
| 22 | + |
| 23 | +const powersync = new PowerSyncDatabase({ |
| 24 | + schema: AppSchema, |
| 25 | + database: new SQLJSOpenFactory({ |
| 26 | + dbFilename: 'powersync.db' |
| 27 | + }) |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +## Persister examples |
| 32 | + |
| 33 | +### Expo |
| 34 | + |
| 35 | +We can use the [Expo File System](https://docs.expo.dev/versions/latest/sdk/filesystem/) to persist the database in an Expo app. |
| 36 | + |
| 37 | +```tsx |
| 38 | +import { PowerSyncDatabase, SQLJSOpenFactory, SQLJSPersister } from '@powersync/react-native'; |
| 39 | +import * as FileSystem from 'expo-file-system'; |
| 40 | + |
| 41 | +const powersync = new PowerSyncDatabase({ |
| 42 | + schema: AppSchema, |
| 43 | + database: new SQLJSOpenFactory({ |
| 44 | + dbFilename: 'powersync.db', |
| 45 | + persister: createSQLJSPersister('powersync.db') |
| 46 | + }) |
| 47 | +}); |
| 48 | + |
| 49 | +const createSQLJSPersister = (dbFilename: string): SQLJSPersister => { |
| 50 | + const dbPath = `${FileSystem.documentDirectory}${dbFilename}`; |
| 51 | + |
| 52 | + return { |
| 53 | + readFile: async (): Promise<ArrayLike<number> | Buffer | null> => { |
| 54 | + try { |
| 55 | + const fileInfo = await FileSystem.getInfoAsync(dbPath); |
| 56 | + if (!fileInfo.exists) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + const result = await FileSystem.readAsStringAsync(dbPath, { |
| 61 | + encoding: FileSystem.EncodingType.Base64 |
| 62 | + }); |
| 63 | + |
| 64 | + const binary = atob(result); |
| 65 | + const bytes = new Uint8Array(binary.length); |
| 66 | + for (let i = 0; i < binary.length; i++) { |
| 67 | + bytes[i] = binary.charCodeAt(i); |
| 68 | + } |
| 69 | + return bytes; |
| 70 | + } catch (error) { |
| 71 | + console.error('Error reading database file:', error); |
| 72 | + return null; |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + writeFile: async (data: ArrayLike<number> | Buffer): Promise<void> => { |
| 77 | + try { |
| 78 | + const uint8Array = new Uint8Array(data); |
| 79 | + const binary = Array.from(uint8Array, (byte) => String.fromCharCode(byte)).join(''); |
| 80 | + const base64 = btoa(binary); |
| 81 | + |
| 82 | + await FileSystem.writeAsStringAsync(dbPath, base64, { |
| 83 | + encoding: FileSystem.EncodingType.Base64 |
| 84 | + }); |
| 85 | + } catch (error) { |
| 86 | + console.error('Error writing database file:', error); |
| 87 | + throw error; |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | +}; |
| 92 | +``` |
0 commit comments