Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Oct 7, 2024
1 parent 7ae4111 commit 25e41b5
Show file tree
Hide file tree
Showing 10 changed files with 22,624 additions and 17,392 deletions.
3 changes: 1 addition & 2 deletions demos/react-native-supabase-todolist/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { PowerSyncContext } from '@powersync/react';
import { Stack } from 'expo-router';
import React, { useMemo } from 'react';
import { useSystem } from '../library/powersync/system';
import { PowerSyncContext } from '@powersync/react-native';

/**
* This App uses a nested navigation stack.
*
Expand Down
66 changes: 29 additions & 37 deletions demos/react-native-supabase-todolist/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,6 @@ PODS:
- hermes-engine (0.74.5):
- hermes-engine/Pre-built (= 0.74.5)
- hermes-engine/Pre-built (0.74.5)
- op-sqlite (8.0.3):
- React
- React-callinvoker
- React-Core
- powersync-op-sqlite (0.1.0):
- DoubleConversion
- glog
- hermes-engine
- powersync-sqlite-core (~> 0.2.1)
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React
- React-callinvoker
- React-Codegen
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- powersync-sqlite-core (0.2.1)
- RCT-Folly (2024.01.01.00):
- boost
Expand Down Expand Up @@ -1033,6 +1005,30 @@ PODS:
- React-debug
- react-native-encrypted-storage (4.0.3):
- React-Core
- react-native-quick-sqlite (1.4.0):
- DoubleConversion
- glog
- hermes-engine
- powersync-sqlite-core (~> 0.2.1)
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React
- React-callinvoker
- React-Codegen
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-safe-area-context (4.10.5):
- React-Core
- React-nativeconfig (0.74.5)
Expand Down Expand Up @@ -1422,8 +1418,6 @@ DEPENDENCIES:
- fmt (from `../node_modules/react-native/third-party-podspecs/fmt.podspec`)
- glog (from `../node_modules/react-native/third-party-podspecs/glog.podspec`)
- hermes-engine (from `../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec`)
- "op-sqlite (from `../../../node_modules/@op-engineering/op-sqlite`)"
- "powersync-op-sqlite (from `../node_modules/@powersync/op-sqlite`)"
- RCT-Folly (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`)
- RCT-Folly/Fabric (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`)
- RCTDeprecation (from `../node_modules/react-native/ReactApple/Libraries/RCTFoundation/RCTDeprecation`)
Expand Down Expand Up @@ -1451,6 +1445,7 @@ DEPENDENCIES:
- React-logger (from `../node_modules/react-native/ReactCommon/logger`)
- React-Mapbuffer (from `../node_modules/react-native/ReactCommon`)
- react-native-encrypted-storage (from `../../../node_modules/react-native-encrypted-storage`)
- "react-native-quick-sqlite (from `../node_modules/@journeyapps/react-native-quick-sqlite`)"
- react-native-safe-area-context (from `../../../node_modules/react-native-safe-area-context`)
- React-nativeconfig (from `../node_modules/react-native/ReactCommon`)
- React-NativeModulesApple (from `../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios`)
Expand Down Expand Up @@ -1526,10 +1521,6 @@ EXTERNAL SOURCES:
hermes-engine:
:podspec: "../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec"
:tag: hermes-2024-06-28-RNv0.74.3-7bda0c267e76d11b68a585f84cfdd65000babf85
op-sqlite:
:path: "../../../node_modules/@op-engineering/op-sqlite"
powersync-op-sqlite:
:path: "../node_modules/@powersync/op-sqlite"
RCT-Folly:
:podspec: "../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec"
RCTDeprecation:
Expand Down Expand Up @@ -1580,6 +1571,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon"
react-native-encrypted-storage:
:path: "../../../node_modules/react-native-encrypted-storage"
react-native-quick-sqlite:
:path: "../node_modules/@journeyapps/react-native-quick-sqlite"
react-native-safe-area-context:
:path: "../../../node_modules/react-native-safe-area-context"
React-nativeconfig:
Expand Down Expand Up @@ -1660,8 +1653,6 @@ SPEC CHECKSUMS:
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: fdfdfe5479092de0c4bdbebedd9056951f092c4f
hermes-engine: 8c1577f3fdb849cbe7729c2e7b5abc4b845e88f8
op-sqlite: 9dbbc06c3782df3c85ab36267efbb90a1ce7215a
powersync-op-sqlite: 742a34c810a4eb946798d24f36982395ac0e30f3
powersync-sqlite-core: 38ead13d8b21920cfbc79e9b3415b833574a506d
RCT-Folly: 02617c592a293bd6d418e0a88ff4ee1f88329b47
RCTDeprecation: 3afceddffa65aee666dafd6f0116f1d975db1584
Expand All @@ -1688,6 +1679,7 @@ SPEC CHECKSUMS:
React-logger: 257858bd55f3a4e1bc0cf07ddc8fb9faba6f8c7c
React-Mapbuffer: 6c1cacdbf40b531f549eba249e531a7d0bfd8e7f
react-native-encrypted-storage: db300a3f2f0aba1e818417c1c0a6be549038deb7
react-native-quick-sqlite: d95fd968dc155dbee3b0a3d16227bb736e3b3de3
react-native-safe-area-context: a240ad4b683349e48b1d51fed1611138d1bdad97
React-nativeconfig: ba9a2e54e2f0882cf7882698825052793ed4c851
React-NativeModulesApple: 8d11ff8955181540585c944cf48e9e7236952697
Expand Down Expand Up @@ -1718,7 +1710,7 @@ SPEC CHECKSUMS:
RNScreens: b32a9ff15bea7fcdbe5dff6477bc503f792b1208
RNVectorIcons: 845eda5c7819bd29699cafd0fc98c9d4afe28c96
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
Yoga: 950bbfd7e6f04790fdb51149ed51df41f329fcc8
Yoga: 2246eea72aaf1b816a68a35e6e4b74563653ae09
ZXingObjC: 8898711ab495761b2dbbdec76d90164a6d7e14c5

PODFILE CHECKSUM: ad989b4e43152979093488e5c8b7457e401bf191
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AttachmentTable } from '@powersync/attachments';
import { Column, ColumnType, Index, IndexedColumn, Schema, Table } from '@powersync/react-native';
import { Column, ColumnType, Index, IndexedColumn, Schema, Table } from '@powersync/common';

export const TODO_TABLE = 'todos';
export const LIST_TABLE = 'lists';
Expand Down
1 change: 0 additions & 1 deletion demos/react-native-supabase-todolist/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
"compilerOptions": {
"strict": true,
"composite": true,
"moduleResolution": "Bundler"
}
}
4 changes: 3 additions & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"description": "PowerSync - sync Postgres with SQLite in your React Native app for offline-first and real-time data",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./lib/index.d.ts",
"files": [
"lib",
Expand Down Expand Up @@ -41,7 +42,8 @@
},
"dependencies": {
"@powersync/common": "workspace:*",
"@powersync/react": "workspace:*"
"@powersync/react": "workspace:*",
"@rollup/plugin-dynamic-import-vars": "^2.1.3"
},
"devDependencies": {
"@craftzdog/react-native-buffer": "^6.0.5",
Expand Down
23 changes: 8 additions & 15 deletions packages/react-native/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import inject from '@rollup/plugin-inject';
import json from '@rollup/plugin-json';
import nodeResolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import path from 'path';
import { fileURLToPath } from 'url';
import terser from '@rollup/plugin-terser';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand All @@ -32,27 +29,23 @@ export default (commandLineArgs) => {
preventAssignment: true
}),
json(),
nodeResolve({ preferBuiltins: false }),
// nodeResolve({ preferBuiltins: false }),
commonjs({
ignore: (id) => {
if (id === '@journeyapps/react-native-quick-sqlite') {
return true;
}
return false;
},

}),
inject({
Buffer: ['@craftzdog/react-native-buffer', 'Buffer'],
ReadableStream: ['web-streams-polyfill/ponyfill', 'ReadableStream'],
TextEncoder: ['text-encoding', 'TextEncoder'],
TextDecoder: ['text-encoding', 'TextDecoder'],
// injecting our crypto implementation
crypto: path.resolve('./vendor/crypto.js')
crypto: path.resolve('./vendor/crypto.js'),
exclude: ['@journeyapps/react-native-quick-sqlite']
}),
alias({
entries: [{ find: 'bson', replacement: path.resolve(__dirname, '../../node_modules/bson/lib/bson.rn.cjs') }]
}),
terser()
// alias({
// entries: [{ find: 'bson', replacement: path.resolve(__dirname, '../../node_modules/bson/lib/bson.rn.cjs') }]
// }),
// terser()
],
external: [
'@journeyapps/react-native-quick-sqlite',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import type { QuickSQLiteConnection } from '@journeyapps/react-native-quick-sqlite';
import {
BaseObserver,
DBAdapter,
DBAdapterListener,
DBGetUtils,
DBLockOptions,
LockContext as PowerSyncLockContext,
Transaction as PowerSyncTransaction,
DBLockOptions,
DBGetUtils,
QueryResult
} from '@powersync/common';
import type { QuickSQLiteConnection } from '@journeyapps/react-native-quick-sqlite';

export type DeferedRNQSOptions = {
dbFilename: string;
location?: string;
};

/**
* Adapter for React Native Quick SQLite
*/
export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DBAdapter {
export class DeferedRNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DBAdapter {
getAll: <T>(sql: string, parameters?: any[]) => Promise<T[]>;
getOptional: <T>(sql: string, parameters?: any[]) => Promise<T | null>;
get: <T>(sql: string, parameters?: any[]) => Promise<T>;

constructor(
protected baseDB: QuickSQLiteConnection,
public name: string
) {
protected initialized: Promise<void>;
protected baseDB: QuickSQLiteConnection | null;

constructor(protected options: DeferedRNQSOptions) {
super();
// link table update commands
baseDB.registerTablesChangedHook((update) => {
this.iterateListeners((cb) => cb.tablesUpdated?.(update));
});

const topLevelUtils = this.generateDBHelpers({
// Arrow function binds `this` for use in readOnlyExecute
Expand All @@ -36,40 +37,73 @@ export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DB
this.getAll = topLevelUtils.getAll;
this.getOptional = topLevelUtils.getOptional;
this.get = topLevelUtils.get;
this.baseDB = null;
this.initialized = this.init();
}

get name() {
return this.options.dbFilename;
}

protected get db() {
if (!this.baseDB) {
throw new Error(`Initialization is not yet completed`);
}
return this.baseDB;
}

protected async init() {
// TODO try catch
const { open } = await import('@journeyapps/react-native-quick-sqlite');
const db = open(this.options.dbFilename, {
location: this.options.location
});

// link table update commands
db.registerTablesChangedHook((update) => {
this.iterateListeners((cb) => cb.tablesUpdated?.(update));
});
this.baseDB = db;
}

close() {
return this.baseDB.close();
this.initialized.then(()=> this.baseDB?.close())
}

readLock<T>(fn: (tx: PowerSyncLockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {
return this.baseDB.readLock((dbTx) => fn(this.generateDBHelpers(dbTx)), options);
async readLock<T>(fn: (tx: PowerSyncLockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {
await this.initialized;
return this.db.readLock((dbTx) => fn(this.generateDBHelpers(dbTx)), options);
}

readTransaction<T>(fn: (tx: PowerSyncTransaction) => Promise<T>, options?: DBLockOptions): Promise<T> {
return this.baseDB.readTransaction((dbTx) => fn(this.generateDBHelpers(dbTx)), options);
async readTransaction<T>(fn: (tx: PowerSyncTransaction) => Promise<T>, options?: DBLockOptions): Promise<T> {
await this.initialized;
return this.db.readTransaction((dbTx) => fn(this.generateDBHelpers(dbTx)), options);
}

writeLock<T>(fn: (tx: PowerSyncLockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {
return this.baseDB.writeLock((dbTx) => fn(this.generateDBHelpers(dbTx)), options);
async writeLock<T>(fn: (tx: PowerSyncLockContext) => Promise<T>, options?: DBLockOptions): Promise<T> {
await this.initialized;
return this.db.writeLock((dbTx) => fn(this.generateDBHelpers(dbTx)), options);
}

writeTransaction<T>(fn: (tx: PowerSyncTransaction) => Promise<T>, options?: DBLockOptions): Promise<T> {
return this.baseDB.writeTransaction((dbTx) => fn(this.generateDBHelpers(dbTx)), options);
async writeTransaction<T>(fn: (tx: PowerSyncTransaction) => Promise<T>, options?: DBLockOptions): Promise<T> {
await this.initialized;
return this.db.writeTransaction((dbTx) => fn(this.generateDBHelpers(dbTx)), options);
}

execute(query: string, params?: any[]) {
return this.baseDB.execute(query, params);
async execute(query: string, params?: any[]) {
await this.initialized;
return this.db.execute(query, params);
}

async executeBatch(query: string, params: any[][] = []): Promise<QueryResult> {
await this.initialized;
const commands: any[] = [];

for (let i = 0; i < params.length; i++) {
commands.push([query, params[i]]);
}

const result = await this.baseDB.executeBatch(commands);
const result = await this.db.executeBatch(commands);
return {
rowsAffected: result.rowsAffected ? result.rowsAffected : 0
};
Expand All @@ -81,8 +115,9 @@ export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DB
* the hood. Helper methods such as `get`, `getAll` and `getOptional` are read only,
* and should use this method.
*/
private readOnlyExecute(sql: string, params?: any[]) {
return this.baseDB.readLock((ctx) => ctx.execute(sql, params));
private async readOnlyExecute(sql: string, params?: any[]) {
await this.initialized;
return this.db.readLock((ctx) => ctx.execute(sql, params));
}

/**
Expand Down
Loading

0 comments on commit 25e41b5

Please sign in to comment.