Skip to content

Chore/update readme #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions demos/supabase-anonymous-auth/lib/app_config_template.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copy this template: `cp lib/app_config_template.dart lib/app_config.dart`
// Edit lib/app_config.dart and enter your Supabase and PowerSync project details.
class AppConfig {
static const String supabaseUrl = 'https://jrimaqxlgrpjipgssldo.supabase.co';
static const String supabaseAnonKey =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImpyaW1hcXhsZ3JwamlwZ3NzbGRvIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTE1ODY5MTAsImV4cCI6MjAwNzE2MjkxMH0.K-Qb-YFz0oVpLshUEezGU-Do-sX08zKqBzVccDoZp_Y';
static const String supabaseUrl = 'https://foo.supabase.co';
static const String supabaseAnonKey = 'foo';
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copy this template: `cp lib/app_config_template.dart lib/app_config.dart`
// Edit lib/app_config.dart and enter your Supabase and PowerSync project details.
class AppConfig {
static const String supabaseUrl = 'https://jrimaqxlgrpjipgssldo.supabase.co';
static const String supabaseAnonKey =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImpyaW1hcXhsZ3JwamlwZ3NzbGRvIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTE1ODY5MTAsImV4cCI6MjAwNzE2MjkxMH0.K-Qb-YFz0oVpLshUEezGU-Do-sX08zKqBzVccDoZp_Y';
static const String supabaseUrl = 'https://foo.supabase.co';
static const String supabaseAnonKey = 'foo';
}
9 changes: 5 additions & 4 deletions demos/supabase-simple-chat/lib/app_config_template.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copy this template: `cp lib/app_config_template.dart lib/app_config.dart`
// Edit lib/app_config.dart and enter your Supabase and PowerSync project details.
class AppConfig {
static const String supabaseUrl = 'https://aaaaaaaaaaaa.supabase.co';
static const String supabaseAnonKey = 'eyeyeyeyeyeye';
static const String powersyncUrl =
'https://65130c9db6679a3682ba380a.powersync.journeyapps.com';
static const String supabaseUrl = 'https://foo.supabase.co';
static const String supabaseAnonKey = 'foo';
static const String powersyncUrl = 'https://foo.powersync.journeyapps.com';
}
12 changes: 6 additions & 6 deletions demos/supabase-todolist/lib/app_config_template.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copy this template: `cp lib/app_config_template.dart lib/app_config.dart`
// Edit lib/app_config.dart and enter your Supabase and PowerSync project details.
class AppConfig {
static const String supabaseUrl = 'https://jrimaqxlgrpjipgssldo.supabase.co';
static const String supabaseAnonKey =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImpyaW1hcXhsZ3JwamlwZ3NzbGRvIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTE1ODY5MTAsImV4cCI6MjAwNzE2MjkxMH0.K-Qb-YFz0oVpLshUEezGU-Do-sX08zKqBzVccDoZp_Y';
static const String powersyncUrl =
'https://64d392172a3c226cddbd5070.powersync.journeyapps.com';
static const String supabaseUrl = 'https://foo.supabase.co';
static const String supabaseAnonKey = 'foo';
static const String powersyncUrl = 'https://foo.powersync.journeyapps.com';
static const String supabaseStorageBucket =
''; // Optional if you want to test out attachments
''; // Optional. Only required when syncing attachments and using Supabase Storage. See packages/powersync_attachments_helper.
}
158 changes: 23 additions & 135 deletions packages/powersync/README.md
Original file line number Diff line number Diff line change
@@ -1,151 +1,39 @@
# PowerSync SDK for Dart/Flutter

[PowerSync](https://powersync.co) is a service and set of SDKs that keeps PostgreSQL databases in sync with on-device SQLite databases.

## SDK Features

* Real-time streaming of changes.
* Direct access to the SQLite database - use SQL on the client and server.
* Operations are asynchronous by default - does not block the UI.
* Supports one write and many reads concurrently.
* No need for client-side database migrations - these are handled automatically.
* Subscribe to queries for live updates.

## Examples

For complete app examples, see our [example app gallery](https://docs.powersync.com/resources/demo-apps-example-projects#flutter)

For examples of some common patterns, see our [example snippets](./example/README.md)

## Getting started
<p align="center">
<a href="https://www.powersync.com" target="_blank"><img src="https://github.com/powersync-ja/.github/assets/19345049/602bafa0-41ce-4cee-a432-56848c278722"/></a>
</p>

You'll need to create a PowerSync account and set up a PowerSync instance. You can do this at [https://www.powersync.com/](https://www.powersync.com/).

### Install the package
# PowerSync SDK for Dart/Flutter

`flutter pub add powersync`
[PowerSync](https://powersync.com) is a service and set of SDKs that keeps Postgres databases in sync with on-device SQLite databases.

### Implement a backend connector and initialize the PowerSync database
This package (`powersync`) is the PowerSync SDK for Dart/Flutter clients.

```dart
import 'package:powersync/powersync.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart';
See a summary of features [here](https://docs.powersync.com/client-sdk-references/flutter).

// Define the schema for the local SQLite database.
// You can automatically generate this schema based on your sync rules:
// In the PowerSync dashboard, right-click on your PowerSync instance and then click "Generate client-side schema"
const schema = Schema([
Table('customers', [Column.text('name'), Column.text('email')])
]);
# Installation

late PowerSyncDatabase db;
```bash
flutter pub add powersync
```

// You must implement a backend connector to define how PowerSync communicates with your backend.
class MyBackendConnector extends PowerSyncBackendConnector {
PowerSyncDatabase db;
# Getting Started

MyBackendConnector(this.db);
@override
Future<PowerSyncCredentials?> fetchCredentials() async {
// implement fetchCredentials to obtain a JWT from your authentication service
// see https://docs.powersync.com/usage/installation/authentication-setup
}
@override
Future<void> uploadData(PowerSyncDatabase database) async {
// Implement uploadData to send local changes to your backend service
// You can omit this method if you only want to sync data from the server to the client
// see https://docs.powersync.com/usage/installation/upload-data
}
}
Our [full SDK reference](https://docs.powersync.com/client-sdk-references/flutter) contains everything you need to know to get started implementing PowerSync in your project.

openDatabase() async {
final dir = await getApplicationSupportDirectory();
final path = join(dir.path, 'powersync-dart.db');
# Changelog

// Setup the database.
db = PowerSyncDatabase(schema: schema, path: path);
await db.initialize();
A changelog for this SDK is available [here](https://releases.powersync.com/announcements/flutter-client-sdk).

// Connect to backend
db.connect(connector: MyBackendConnector(db));
}
```
# API Reference

### Subscribe to changes in data

```dart
StreamBuilder(
// you can watch any SQL query
stream: return db.watch('SELECT * FROM customers order by id asc'),
builder: (context, snapshot) {
if (snapshot.hasData) {
// TODO: implement your own UI here based on the result set
return ...;
} else {
return const Center(child: CircularProgressIndicator());
}
},
)
```
The full API reference for this SDK can be found [here](https://pub.dev/documentation/powersync/latest/powersync/powersync-library.html).

### Insert, update, and delete data in the SQLite database as you would normally

```dart
FloatingActionButton(
onPressed: () async {
await db.execute(
'INSERT INTO customers(id, name, email) VALUES(uuid(), ?, ?)',
['Fred', 'fred@example.org'],
);
},
tooltip: '+',
child: const Icon(Icons.add),
);
```
# Examples

### Send changes in local data to your backend service

```dart
// Implement the uploadData method in your backend connector
@override
Future<void> uploadData(PowerSyncDatabase database) async {
final batch = await database.getCrudBatch();
if (batch == null) return;
for (var op in batch.crud) {
switch (op.op) {
case UpdateType.put:
// Send the data to your backend service
// replace `_myApi` with your own API client or service
await _myApi.put(op.table, op.opData!);
break;
default:
// TODO: implement the other operations (patch, delete)
break;
}
}
await batch.complete();
}
```
For example projects built with PowerSync and Flutter, see our [Demo Apps / Example Projects](https://docs.powersync.com/resources/demo-apps-example-projects#flutter) gallery. Most of these projects can also be found in the [`demos/`](../demos/) directory.

### Logging

You can enable logging to see what's happening under the hood
or to debug connection/authentication/sync issues.

```dart
Logger.root.level = Level.INFO;
Logger.root.onRecord.listen((record) {
if (kDebugMode) {
print('[${record.loggerName}] ${record.level.name}: ${record.time}: ${record.message}');

if (record.error != null) {
print(record.error);
}
if (record.stackTrace != null) {
print(record.stackTrace);
}
}
});
```
# Found a bug or need help?

- Join our [Discord server](https://discord.gg/powersync) where you can browse topics from our community, ask questions, share feedback, or just say hello :)
- Please open a [GitHub issue](https://github.com/powersync-ja/powersync.dart/issues) when you come across a bug.
- Have feedback or an idea? [Submit an idea](https://roadmap.powersync.com/tabs/5-roadmap/submit-idea) via our public roadmap or [schedule a chat](https://calendly.com/powersync/powersync-chat) with someone from our product team.