@powersync/op-sqlite 0.0.3 → 0.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/op-sqlite",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "PowerSync - sync Postgres or MongoDB with SQLite in your React Native app for offline-first and real-time data",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./lib/commonjs/index.js",
@@ -58,7 +58,7 @@
58
58
  "access": "public"
59
59
  },
60
60
  "peerDependencies": {
61
- "@op-engineering/op-sqlite": "^9.1.3",
61
+ "@op-engineering/op-sqlite": "^9.2.1",
62
62
  "@powersync/common": "^1.20.0",
63
63
  "react": "*",
64
64
  "react-native": "*"
@@ -68,7 +68,7 @@
68
68
  "@powersync/common": "1.20.0"
69
69
  },
70
70
  "devDependencies": {
71
- "@op-engineering/op-sqlite": "^9.1.3",
71
+ "@op-engineering/op-sqlite": "^9.2.1",
72
72
  "@react-native/eslint-config": "^0.73.1",
73
73
  "@types/async-lock": "^1.4.0",
74
74
  "@types/react": "^18.2.44",
@@ -11,7 +11,7 @@ import { ANDROID_DATABASE_PATH, IOS_LIBRARY_PATH, open, type DB } from '@op-engi
11
11
  import Lock from 'async-lock';
12
12
  import { OPSQLiteConnection } from './OPSQLiteConnection';
13
13
  import { NativeModules, Platform } from 'react-native';
14
- import { DEFAULT_SQLITE_OPTIONS, SqliteOptions } from './SqliteOptions';
14
+ import { SqliteOptions } from './SqliteOptions';
15
15
 
16
16
  /**
17
17
  * Adapter for React Native Quick SQLite
@@ -50,15 +50,10 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
50
50
  }
51
51
 
52
52
  protected async init() {
53
- const { lockTimeoutMs, journalMode, journalSizeLimit, synchronous } = this.options.sqliteOptions;
54
- // const { dbFilename, dbLocation } = this.options;
53
+ const { lockTimeoutMs, journalMode, journalSizeLimit, synchronous, encryptionKey } = this.options.sqliteOptions;
55
54
  const dbFilename = this.options.name;
56
- //This is needed because an undefined dbLocation will cause the open function to fail
57
- const location = this.getDbLocation(this.options.dbLocation);
58
- const DB: DB = open({
59
- name: dbFilename,
60
- location: location
61
- });
55
+
56
+ this.writeConnection = await this.openConnection(dbFilename);
62
57
 
63
58
  const statements: string[] = [
64
59
  `PRAGMA busy_timeout = ${lockTimeoutMs}`,
@@ -70,7 +65,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
70
65
  for (const statement of statements) {
71
66
  for (let tries = 0; tries < 30; tries++) {
72
67
  try {
73
- await DB.execute(statement);
68
+ await this.writeConnection!.execute(statement);
74
69
  break;
75
70
  } catch (e: any) {
76
71
  if (e instanceof Error && e.message.includes('database is locked') && tries < 29) {
@@ -82,34 +77,24 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
82
77
  }
83
78
  }
84
79
 
85
- this.loadExtension(DB);
86
-
87
- await DB.execute('SELECT powersync_init()');
80
+ // Changes should only occur in the write connection
81
+ this.writeConnection!.registerListener({
82
+ tablesUpdated: (notification) => this.iterateListeners((cb) => cb.tablesUpdated?.(notification))
83
+ });
88
84
 
89
85
  this.readConnections = [];
90
86
  for (let i = 0; i < READ_CONNECTIONS; i++) {
91
87
  // Workaround to create read-only connections
92
88
  let dbName = './'.repeat(i + 1) + dbFilename;
93
- const conn = await this.openConnection(location, dbName);
89
+ const conn = await this.openConnection(dbName);
94
90
  await conn.execute('PRAGMA query_only = true');
95
91
  this.readConnections.push(conn);
96
92
  }
97
-
98
- this.writeConnection = new OPSQLiteConnection({
99
- baseDB: DB
100
- });
101
-
102
- // Changes should only occur in the write connection
103
- this.writeConnection!.registerListener({
104
- tablesUpdated: (notification) => this.iterateListeners((cb) => cb.tablesUpdated?.(notification))
105
- });
106
93
  }
107
94
 
108
- protected async openConnection(dbLocation: string, filenameOverride?: string): Promise<OPSQLiteConnection> {
109
- const DB: DB = open({
110
- name: filenameOverride ?? this.options.name,
111
- location: dbLocation
112
- });
95
+ protected async openConnection(filenameOverride?: string): Promise<OPSQLiteConnection> {
96
+ const dbFilename = filenameOverride ?? this.options.name;
97
+ const DB: DB = this.openDatabase(dbFilename, this.options.sqliteOptions.encryptionKey);
113
98
 
114
99
  //Load extension for all connections
115
100
  this.loadExtension(DB);
@@ -129,6 +114,24 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
129
114
  }
130
115
  }
131
116
 
117
+ private openDatabase(dbFilename: string, encryptionKey?: string): DB {
118
+ //This is needed because an undefined/null dbLocation will cause the open function to fail
119
+ const location = this.getDbLocation(this.options.dbLocation);
120
+ //Simarlily if the encryption key is undefined/null when using SQLCipher it will cause the open function to fail
121
+ if (encryptionKey) {
122
+ return open({
123
+ name: dbFilename,
124
+ location: location,
125
+ encryptionKey: encryptionKey
126
+ });
127
+ } else {
128
+ return open({
129
+ name: dbFilename,
130
+ location: location
131
+ });
132
+ }
133
+ }
134
+
132
135
  private loadExtension(DB: DB) {
133
136
  if (Platform.OS === 'ios') {
134
137
  const bundlePath: string = NativeModules.PowerSyncOpSqlite.getBundlePath();
@@ -23,6 +23,12 @@ export interface SqliteOptions {
23
23
  * Set to null or zero to fail immediately when the database is locked.
24
24
  */
25
25
  lockTimeoutMs?: number;
26
+
27
+ /**
28
+ * Encryption key for the database.
29
+ * If set, the database will be encrypted using SQLCipher.
30
+ */
31
+ encryptionKey?: string;
26
32
  }
27
33
 
28
34
  // SQLite journal mode. Set on the primary connection.
@@ -36,14 +42,14 @@ enum SqliteJournalMode {
36
42
  truncate = 'TRUNCATE',
37
43
  persist = 'PERSIST',
38
44
  memory = 'MEMORY',
39
- off = 'OFF',
45
+ off = 'OFF'
40
46
  }
41
47
 
42
48
  // SQLite file commit mode.
43
49
  enum SqliteSynchronous {
44
50
  normal = 'NORMAL',
45
51
  full = 'FULL',
46
- off = 'OFF',
52
+ off = 'OFF'
47
53
  }
48
54
 
49
55
  export const DEFAULT_SQLITE_OPTIONS: Required<SqliteOptions> = {
@@ -51,4 +57,5 @@ export const DEFAULT_SQLITE_OPTIONS: Required<SqliteOptions> = {
51
57
  synchronous: SqliteSynchronous.normal,
52
58
  journalSizeLimit: 6 * 1024 * 1024,
53
59
  lockTimeoutMs: 30000,
60
+ encryptionKey: null
54
61
  };