@powersync/react-native
Version:
PowerSync React Native SDK. Sync Postgres, MongoDB or MySQL with SQLite in your React Native app
49 lines (48 loc) • 1.85 kB
JavaScript
import { AbstractPowerSyncDatabase } from '@powersync/common';
import { ReactNativeRemote } from '../sync/stream/ReactNativeRemote';
import { ReactNativeStreamingSyncImplementation } from '../sync/stream/ReactNativeStreamingSyncImplementation';
import { ReactNativeBucketStorageAdapter } from './../sync/bucket/ReactNativeBucketStorageAdapter';
import { ReactNativeQuickSqliteOpenFactory } from './adapters/react-native-quick-sqlite/ReactNativeQuickSQLiteOpenFactory';
/**
* A PowerSync database which provides SQLite functionality
* which is automatically synced.
*
* @example
* ```typescript
* export const db = new PowerSyncDatabase({
* schema: AppSchema,
* database: {
* dbFilename: 'example.db'
* }
* });
* ```
*/
export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
async _initialize() { }
/**
* Opens a DBAdapter using React Native Quick SQLite as the
* default SQLite open factory.
*/
openDBAdapter(options) {
const defaultFactory = new ReactNativeQuickSqliteOpenFactory(options.database);
return defaultFactory.openDB();
}
generateBucketStorageAdapter() {
return new ReactNativeBucketStorageAdapter(this.database, this.logger);
}
generateSyncStreamImplementation(connector, options) {
const remote = new ReactNativeRemote(connector, this.logger);
return new ReactNativeStreamingSyncImplementation({
adapter: this.bucketStorageAdapter,
remote,
uploadCrud: async () => {
await this.waitForReady();
await connector.uploadData(this);
},
retryDelayMs: options.retryDelayMs,
crudUploadThrottleMs: options.crudUploadThrottleMs,
identifier: this.database.name,
logger: this.logger
});
}
}