Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-cameras-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/common': patch
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change affects the public API surface of WatchedAttachmentItem (even though non-breaking). I think this should be a minor change instead.

---

Allow watched attachment items to provide a media type for queued downloads.
1 change: 1 addition & 0 deletions packages/common/src/attachments/AttachmentQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export class AttachmentQueue {
state: AttachmentState.QUEUED_DOWNLOAD,
hasSynced: false,
metaData: watchedAttachment.metaData,
mediaType: watchedAttachment.mediaType,
timestamp: new Date().getTime()
});
continue;
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/attachments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,12 @@ type WatchedAttachmentItem = {
id: string;
fileExtension: string; // e.g., 'jpg', 'pdf'
metaData?: string;
mediaType?: string; // e.g., 'image/jpeg'
} | {
id: string;
filename: string; // e.g., 'document.pdf'
metaData?: string;
mediaType?: string; // e.g., 'application/pdf'
};
```

Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/attachments/WatchedAttachmentItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export type WatchedAttachmentItem =
filename: string;
fileExtension?: never;
metaData?: string;
mediaType?: string;
}
| {
id: string;
fileExtension: string;
filename?: never;
metaData?: string;
mediaType?: string;
};
61 changes: 61 additions & 0 deletions packages/node/tests/attachments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,67 @@ describe('attachment queue', () => {
}
);

it(
'should pass watched media type to downloaded attachments',
{
timeout: 10000
},
async () => {
queue = new AttachmentQueue({
db,
watchAttachments: (onUpdate) => {
db.watch(
/* sql */
`
SELECT
photo_id
FROM
users
WHERE
photo_id IS NOT NULL
`,
[],
{
onResult: async (result: any) =>
await onUpdate(
result.rows?._array.map((r: any) => ({
id: r.photo_id,
fileExtension: 'jpg',
mediaType: 'image/jpeg'
})) ?? []
)
}
);
},
remoteStorage: mockRemoteStorage,
localStorage: mockLocalStorage,
syncIntervalMs: INTERVAL_MILLISECONDS,
archivedCacheLimit: 0
});

await queue.startSync();

const id = await queue.generateAttachmentId();
await db.execute('INSERT INTO users (id, name, email, photo_id) VALUES (uuid(), ?, ?, ?)', [
'testuser',
'testuser@journeyapps.com',
id
]);

const attachments = await waitForMatchCondition(
() => watchAttachmentsTable(),
(results) => results.some((r) => r.id === id && r.state === AttachmentState.SYNCED),
5
);

const attachmentRecord = attachments.find((r) => r.id === id);
expect(attachmentRecord?.mediaType).toBe('image/jpeg');
expect(mockDownloadFile).toHaveBeenCalledWith(expect.objectContaining({ mediaType: 'image/jpeg' }));

await queue.stopSync();
}
);

it(
'should upload attachments when a new file is saved',
{
Expand Down
Loading