Skip to content

rodydavis/drift-pocketbase-sync

Repository files navigation

drift-pocketbase-sync

This is a simple Flutter project that demonstrates how to sync a Pocketbase database with a Flutter SQLite database via drift without using a CRDT.

Getting Started

Run the server

make dev 

Create an admin account

Create an admin account for testing:

  • Username: admin@email.com
  • Password: admin123456

Import the collections

Import the pb_schema.json file into your Pocketbase admin dashboard.

Run the tests

flutter test

Update the generated code

After making any changes to the schema in Pocketbase, you need to regenerate the client code.

The client needs to be in sync with the server to ensure schema alignment.

Regenerate the client schema

To update the generated code, run the following command:

make generate

This will run the generate command, which you can change the admin username and password variables in the Makefile.

Drift does support schema migrations, see the documentation for more information.

FAQ

Why not use a CRDT?

CRDTs are great for eventual consistency, but they can be complex to implement and maintain. This project demonstrates how to sync a Pocketbase database with a Flutter SQLite database without using a CRDT by using a simple sync algorithm since the PocketBase server is a single source of truth and the date time for created/updated are always in order.

How do I generate new IDs on the client?

The code for generating a new ID is take directly from the Pocketbase codebase.

final id = Database.newId();

How do I sync the data?

To sync the data call the sync command:

final Database db = ...;
final PocketBase pb = ...;
final service = SyncService(db, pb);
await service.syncCollections();

This will first check for any new deletes and remove them from the local database.

Then it will check for any new or updated records and add them to the local database.

Finally, it will check the local database for any new or updated records and add them to the Pocketbase database.

How are deletes handled?

Deletes are soft deletes first on both the server and the client. There is a hook to add the deleted record to a special table for later cleanup.

This table is cleaned up after a certain period of time.

Can I use this with a precompiled Pocketbase database?

No, this requires extending the PocketBase server with go. It might be possible with pb_hooks and JS.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published