Engineering
February 27, 2024
April 9, 2024
(updated)

Using ORMs with PowerSync

Ralf Kistner

As much as some developers love to drop into raw SQL for advanced queries, it can be annoying to have to write SQL for simple queries, often because there’s no type-safety. Using an ORM helps address this challenge.

What’s available today?

Note: We use “ORM”  as a shorthand description of the libraries we’re integrating with. The libraries themselves can provide more than ORM functionality or describe themselves differently: Drift is a "relational persistence library", Kysely calls itself a "query builder” and TinyBase is a “reactive data store”.

Our plan is to add ORM support for each of our client SDKs, starting with Flutter and JavaScript.

Flutter

Flutter ORM Support with Drift

We’ve recently released a package that enables using the popular Drift persistence library (ORM) with the PowerSync SDK for Flutter. Our thanks to the author of Drift, Simon Binder, for his help to enable this integration. The Drift integration gives Flutter developers the flexibility to write queries in either Dart or SQL, in the same app and against the same client-side database.

Importantly, the Drift ORM integration supports propagating change notifications from the PowerSync side to Drift, which is necessary for streaming queries.

The use of this package is recommended for Flutter developers who already know Drift, or specifically want the benefits of an ORM for their PowerSync projects. An example of using the drift_sqlite_async package with PowerSync is available here.

Support for Other Flutter ORMs

Other ORMs for Flutter, like Floor, are not currently supported. It is technically possible to open a separate connection to the same database file using Floor but there are two big caveats to that:

Write locks

Every write transaction (or write statement) will lock the database for other writes for the duration of the transaction. While transactions are typically short, if multiple happen to run at the same time they may fail with a SQLITE_BUSY or similar error.

External modifications

Often, ORMs only detect notifications made using the same library. In order to support streaming queries, PowerSync requires the ORM to allow external modifications to trigger the same change notifications, meaning streaming queries are unlikely to work out-of-the-box.

JavaScript

Kysely

On the JavaScript side we’ve completed a wide evaluation of existing ORMs and settled on Kysely. Kysely has a great blend between declarative query building while also providing type-safe imperative APIs. 

We’ve recently shipped a beta release of kysely-driver, which enables using Kysely with PowerSync React Native and web SDKs.

TinyBase

TinyBase is a reactive local data store with a query builder, TinyQL, that provides type-safe APIs. TinyBase added support for PowerSync in their version 4.8 release and now ships with a PowerSync persister built-in. Community member Benedikt Müller is the driving force behind this integration and wrote an article on the benefits of this approach.

Why doesn’t PowerSync have a built-in ORM?

Developers often have strong opinions on using ORMs — they either love it or hate it. With PowerSync our goal is to not force that decision on developers. Instead, we want to allow any approach from raw SQL queries to arbitrary ORM libraries. We started with just raw SQL, and are now focusing on ORM integrations.

We specifically avoid implementing our own ORM since we feel it's better to support popular existing ORMs, which likely do a much better job than we can. It also makes it easier to switch to/from PowerSync if you can keep most of your database code the same.

That said, integrating PowerSync with any ORM unfortunately requires some custom work. However, we do try to do the integration work on a generic level. For example in the case of the Drift ORM for Flutter, the integration is between sqlite_async (which PowerSync uses) and Drift, so you're not locked into PowerSync at all.

I want to use a specific ORM with PowerSync

We’re open to discussing any requirements or ideas around ORMs, feel free to ask us any questions on our Discord server.