Your backend application needs to expose an API endpoint to apply write operations to your backend database that are received from the PowerSync Client SDK.
Your backend application receives the write operations based on how you defined your uploadData()
function in the PowerSyncBackendConnector
in your client-side app. See Integrate with your Backend in the Client-Side Setup section for details.
Since you get to define the client-side uploadData()
function as you wish, you have full control over how to structure your backend application API to accept write operations from the client. For example, you can have:
uploadData()
, you can call the respective endpoints as needed.You can also use any API style you want — e.g. REST, GraphQL, gRPC, etc.
It’s important that your API endpoint be blocking/synchronous with underlying writes to the backend database (Postgres, MongoDB or MySQL).
In other words, don’t place writes into something like a queue for processing later — process them immediately. For more details, see the explainer below.
The upload queue on the client stores three types of operations:
Operation | Purpose | Contents | SQLite Statement |
---|---|---|---|
PUT | Create new row | Contains the value for each non-null column | Generated by INSERT statements. |
PATCH | Update existing row | Contains the row id , and value of each changed column. | Generated by UPDATE statements. |
DELETE | Delete existing row | Contains the row id | Generated by DELETE statements. |
The PowerSync Client SDK does not prescribe any specific request/response format for your backend application API that accepts the write operations. You can implement it as you wish.
We do however recommend the following:
5xx
) only when the write operations cannot be applied due to a temporary error (e.g. backend database not available). In this scenario, the PowerSync Client SDK can retry uploading the write operation and it should succeed at a later time.4xx
), since it will block the PowerSync client’s upload queue. Instead, it is best to return a 2xx
response, and if needed, propagate the validation or other error message(s) back to the client, for example by:
2xx
response.For details on approaches, see:
For details on handling write conflicts, see:
See our Example Projects page for examples of custom backend implementations (e.g. Django, Node.js, Rails, etc.) that you can use as a guide for your implementation.
For Postgres developers, using Supabase is an easy alternative to a custom backend. Several of our example/demo apps demonstrate how to use Supabase as the backend. These examples use the PostgREST API exposed by Supabase to upload write operations. Alternatively, Supabase’s Edge Functions can also be used.