|
min. read

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

Following a wide evaluation of JavaScript ORMs we chose Kysely as the first ORM to integrate with PowerSync. This choice was driven by Kysely’s unique combination of declarative query building and type-safe imperative APIs.

Kysely integrates with our React Native and web SDKs through the kysely-driver package. Visit our docs to learn how to get started.

Drizzle

In late 2024 we also released support for Drizzle, which is quickly gaining popularity. Drizzle stands out for offering both relational and a SQL-style queries, giving developers the best of both worlds.

Drizzle queries can be used in our React Native and web SDKs through the drizzle-driver package. See our docs to get started and for common usage examples.

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.

Subscribe to receive updates

Thank you! Your submission has been received.
Oops! Something went wrong while submitting the form. Please try again.