Skip to main content

SyncingService

Internal

Orchestrates attachment synchronization between local and remote storage. Handles uploads, downloads, deletions, and state transitions.

Constructors

new SyncingService()

new SyncingService(
attachmentService,
localStorage,
remoteStorage,
logger,
errorHandler?): SyncingService

Parameters

ParameterType
attachmentServiceAttachmentService
localStorageLocalStorageAdapter
remoteStorageRemoteStorageAdapter
loggerILogger
errorHandler?AttachmentErrorHandler

Returns

SyncingService

Methods

deleteArchivedAttachments()

deleteArchivedAttachments(context): Promise<boolean>

Performs cleanup of archived attachments by removing their local files and records. Errors during local file deletion are logged but do not prevent record deletion.

Parameters

ParameterType
contextAttachmentContext

Returns

Promise<boolean>


deleteAttachment()

deleteAttachment(attachment, context): Promise<AttachmentRecord>

Deletes an attachment from both remote and local storage. Removes the remote file, local file (if exists), and the attachment record. On failure, defers to error handler or archives.

Parameters

ParameterTypeDescription
attachmentAttachmentRecordThe attachment record to delete
contextAttachmentContextAttachment context for database operations

Returns

Promise<AttachmentRecord>

Updated attachment record


downloadAttachment()

downloadAttachment(attachment): Promise<AttachmentRecord>

Downloads an attachment from remote storage to local storage. Retrieves the file, converts to base64, and saves locally. On success, marks as SYNCED. On failure, defers to error handler or archives.

Parameters

ParameterTypeDescription
attachmentAttachmentRecordThe attachment record to download

Returns

Promise<AttachmentRecord>

Updated attachment record with local URI and new state


processAttachments()

processAttachments(attachments, options?): Promise<void>

Processes attachments based on their state (upload, download, or delete).

Each attachment's I/O runs outside the attachment-service mutex, and the row's state transition is persisted immediately after it completes. This keeps the mutex available to concurrent saveFile / deleteFile / watched-attachment processing while a batch is in flight, and means consumer queries against the attachments queue see incremental progress instead of one atomic commit at the end of the batch.

Parameters

ParameterTypeDescription
attachmentsAttachmentRecord[]Array of attachment records to process
options?{ signal: AbortSignal; }Optional controls. Pass signal (an AbortSignal) to interrupt the batch: it is checked between attachments and, once aborted, the loop exits early — letting stopSync stop a running batch within one attachment's processing time.
options.signal?AbortSignal-

Returns

Promise<void>


uploadAttachment()

uploadAttachment(attachment): Promise<AttachmentRecord>

Uploads an attachment from local storage to remote storage. On success, marks as SYNCED. On failure, defers to error handler or archives.

Parameters

ParameterTypeDescription
attachmentAttachmentRecordThe attachment record to upload

Returns

Promise<AttachmentRecord>

Updated attachment record with new state

Throws

Error if the attachment has no localUri