Skip to content

Commit cfec587

Browse files
committed
wip
1 parent 074f4cd commit cfec587

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

demos/react-native-supabase-todolist/library/powersync/system.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import '@azure/core-asynciterator-polyfill';
22

3-
import { createBaseLogger, LogLevel, PowerSyncDatabase, SyncClientImplementation } from '@powersync/react-native';
3+
import {
4+
createBaseLogger,
5+
LogLevel,
6+
PowerSyncDatabase,
7+
SyncClientImplementation,
8+
SyncStreamConnectionMethod
9+
} from '@powersync/react-native';
10+
11+
import { SQLJSOpenFactory } from '@powersync/adapter-sql-js';
412
import React from 'react';
513
import { SupabaseStorageAdapter } from '../storage/SupabaseStorageAdapter';
614

@@ -27,13 +35,22 @@ export class System {
2735
this.kvStorage = new KVStorage();
2836
this.supabaseConnector = new SupabaseConnector(this);
2937
this.storage = this.supabaseConnector.storage;
38+
// this.powersync = new PowerSyncDatabase({
39+
// schema: AppSchema,
40+
// database: {
41+
// dbFilename: 'sqlite.db'
42+
// },
43+
// logger
44+
// });
45+
3046
this.powersync = new PowerSyncDatabase({
3147
schema: AppSchema,
32-
database: {
48+
database: new SQLJSOpenFactory({
3349
dbFilename: 'sqlite.db'
34-
},
50+
}),
3551
logger
3652
});
53+
3754
/**
3855
* The snippet below uses OP-SQLite as the default database adapter.
3956
* You will have to uninstall `@journeyapps/react-native-quick-sqlite` and
@@ -68,7 +85,10 @@ export class System {
6885

6986
async init() {
7087
await this.powersync.init();
71-
await this.powersync.connect(this.supabaseConnector, { clientImplementation: SyncClientImplementation.RUST });
88+
await this.powersync.connect(this.supabaseConnector, {
89+
clientImplementation: SyncClientImplementation.RUST
90+
// connectionMethod: SyncStreamConnectionMethod.HTTP
91+
});
7292

7393
if (this.attachmentQueue) {
7494
await this.attachmentQueue.init();

demos/react-native-supabase-todolist/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
"scripts": {
66
"android": "expo run:android",
77
"ios": "expo run:ios",
8+
"start": "expo start",
89
"clean": "watchman watch-del-all && rm -rf .expo"
910
},
1011
"dependencies": {
1112
"@azure/core-asynciterator-polyfill": "^1.0.2",
1213
"@expo/vector-icons": "^14.0.3",
1314
"@journeyapps/react-native-quick-sqlite": "^2.4.6",
15+
"@powersync/adapter-sql-js": "workspace:*",
1416
"@powersync/attachments": "workspace:*",
1517
"@powersync/common": "workspace:*",
1618
"@powersync/react": "workspace:*",

packages/adapter-sql-js/src/SQLJSAdapter.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,39 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
113113
};
114114
}
115115

116+
/**
117+
* Preprocesses parameters to convert ArrayBuffer objects to Uint8Array
118+
* which SQL.js can properly handle in React Native environments
119+
*/
120+
protected preprocessParams(params?: any[]): any[] | undefined {
121+
if (!params) {
122+
return params;
123+
}
124+
125+
return params.map((param) => {
126+
// Only convert actual ArrayBuffer instances, be very specific to avoid
127+
// accidentally converting numeric values or other legitimate types
128+
if (param instanceof ArrayBuffer) {
129+
return new Uint8Array(param);
130+
}
131+
132+
// More conservative check for cross-context ArrayBuffer detection
133+
// Only convert if it's clearly an ArrayBuffer with the right properties
134+
if (
135+
param &&
136+
typeof param === 'object' &&
137+
param.constructor?.name === 'ArrayBuffer' &&
138+
typeof param.byteLength === 'number' &&
139+
typeof param.slice === 'function'
140+
) {
141+
return new Uint8Array(param);
142+
}
143+
144+
// Leave all other parameters unchanged (numbers, strings, etc.)
145+
return param;
146+
});
147+
}
148+
116149
protected async init(): Promise<SQLJs.Database> {
117150
const SQL = await SQLJs({
118151
locateFile: (filename: any) => `../dist/${filename}`,
@@ -143,8 +176,9 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
143176
const rawResults: any[][] = [];
144177
let columnNames: string[] | null = null;
145178
try {
146-
if (params) {
147-
statement.bind(params);
179+
const processedParams = this.preprocessParams(params);
180+
if (processedParams) {
181+
statement.bind(processedParams);
148182
}
149183
while (statement.step()) {
150184
if (!columnNames) {
@@ -194,8 +228,9 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
194228
const statement = db.prepare(query);
195229
const rawResults: any[][] = [];
196230
try {
197-
if (params) {
198-
statement.bind(params);
231+
const processedParams = this.preprocessParams(params);
232+
if (processedParams) {
233+
statement.bind(processedParams);
199234
}
200235
while (statement.step()) {
201236
rawResults.push(statement.get());
@@ -230,7 +265,8 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
230265
const stmt = db.prepare(query);
231266
try {
232267
for (const paramSet of params) {
233-
stmt.run(paramSet);
268+
const processedParams = this.preprocessParams(paramSet);
269+
stmt.run(processedParams);
234270
totalRowsAffected += db.getRowsModified();
235271
}
236272

0 commit comments

Comments
 (0)