
Reading Data (SQLite)
App clients always read data from the client-side SQLite database. When the user is online and the app is connected to the PowerSync Service, changes on the source database reflect in real-time in the SQLite database, and Live Queries / Watch Queries allows the app UI to have real-time reactivity too.Client-Side Schema and SQLite Database Structure
When you implement the PowerSync Client SDK in your application, you need to define a client-side schema with tables, columns and indexes that correspond to your Sync Rules. You provide this schema when the PowerSync-managed SQLite database is instantiated. The tables defined in your client-side schema are usable in SQL queries as if they were actual SQLite tables, while in reality they are created as SQLite views based on the schemaless JSON data being synced (see PowerSync Protocol). The PowerSync Client SDK automatically maintains the following tables in the SQLite database:-
ps_data__<table>- This contains the data for each “table”, in JSON format. Since JSON is being used, this table’s schema does not change when columns are added, removed or changed in the Sync Rules and client-side schema. -
ps_data_local__<table>- Same as the previous point, but for local-only tables. -
<table>(VIEW) - These are views on the aboveps_datatables, with each defined column in the client-side schema extracted from the JSON. For example, adescriptiontext column would beCAST(data ->> '$.description' as TEXT). -
ps_untyped- Any synced table that does is not defined in the client-side schema is placed here. If the table is added to the schema at a later point, the data is then migrated tops_data__<table>. -
ps_oplog- This is operation history data as received from the PowerSync Service, grouped per bucket. -
ps_crud- The client-side upload queue (see Writing Data below) -
ps_buckets- A small amount of metadata for each bucket. -
ps_migrations- Table keeping track of Client SDK schema migrations.
ps_data__<table> table, and in ps_oplog.
The copy of the row in ps_oplog may be newer than the one in ps_data__<table>. This is because of the checkpoint system in PowerSync that gives the system its consistency properties. When a full checkpoint has been downloaded, data is copied over from ps_oplog to the individual ps_data__<table> tables.
It is possible for different buckets in Sync Rules to include overlapping data (for example, if multiple buckets query data from the same table). If rows with the same table and ID have been synced via multiple buckets, it may be present multiple times in ps_oplog, but only one will be preserved in the ps_data__<table> table (the one with the highest op_id).
Raw Tables Instead of JSON-Backed SQLite Views: If you run into limitations with the above JSON-based SQLite view system, check out the Raw Tables experimental feature which allows you to define and manage raw SQLite tables to work around some of the limitations of PowerSync’s default JSON-backed SQLite views system. We are actively seeking feedback on the raw tables functionality.
Writing Data (via SQLite Database and Upload Queue)
Any mutations on the SQLite database, namely updates, deletes and inserts, are immediately reflected in the SQLite database, and also also automatically placed into an upload queue by the Client SDK. The upload queue is a blocking FIFO queue. The upload queue is automatically managed by the PowerSync Client SDK. The Client SDK processes the upload queue by invoking anuploadData() function that you define when you integrate the Client SDK. Your uploadData() function implementation should call your backend application API to persist the mutations to the backend source database.
The reason why we designed PowerSync this way is that it allows you to apply your own backend business logic, validations and authorization to any mutations going to your source database.
The PowerSync Client SDK automatically takes care of network failures and retries. If processing the upload queue fails (e.g. because the user is offline), it is automatically retried.
