- The app posts each scan to the API and queues it only when the call fails. Now there are two code paths, and the offline path is harder to test.
- A background job retries a scan after a timeout, and the server records the scan event twice.
- A large POD photo stuck on a weak uplink blocks the scan events queued behind it.
- Dispatch cancels a stop while the driver is in a dead zone, and the driver’s delivery collides with the cancellation.
Although PowerSync works with many front-end stacks and backend databases, the example code below uses React Native and Postgres. In our Migration Guides section, we also have a complete delivery driver example app that uses SQL Server and .NET MAUI. See Migrating a SQL Server and .NET MAUI App to Offline-First.
1. Put a Database on the Driver’s Device
For robust offline behavior, the network must not be on the critical path for a driver accessing their route or scanning packages. PowerSync gives the app on the driver’s device (a phone, tablet, or rugged handheld) a client-side SQLite database. Whether online or offline, the app reads and writes only to that database. The PowerSync client SDK downloads changes via the PowerSync Service based on your Sync Streams, and uploads writes via your backend API. The SDK automatically handles retries of failed uploads. When wiring up the PowerSync client SDK, you define a schema for the local SQLite database that matches your Sync Streams. Here is what a basic schema for a delivery driver app typically looks like: A route is the driver’sstops rows for the day, flowing down from dispatch. Proof of delivery is one deliveries row per attempt, with an outcome, plus photo_id and signature_id pointing to attachments.
powersync/AppSchema.ts
2. Record Every Scan as an Append-Only Scan Event
Edits to scan rows break audit trails and can lead to data inconsistencies. A retried upload must not overwrite the original scan event. To address this, every scan (loaded at the warehouse, out for delivery, delivered, failed) is a newscan_events row. Nothing is updated or deleted, so a package’s history is the list of its scan events, and a retried upload can’t change a scan already recorded.
With PowerSync, scan_events is a standard synced table. Your app writes each scan to it directly. The PowerSync client SDK manages uploading those writes via your backend API. You configure a Sync Stream that sends each driver’s own scan events back to the device (section 7), so the full history stays available after upload.
A failed delivery attempt is a deliveries row with outcome failed, plus a scan event with kind = 'failed' and a failure_reason. A later attempt is a new row.
Record lat and lng on each scan event, since location is part of the evidence. Keep two clocks: scanned_at is what the device said, and received_at is set by your backend on insert (null on the device until the row syncs back). The device’s clock can be wrong, so your backend never overwrites scanned_at.
Write the scan event and the delivery in one writeTransaction(), so they upload together:
deliveries/recordDelivery.ts
3. Make Every Upload Idempotent by Generating IDs on the Device
Due to the flaky nature of networks, when retrying write uploads, the server can sometimes apply the write, but the client does not receive a response. Therefore, write operations should be made idempotent, which requires the use of an Idempotency Key, PowerSync provides theuuid() SQLite function (see Client ID), so you can generate a unique ID for each row on the device, without relying on the network. In the above example, we use uuid() for the deliveries and scan_events rows. Your backend stores those IDs as the primary keys for the rows.
In your backend implementation for handling writes, inserts should be processed using ON CONFLICT (id) DO NOTHING to make them idempotent. If a scan is uploaded multiple times, the repeats have no effect.
4. Scan Anytime and Upload When Online
Scans recorded while the device is offline must reach your backend in order, even across app restarts. DIY sync implementations need an outbox table plus a worker that drains and retries it, and only one instance of that worker may ever run on the device. When PowerSync, you don’t need to build an outbox. PowerSync records every write to a synced table in its upload queue. You define anuploadData() function that determines how entries in that upload queue are uploaded to your backend. The PowerSync SDK calls that uploadData() function after local writes, on reconnection, and automatically retries after failures – so queued scans upload as soon as the network allows. Entries leave the queue only once they have been successfully processed.
powersync/Connector.ts
UPDATE or DELETE on scan_events, since they are considered append-only.
5. Upload POD Photos and Signatures as Attachments
Keep photos and signatures out of your database rows. Embedding image data as base64 or binary blobs bloats the local database, slows queries and sync, and puts multi-megabyte uploads in the same queue as the scan events and status changes. Store each file in object storage and keep only its ID in the row. PowerSync’s built-in Attachment Helpers can handle POD photos and signatures. Thedeliveries row holds the attachment IDs.
For more details on how this works, refer to Attachments / Files.
pod/attachPhoto.ts
AttachmentQueue an errorHandler whose onUploadError returns true, so failed uploads retry. Returning false archives the file, which loses the POD evidence.
Attachments upload on their own queue, not with the row, so your backend must accept a photo_id before the file arrives. The upload to storage must also be safe to repeat, since a killed app can re-upload a photo that already finished.
6. Write Down Your Conflict Rules
When a device comes back online, its local state and pending uploads may conflict with what was written to the backend while it was offline. Your backend needs conflict resolution rules to handle this. Append-only rows with client IDs can’t conflict, so this design has minimal conflict rules:- Scan events: never conflict and are never rejected. Your backend records them even against a cancelled stop.
- Stops: dispatch-owned. Only dispatch creates and cancels them; drivers only record outcomes. Your backend sets
stops.statusfrom the delivery it receives. Until then, your app can show a stop as done if it has a localdeliveriesrow. - A delivery on a stop which dispatch already cancelled: your backend decides. For example, you can record the attempted delivery in a dead-letter queue and propagate an error message back to the driver via a synced table.
7. Load the Route at the Warehouse and Sync Only the Driver’s Stops
The app must have the driver’s scheduled stops available even while offline. Each device should download only the stops and deliveries for its own driver. PowerSync provides Sync Streams as a declarative way to define what each device receives and stores locally. The driver isauth.user_id() from the JWT, and the my_route stream below filters on it (see Using Parameters).
The my_route stream sends the driver’s stops and deliveries, plus their own scan_events. Without the scan_events query, scans disappear from the device after upload.
For the purposes of the example below, the is_current_route flag is set by the backend to indicate the current active route for the driver.
my_route downloads the scheduled stops before the driver is dispatched, while their device is connected. Before leaving the warehouse or going offline, the driver should be able to check whether their device is synced. This can be implemented in your UI using SyncStatus provided by the PowerSync client SDK.
8. Show the Driver What’s Still Waiting to Upload
It can be helpful to show the driver which scans and POD are safe on the device but not yet uploaded. PowerSync’sSyncStatus gives connected, uploading, and lastSyncedAt (see Production Readiness Guide). The number of pending uploads (i.e. scans and deliveries) in the upload queue can also be displayed to the driver: SELECT COUNT(*) FROM ps_crud. Pending photos and signatures can also be shown: they are rows in the local attachments table with state QUEUED_UPLOAD.
What PowerSync Removes from a DIY Design
You still need to implement:
- Your idempotent write endpoint, including rejection handling
- Wiring up the Attachment Helper
- Driver auth and JWTs (your backend sets
driver_idfrom the JWT) - Dispatch functionality that maintains the driver’s scheduled stops
Next Steps
Setup Guide
Connect a source database and client app.
Attachments / Files
Set up the attachment queue.