Skip to content

Commit ef52152

Browse files
committed
chore: updates to powersync-attachemnt readme
1 parent 21dce42 commit ef52152

File tree

1 file changed

+65
-38
lines changed

1 file changed

+65
-38
lines changed

packages/powersync-attachments/README.md

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ Note: This package is currently in a beta release.
1212
yarn add @journeyapps/powersync-attachments
1313
```
1414

15-
**pnpm**
15+
**npm**
1616
```bash
17-
pnpm install @journeyapps/powersync-attachments
17+
npm install @journeyapps/powersync-attachments
1818
```
1919

2020
## Usage
2121

2222
The `AttachmentQueue` class is used to manage and sync attachments in your app.
2323

24-
In this example we are capturing photos as part of an inspection workflow.
24+
### Example
2525

26-
Here is the schema for the `checklist` table:
26+
In this example, the user captures photos when checklist item are be completed as part of an Inspection workflow.
2727

28-
```typescript
28+
The schema for the `checklist` table:
29+
30+
```javascript
2931
const AppSchema = new Schema([
3032
new Table({
3133
name: 'checklists',
@@ -46,10 +48,10 @@ const AppSchema = new Schema([
4648
]);
4749
```
4850

49-
### Steps to implement `AttachmentQueue`
51+
### Steps to implement
5052

5153
1. Create a new class `AttachmentQueue` that extends `AbstractAttachmentQueue` from `@journeyapps/powersync-attachments`.
52-
```typescript
54+
```javascript
5355
import { AbstractAttachmentQueue } from '@journeyapps/powersync-attachments';
5456

5557
export class AttachmentQueue extends AbstractAttachmentQueue {
@@ -58,13 +60,14 @@ export class AttachmentQueue extends AbstractAttachmentQueue {
5860
```
5961

6062
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.
6263

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.
65+
66+
```javascript
6467
import { AbstractAttachmentQueue } from '@journeyapps/powersync-attachments';
6568

6669
export class AttachmentQueue extends AbstractAttachmentQueue {
67-
async *attachmentIds(): AsyncIterable<string[]> {
70+
async *attachmentIds() {
6871
for await (const result of this.powersync.watch(
6972
`SELECT photo_id as id FROM checklists WHERE photo_id IS NOT NULL`,
7073
[]
@@ -76,16 +79,15 @@ export class AttachmentQueue extends AbstractAttachmentQueue {
7679
```
7780
7881
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.
8184
82-
```typescript
85+
```javascript
8386
import { AbstractAttachmentQueue } from '@journeyapps/powersync-attachments';
8487

8588
export class AttachmentQueue extends AbstractAttachmentQueue {
8689
// ...
87-
88-
async newAttachmentRecord(record?: Partial<AttachmentRecord>): Promise<AttachmentRecord> {
90+
async newAttachmentRecord(record) {
8991
const photoId = record?.id ?? uuid();
9092
const filename = record?.filename ?? `${photoId}.jpg`;
9193
return {
@@ -101,7 +103,7 @@ export class AttachmentQueue extends AbstractAttachmentQueue {
101103
102104
4. Add an `AttachmentTable` to your app's PowerSync Schema:
103105
104-
```typescript
106+
```javascript
105107
import { AttachmentTable } from '@journeyapps/powersync-attachments';
106108

107109
const AppSchema = new Schema([
@@ -110,14 +112,14 @@ const AppSchema = new Schema([
110112
]);
111113
```
112114
113-
The `AttachmentTable` can optionally be configured with the following options, in addition to `Table` options:
115+
In addition to `Table` options, the `AttachmentTable` can optionally be configured with the following options:
114116
115-
| Option | Description | Default |
116-
|---------------------|--------------------------------------------------------------------------------------|-------------------------------|
117-
| `name` | The name of the table | `attachments` |
118-
| `additionalColumns` | An array of addition `Column` objects that added to the default columns in the table | See below for default columns |
117+
| Option | Description | Default |
118+
|---------------------|---------------------------------------------------------------------------------|-------------------------------|
119+
| `name` | The name of the table | `attachments` |
120+
| `additionalColumns` | An array of addition `Column` objects added to the default columns in the table | See below for default columns |
119121
120-
The default columns in the `AttachmentTable` are:
122+
The default columns in `AttachmentTable`:
121123
122124
| Column Name | Type | Description |
123125
|--------------|-----------|-------------------------------------------------------------------|
@@ -131,9 +133,10 @@ The default columns in the `AttachmentTable` are:
131133
5. To instantiate an `AttachmentQueue`, one needs to provide an instance of `AbstractPowerSyncDatabase` from PowerSync and an instance of `StorageAdapter`.
132134
See the `StorageAdapter` interface definition [here](./src/StorageAdapter.ts).
133135
134-
6. Finally, create a new `AttachmentQueue` and call `init()` to start syncing attachments. Our example, uses a `StorageAdapter` that integrates with Supabase Storage
135136
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
137140
this.storage = this.supabaseConnector.storage;
138141
this.powersync = factory.getInstance();
139142

@@ -142,14 +145,37 @@ this.attachmentQueue = new AttachmentQueue({
142145
storage: this.storage
143146
});
144147

148+
// Initialize and connect PowerSync ...
149+
// Then initialize the attachment queue
145150
await this.attachmentQueue.init();
146151
```
147152
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:
156+
157+
```javascript
158+
159+
export class AttachmentQueue extends AbstractAttachmentQueue {
160+
// ...
161+
async savePhoto(base64Data) {
162+
const photoAttachment = await this.newAttachmentRecord();
163+
photoAttachment.local_uri = this.getLocalUri(photoAttachment.filename);
164+
await this.storage.writeFile(photoAttachment.local_uri, base64Data, { encoding: 'base64' });
165+
166+
return this.saveToQueue(photoAttachment);
167+
}
168+
169+
}
170+
```
171+
148172
# Implementation details
149173
150-
## Attachment States
174+
## Attachment State
175+
176+
The `AttachmentQueue` class manages attachments in your app by tracking their state.
151177
152-
The `AttachmentQueue` class manages attachments in your app by tracking their state. The state of an attachment can be one of the following:
178+
The state of an attachment can be one of the following:
153179
154180
| State | Description |
155181
|-------------------|-------------------------------------------------------------------------------|
@@ -162,7 +188,7 @@ The `AttachmentQueue` class manages attachments in your app by tracking their st
162188
## Initial sync
163189
164190
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.
166192
167193
## Syncing attachments
168194
@@ -179,24 +205,25 @@ By default, this is every 30 seconds, but can be configured by setting `syncInte
179205
180206
### Downloading
181207
182-
- An `AttachmentRecord` is created or updated with QUEUED_DOWNLOAD state.
208+
- An `AttachmentRecord` is created or updated with `QUEUED_DOWNLOAD` state.
183209
- The watch query adds the `id` into a queue of IDs to download and triggers the download process
184210
- This checks whether the photo is already on the device and if so, skips downloading.
185211
- If the photo is not on the device, it is downloaded from cloud storage.
186212
- 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`.
188214
- If any of these fail, the download is retried in the next sync trigger.
189215
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-
197216
### Deleting attachments
198217
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:
200219
- Related `AttachmentRecord` is removed from attachments table.
201220
- 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

Comments
 (0)