Skip to content

Commit 5ada083

Browse files
fix: capacitor readTransaction not allowed errors
1 parent 1530190 commit 5ada083

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/common': patch
3+
---
4+
5+
Fixed readTransaction method throwing "not allowed in read-only mode" errors

packages/capacitor/src/adapter/CapacitorSQLiteAdapter.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,29 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
120120
protected generateLockContext(db: SQLiteDBConnection): LockContext {
121121
const _execute = async (query: string, params: any[] = []): Promise<QueryResult> => {
122122
const platform = Capacitor.getPlatform();
123+
124+
const _query = async (query: string, params: any[] = []) => {
125+
const result = await db.query(query, params);
126+
const arrayResult = result.values ?? [];
127+
return {
128+
rowsAffected: 0,
129+
rows: {
130+
_array: arrayResult,
131+
length: arrayResult.length,
132+
item: (idx: number) => arrayResult[idx]
133+
}
134+
};
135+
};
136+
137+
if (db.getConnectionReadOnly()) {
138+
return _query(query, params);
139+
}
140+
123141
if (platform == 'android') {
124142
// Android: use query for SELECT and executeSet for mutations
125143
// We cannot use `run` here for both cases.
126144
if (query.toLowerCase().trim().startsWith('select')) {
127-
const result = await db.query(query, params);
128-
const arrayResult = result.values ?? [];
129-
return {
130-
rowsAffected: 0,
131-
rows: {
132-
_array: arrayResult,
133-
length: arrayResult.length,
134-
item: (idx: number) => arrayResult[idx]
135-
}
136-
};
145+
return _query(query, params);
137146
} else {
138147
const result = await db.executeSet([{ statement: query, values: params }], false);
139148
return {

0 commit comments

Comments
 (0)