Skip to content

Commit

Permalink
Improve dart demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mugikhan committed Nov 11, 2024
1 parent 9ad3065 commit 5888444
Showing 1 changed file with 68 additions and 3 deletions.
71 changes: 68 additions & 3 deletions packages/powersync_core/example/getting_started.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import 'dart:ffi';
import 'dart:io';

import 'package:powersync_core/powersync_core.dart';
import 'package:path/path.dart';
import 'package:powersync_core/sqlite3.dart' as sqlite;
import 'package:powersync_core/sqlite3_common.dart';
import 'package:powersync_core/sqlite_async.dart';
import 'package:powersync_core/sqlite3_open.dart' as sqlite_open;

const schema = Schema([
Table('customers', [Column.text('name'), Column.text('email')])
Expand All @@ -25,13 +30,73 @@ class BackendConnector extends PowerSyncBackendConnector {
}
}

openDatabase() async {
/// Custom factory to load the PowerSync extension.
/// This is required to load the extension from a custom location.
/// The extension is required to sync data with the backend.
/// On macOS and Linux, the default sqlite3 library is overridden to load the extension.
class PowerSyncDartOpenFactory extends PowerSyncOpenFactory {
PowerSyncDartOpenFactory({required super.path, super.sqliteOptions});

@override
CommonDatabase open(SqliteOpenOptions options) {
sqlite_open.open.overrideFor(sqlite_open.OperatingSystem.linux, () {
return DynamicLibrary.open('libsqlite3.so.0');
});
sqlite_open.open.overrideFor(sqlite_open.OperatingSystem.macOS, () {
return DynamicLibrary.open('libsqlite3.dylib');
});
return super.open(options);
}

@override
void enableExtension() {
var powersyncLib = DynamicLibrary.open(getLibraryForPlatform());
sqlite.sqlite3.ensureExtensionLoaded(sqlite.SqliteExtension.inLibrary(
powersyncLib, 'sqlite3_powersync_init'));
}

@override
String getLibraryForPlatform({String? path = "."}) {
switch (Abi.current()) {
case Abi.androidArm:
case Abi.androidArm64:
case Abi.androidX64:
return '$path/libpowersync.so';
case Abi.macosArm64:
case Abi.macosX64:
return '$path/libpowersync.dylib';
case Abi.linuxX64:
case Abi.linuxArm64:
return '$path/libpowersync.so';
case Abi.windowsX64:
return '$path/powersync.dll';
case Abi.androidIA32:
throw PowersyncNotReadyException(
'Unsupported processor architecture. X86 Android emulators are not '
'supported. Please use an x86_64 emulator instead. All physical '
'Android devices are supported including 32bit ARM.',
);
default:
throw PowersyncNotReadyException(
'Unsupported processor architecture "${Abi.current()}". '
'Please open an issue on GitHub to request it.',
);
}
}
}

Future<String> getDatabasePath() async {
const dbFilename = 'powersync-demo.db';
final dir = (Directory.current.uri).toFilePath();
var path = join(dir, dbFilename);
return join(dir, dbFilename);
}

openDatabase() async {
// Setup the database.
db = PowerSyncDatabase(schema: schema, path: path);
final psFactory = PowerSyncDartOpenFactory(path: await getDatabasePath());
db = PowerSyncDatabase.withFactory(psFactory, schema: schema);

// Initialise the database.
await db.initialize();

// Run local statements.
Expand Down

0 comments on commit 5888444

Please sign in to comment.