Skip to main content

AttachmentContext

Internal

AttachmentContext provides database operations for managing attachment records.

Provides methods to query, insert, update, and delete attachment records with proper transaction management through PowerSync.

Constructors

new AttachmentContext()

new AttachmentContext(
db,
tableName,
logger,
archivedCacheLimit): AttachmentContext

Creates a new AttachmentContext instance.

Parameters

ParameterTypeDescription
dbAbstractPowerSyncDatabasePowerSync database instance
tableNameundefined | stringName of the table storing attachment records. Default: 'attachments'
loggerILoggerLogger instance for diagnostic output
archivedCacheLimitnumber-

Returns

AttachmentContext

Properties

PropertyTypeDescription
archivedCacheLimitnumberMaximum number of archived attachments to keep before cleanup
dbAbstractPowerSyncDatabasePowerSync database instance for executing queries
loggerILoggerLogger instance for diagnostic information
tableNamestringName of the database table storing attachment records

Methods

clearQueue()

clearQueue(): Promise<void>

Returns

Promise<void>


deleteArchivedAttachments()

deleteArchivedAttachments(callback?): Promise<boolean>

Parameters

ParameterType
callback?(attachments) => Promise<void>

Returns

Promise<boolean>


deleteAttachment()

deleteAttachment(attachmentId): Promise<void>

Permanently deletes an attachment record from the database.

This operation removes the attachment record but does not delete the associated local or remote files. File deletion should be handled separately through the appropriate storage adapters.

Parameters

ParameterTypeDescription
attachmentIdstringUnique identifier of the attachment to delete

Returns

Promise<void>


getActiveAttachments()

getActiveAttachments(): Promise<AttachmentRecord[]>

Retrieves all active attachments that require synchronization. Active attachments include those queued for upload, download, or delete. Results are ordered by timestamp in ascending order.

Returns

Promise<AttachmentRecord[]>

Promise resolving to an array of active attachment records


getArchivedAttachments()

getArchivedAttachments(): Promise<AttachmentRecord[]>

Retrieves all archived attachments.

Archived attachments are no longer referenced but haven't been permanently deleted. These are candidates for cleanup operations to free up storage space.

Returns

Promise<AttachmentRecord[]>

Promise resolving to an array of archived attachment records


getAttachment()

getAttachment(id): Promise<undefined | AttachmentRecord>

Parameters

ParameterType
idstring

Returns

Promise<undefined | AttachmentRecord>


getAttachments()

getAttachments(): Promise<AttachmentRecord[]>

Retrieves all attachment records regardless of state. Results are ordered by timestamp in ascending order.

Returns

Promise<AttachmentRecord[]>

Promise resolving to an array of all attachment records


saveAttachments()

saveAttachments(attachments): Promise<void>

Saves multiple attachment records in a single transaction.

All updates are saved in a single batch after processing. If the attachments array is empty, no database operations are performed.

Parameters

ParameterTypeDescription
attachmentsAttachmentRecord[]Array of attachment records to save

Returns

Promise<void>


upsertAttachment()

upsertAttachment(attachment, context): Promise<void>

Inserts or updates an attachment record within an existing transaction.

Performs an upsert operation (INSERT OR REPLACE). Must be called within an active database transaction context.

Parameters

ParameterTypeDescription
attachmentAttachmentRecordThe attachment record to upsert
contextTransactionActive database transaction context

Returns

Promise<void>