You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -58,13 +60,14 @@ export class AttachmentQueue extends AbstractAttachmentQueue {
58
60
```
59
61
60
62
2. Implement `attachmentIds`, an `AsyncIterator` method to return an array of `string` values of IDs that relate to attachments in your app. We recommend using `PowerSync`'s `watch` query to return the all IDs of attachments in your app.
61
-
In this example, we query all photos that have been captured as part of an inspection and map them to an array of `string` values.
62
63
63
-
```typescript
64
+
In this example, we query all photos that have been captured as part of an inspection and map these to an array of `string` values.
`SELECT photo_id as id FROM checklists WHERE photo_id IS NOT NULL`,
70
73
[]
@@ -76,16 +79,15 @@ export class AttachmentQueue extends AbstractAttachmentQueue {
76
79
```
77
80
78
81
3. Implement `newAttachmentRecord` to return an object that represents the attachment record in your app.
79
-
In this example we always work with `JPEG` images, but you can use any media type that is supported by your app and storage solution.
80
-
Note in this example we are setting the state to `QUEUED_UPLOAD` when creating a new photo record which assumes that the photo data is already on the device.
82
+
83
+
In this example we always work with `JPEG` images, but you can use any media type that is supported by your app and storage solution. Note: we are set the state to `QUEUED_UPLOAD` when creating a new photo record which assumes that the photo data is already on the device.
@@ -131,9 +133,10 @@ The default columns in the `AttachmentTable` are:
131
133
5. To instantiate an `AttachmentQueue`, one needs to provide an instance of `AbstractPowerSyncDatabase` from PowerSync and an instance of `StorageAdapter`.
132
134
See the `StorageAdapter` interface definition [here](./src/StorageAdapter.ts).
133
135
134
-
6. Finally, create a new `AttachmentQueue` and call `init()` to start syncing attachments. Our example, uses a `StorageAdapter` that integrates with Supabase Storage
135
136
136
-
```typescript
137
+
6. Instantiate a new `AttachmentQueue` and call `init()` to start syncing attachments. Our example, uses a `StorageAdapter` that integrates with Supabase Storage.
138
+
139
+
```javascript
137
140
this.storage=this.supabaseConnector.storage;
138
141
this.powersync=factory.getInstance();
139
142
@@ -142,14 +145,37 @@ this.attachmentQueue = new AttachmentQueue({
142
145
storage:this.storage
143
146
});
144
147
148
+
// Initialize and connect PowerSync ...
149
+
// Then initialize the attachment queue
145
150
awaitthis.attachmentQueue.init();
146
151
```
147
152
153
+
7. Finally, to create an attachment and add it to the queue, call `saveToQueue()`.
154
+
155
+
In our example we added a `savePhoto()` method to our `AttachmentQueue` class, that does this:
@@ -162,7 +188,7 @@ The `AttachmentQueue` class manages attachments in your app by tracking their st
162
188
## Initial sync
163
189
164
190
Upon initializing the `AttachmentQueue`, an initial sync of attachments will take place if the `performInitialSync` is set to true.
165
-
Any attachment record with `id` in first set of IDs retrieved from the watch query will be marked as `QUEUED_SYNC`, and these records will be rechecked to see if they need to be uploaded or downloaded.
191
+
Any `AttachmentRecord` with `id` in first set of IDs retrieved from the watch query will be marked as `QUEUED_SYNC`, and these records will be rechecked to see if they need to be uploaded or downloaded.
166
192
167
193
## Syncing attachments
168
194
@@ -179,24 +205,25 @@ By default, this is every 30 seconds, but can be configured by setting `syncInte
179
205
180
206
### Downloading
181
207
182
-
- An `AttachmentRecord` is created or updated with QUEUED_DOWNLOAD state.
208
+
- An `AttachmentRecord` is created or updated with `QUEUED_DOWNLOAD` state.
183
209
- The watch query adds the `id` into a queue of IDs to download and triggers the download process
184
210
- This checks whether the photo is already on the device and if so, skips downloading.
185
211
- If the photo is not on the device, it is downloaded from cloud storage.
186
212
- Writes file to the user's local storage.
187
-
- If this is successful, update the AttachmentRecord state to `SYNCED`.
213
+
- If this is successful, update the `AttachmentRecord` state to `SYNCED`.
188
214
- If any of these fail, the download is retried in the next sync trigger.
189
215
190
-
### Expire Cache
191
-
192
-
When PowerSync removes a records (as a result of coming back online or conflict resolution):
193
-
- Any associated `AttachmentRecord` is orphaned.
194
-
- On the next sync trigger, the `AttachmentQueue` sets all records that are orphaned to `ARCHIVED` state.
195
-
- By default, the `AttachmentQueue` only keeps the last `100` attachment records and then expires the rest. (This can be configured by setting `cacheLimit` in the `AttachmentQueue` constructor options).
196
-
197
216
### Deleting attachments
198
217
199
-
When a list or to-do item is deleted by a user action or cache expiration:
218
+
When an attachment is deleted by a user action or cache expiration:
200
219
- Related `AttachmentRecord` is removed from attachments table.
201
220
- Local file (if exists) is deleted.
202
-
- File on cloud storage is deleted.
221
+
- File on cloud storage is deleted.
222
+
223
+
### Expire Cache
224
+
225
+
When PowerSync removes a record, as a result of coming back online or conflict resolution for instance:
226
+
- Any associated `AttachmentRecord` is orphaned.
227
+
- On the next sync trigger, the `AttachmentQueue` sets all records that are orphaned to `ARCHIVED` state.
228
+
- By default, the `AttachmentQueue` only keeps the last `100` attachment records and then expires the rest.
229
+
- This can be configured by setting `cacheLimit` in the `AttachmentQueue` constructor options.
0 commit comments