Introduction
The current version of the To-Do List demo app implements aPhotoAttachmentQueue
class which
enables photo attachments (specifically a jpeg
) to be synced. This tutorial will guide you on the changes needed to support PDF attachments.
An overview of the required changes are:
- Update the app schema by adding a
pdf_id
column to the todos table to link a pdf to a to-do item. - Add a
PdfAttachmentQueue
class - Initialize the
PdfAttachmentQueue
class
The following pre-requisites are required to complete this tutorial:
- Clone the To-Do List demo app repo
- Follow the instructions in the README and ensure that the app runs locally
- A running PowerSync Service and Supabase (can be self-hosted)
- Storage configuration in Supabase
Steps
Step 1: Update app schema
Step 1: Update app schema
Step 2: Create the `PdfAttachmentQueue` class
Step 2: Create the `PdfAttachmentQueue` class
The
PdfAttachmentQueue
class below updates the existing PhotoAttachmentQueue
found in the demo app. The highlighted lines indicate which lines have been updated. For more information on attachments, see the attachments package.PdfAttachmentQueue.ts
Step 3: Initialize the `PdfAttachmentQueue` class
Step 3: Initialize the `PdfAttachmentQueue` class
We start by importing the The We can then update the The complete updated
PdfAttachmentQueue
and adding an attachmentPdfQueue
class variable.attachmentPdfQueue
can then be initialized in the constructor, where a new instance of PdfAttachmentQueue is created and assigned to attachmentPdfQueue
if the supabaseBucket
is configured.init
method to include the initialization of the attachmentPdfQueue
.system.ts
file can be found below with highlighted lines indicating the changes made above.system.ts
Usage Example
The newly createdattachmentPdfQueue
can now be used in a component by using the useSystem
hook created in step-3 above
The code snippet below illustrates how a pdf could be saved when pressing a button. It uses a DocumentPicker UI component
to allow the user to select a pdf. When the button is pressed, savePdf
is called.
The saveAttachment
method in the PdfAttachmentQueue
class expects a base64 encoded string. We can therefore use
react-native-fs to read the file and return the base64 encoded string which is passed to saveAttachment
.
If your use-case generates a pdf file, ensure that you return a base64 encoded string.
Notes
Although this tutorial adds a newpdf_id
column, the approach you should take strongly depends on your requirements.
An alternative approach could be to replace the photo_id
with an attachment_id
and have one AttachmentQueue
class that handles all attachment types instead of having a class per attachment type.