Overview
When syncing large amounts of data to connected clients, it can be useful to pre-seed the SQLite database with an initial snapshot of the data. This can help to reduce the initial sync time and improve the user experience. To achieve this, you can run server-side processes using the PowerSync Node.js SDK to pre-seed SQLite files. These SQLite files can then be uploaded to blob storage providers such as AWS S3, Azure Blob Storage, or Google Cloud Storage and downloaded directly by client applications. Client applications can then initialize the pre-seeded SQLite file, effectively bypassing the initial sync process.Demo App
If you’re interested in seeing an end-to-end example, we’ve prepared a demo repo that can be used as a template for your own implementation. This repo covers all of the key concepts and code examples shown in this page.nodejs-react-native-sqlite-seeder
Self-hosted PowerSync instance connected to a PostgreSQL database, using the PowerSync Node.js SDK, React Native SDK and AWS S3 for storing the pre-seeded SQLite files.
Main Concepts
Generate a scoped JWT token
In most cases you’d want to pre-seed the SQLite database with user specific data and not all data from the source database, as you normally would when using PowerSync. For this you would need to generate a JWT tokens that include the necessary properties to satisfy the conditions of the parameter queries in your sync rules. Let’s say we have sync rules that look like this:store_id is part of the JWT payload and is used in a parameter query to filter products by store for a user. Given this we would want to do the following:
- Query the source database, directly from the Node.js application, for all the store ids you’d want a pre-seeded SQLite database for.
- Generate a JWT token for each store and include the
store_idin the payload. - In the Node.js application which implements the PowerSync SDK, return the JWT token in the
fetchCredentials()function.
store_id using the jose library:
Pre-seeding script
Once you’ve got a plan in place for generating the JWT tokens, you can write a simple script to connect to the PowerSync instance and pre-seed the SQLite database. Here’s an example of a script that does this:Some critical points to note:
- You will need to wait for the first sync to complete before deleting the
client_idkey and vacuuming the database. This makes sure all of the data is synced to the database before we proceed. - The
client_idkey is used to identify the client device and is typically set when the client connects to the PowerSync instance. So when pre-seeding the database, we need to delete theclient_idkey to avoid conflicts when the client connects to the PowerSync instance. - It’s important to note that you will need to use the
VACUUM INTOcommand to create a clean, portable SQLite database file. This will help to reduce the size of the database file and provide an optimized version for the client to download. - In this example the upload function is using AWS S3, but you can use any blob storage provider that you prefer.
Scheduling and Cleaning Up
To enhance the process you can consider doing the following:- To keep the pre-seeded SQLite databases fresh, schedule a CRON jobs for periodic regeneration, ensuring that new clients always download the latest snapshot of the initial sync data.
- After each run, perform some environment cleanup to avoid disk bloat. This can be done by deleting the pre-seeded SQLite database files after they have been uploaded to the blob storage provider.
Client Side Usage
When the client application boots, before connecting to the PowerSync instance, check if a SQLite database exists in the application’s permanent storage. If it does, use it, else download a pre-seeded SQLite database from the blob storage provider. Here’s an example of a function that checks if a file exists in the application’s permanent storage:PowerSyncDatabase class with the file path and connect to the PowerSync instance.