Skip to main content

AttachmentContext

Experimental Alpha

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​

Constructor​

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

Alpha

Creates a new AttachmentContext instance.

Parameters​

ParameterTypeDefault valueDescription
dbCommonPowerSyncDatabaseundefinedPowerSync database instance
tableNamestring'attachments'Name of the table storing attachment records. Default: 'attachments'
loggerPowerSyncLoggerundefinedLogger instance for diagnostic output
archivedCacheLimitnumberundefined-

Returns​

AttachmentContext

Properties​

PropertyModifierTypeDefault valueDescription
archivedCacheLimitreadonlynumber100Alpha Maximum number of archived attachments to keep before cleanup
dbreadonlyCommonPowerSyncDatabaseundefinedAlpha PowerSync database instance for executing queries
loggerreadonlyPowerSyncLoggerundefinedAlpha Logger instance for diagnostic information
tableNamereadonlystringundefinedAlpha Name of the database table storing attachment records

Methods​

clearQueue()​

clearQueue(): Promise<void>;

Alpha Experimental

Returns​

Promise<void>


deleteArchivedAttachments()​

deleteArchivedAttachments(callback?): Promise<boolean>;

Alpha Experimental

Parameters​

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

Returns​

Promise<boolean>


deleteAttachment()​

deleteAttachment(attachmentId): Promise<void>;

Alpha

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[]>;

Alpha

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[]>;

Alpha

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<AttachmentRecord | undefined>;

Alpha Experimental

Parameters​

ParameterType
idstring

Returns​

Promise<AttachmentRecord | undefined>


getAttachments()​

getAttachments(): Promise<AttachmentRecord[]>;

Alpha

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>;

Alpha

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>;

Alpha

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>