|
| 1 | +## Sync interface |
| 2 | + |
| 3 | +The core extension implements the state machine and necessary SQL handling to decode and apply |
| 4 | +sync line sent from a PowerSync service instance. |
| 5 | + |
| 6 | +After registering the PowerSync extension, this client is available through the `powersync_control` |
| 7 | +function, which takes two arguments: A command (text), and a payload (text, blob, or null). |
| 8 | + |
| 9 | +The following commands are supported: |
| 10 | + |
| 11 | +1. `start`: Payload is a JSON-encoded object. This requests the client to start a sync iteration, specifying |
| 12 | + parameters. |
| 13 | +2. `stop`: No payload, requests the current sync iteration (if any) to be shut down. |
| 14 | +3. `line_text`: Payload is a serialized JSON object received from the sync service. |
| 15 | +4. `line_binary`: Payload is a BSON-encoded object received from the sync service. |
| 16 | +5. `refreshed_token`: Notify the sync client that the JWT used to authenticate to the PowerSync service has |
| 17 | + changed. |
| 18 | + - The client will emit an instruction to stop the current stream, clients should restart by sending another `start` |
| 19 | + command. |
| 20 | +6. `completed_upload`: Notify the sync implementation that all local changes have been uploaded. |
| 21 | + |
| 22 | +`powersync_control` returns a JSON-encoded array of instructions for the client: |
| 23 | + |
| 24 | +```typescript |
| 25 | +type Instruction = { LogLine: LogLine } |
| 26 | + | { UpdateSyncStatus: UpdateSyncStatus } |
| 27 | + | { EstablishSyncStream: EstablishSyncStream } |
| 28 | + | { FetchCredentials: FetchCredentials } |
| 29 | + // Close a connection previously started after EstablishSyncStream |
| 30 | + | { CloseSyncStream: {} } |
| 31 | + // For the Dart web client, flush the (otherwise non-durable) file system. |
| 32 | + | { FlushFileSystem: {} } |
| 33 | + // Notify clients that a checkpoint was completed. Clients can clear the |
| 34 | + // download error state in response to this. |
| 35 | + | { DidCompleteSync: {} } |
| 36 | + |
| 37 | +interface LogLine { |
| 38 | + severity: 'DEBUG' | 'INFO' | 'WARNING', |
| 39 | + line: String, |
| 40 | +} |
| 41 | + |
| 42 | +// Instructs client SDKs to open a connection to the sync service. |
| 43 | +interface EstablishSyncStream { |
| 44 | + request: any // The JSON-encoded StreamingSyncRequest to send to the sync service |
| 45 | +} |
| 46 | + |
| 47 | +// Instructs SDKS to update the downloading state of their SyncStatus. |
| 48 | +interface UpdateSyncStatus { |
| 49 | + connected: boolean, |
| 50 | + connecting: boolean, |
| 51 | + priority_status: [], |
| 52 | + downloading: null | DownloadProgress, |
| 53 | +} |
| 54 | + |
| 55 | +// Instructs SDKs to refresh credentials from the backend connector. |
| 56 | +// They don't necessary have to close the connection, a CloseSyncStream instruction |
| 57 | +// will be sent when the token has already expired. |
| 58 | +interface FetchCredentials { |
| 59 | + // Set as an option in case fetching and prefetching should be handled differently. |
| 60 | + did_expire: boolean |
| 61 | +} |
| 62 | + |
| 63 | +interface SyncPriorityStatus { |
| 64 | + priority: int, |
| 65 | + last_synced_at: null | int, |
| 66 | + has_synced: null | boolean, |
| 67 | +} |
| 68 | + |
| 69 | +interface DownloadProgress { |
| 70 | + buckets: Record<string, BucketProgress> |
| 71 | +} |
| 72 | + |
| 73 | +interface BucketProgress { |
| 74 | + priority: int, |
| 75 | + at_last: int, |
| 76 | + since_last: int, |
| 77 | + target_count: int |
| 78 | +} |
| 79 | +``` |
0 commit comments