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/silly-emus-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/common': minor
---

[Attachments] Extracted `AttachmentQueue` constructor options to an options interface named `AttachmentQueueOptions`.
31 changes: 17 additions & 14 deletions packages/common/etc/common.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,20 +408,8 @@ export interface AttachmentErrorHandler {
export function attachmentFromSql(row: any): AttachmentRecord;

// @alpha
export class AttachmentQueue implements AttachmentQueue {
constructor(input: {
db: AbstractPowerSyncDatabase;
remoteStorage: RemoteStorageAdapter;
localStorage: LocalStorageAdapter;
watchAttachments: (onUpdate: (attachment: WatchedAttachmentItem[]) => Promise<void>, signal: AbortSignal) => void;
tableName?: string;
logger?: ILogger;
syncIntervalMs?: number;
syncThrottleDuration?: number;
downloadAttachments?: boolean;
archivedCacheLimit?: number;
errorHandler?: AttachmentErrorHandler;
});
export class AttachmentQueue {
constructor(input: AttachmentQueueOptions);
readonly archivedCacheLimit: number;
// (undocumented)
clearQueue(): Promise<void>;
Expand Down Expand Up @@ -455,6 +443,21 @@ export class AttachmentQueue implements AttachmentQueue {
withAttachmentContext<T>(callback: (context: AttachmentContext) => Promise<T>): Promise<T>;
}

// @alpha
export interface AttachmentQueueOptions {
archivedCacheLimit?: number;
db: AbstractPowerSyncDatabase;
downloadAttachments?: boolean;
errorHandler?: AttachmentErrorHandler;
localStorage: LocalStorageAdapter;
logger?: ILogger;
remoteStorage: RemoteStorageAdapter;
syncIntervalMs?: number;
syncThrottleDuration?: number;
tableName?: string;
watchAttachments: (onUpdate: (attachment: WatchedAttachmentItem[]) => Promise<void>, signal: AbortSignal) => void;
}

// @alpha
export interface AttachmentRecord {
// (undocumented)
Expand Down
98 changes: 54 additions & 44 deletions packages/common/src/attachments/AttachmentQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,58 @@ import { ATTACHMENT_TABLE, AttachmentRecord, AttachmentState } from './Schema.js
import { SyncingService } from './SyncingService.js';
import { WatchedAttachmentItem } from './WatchedAttachmentItem.js';

/**
* Configuration options for {@link AttachmentQueue}.
*
* @experimental
* @alpha This is currently experimental and may change without a major version bump.
*/
export interface AttachmentQueueOptions {
/**
* PowerSync database instance
*/
db: AbstractPowerSyncDatabase;
/**
* Remote storage adapter for upload/download operations
*/
remoteStorage: RemoteStorageAdapter;
/**
* Local storage adapter for file persistence
*/
localStorage: LocalStorageAdapter;
/**
* Callback for monitoring attachment changes in your data model
*/
watchAttachments: (onUpdate: (attachment: WatchedAttachmentItem[]) => Promise<void>, signal: AbortSignal) => void;
/**
* Name of the table to store attachment records. Default: 'ps_attachment_queue'
*/
tableName?: string;
/**
* Logger instance. Defaults to db.logger
*/
logger?: ILogger;
/**
* Periodic polling interval in milliseconds for retrying failed uploads/downloads. Default: 30000
*/
syncIntervalMs?: number;
/**
* Throttle duration in milliseconds for the reactive watch query that detects attachment changes. Prevents rapid-fire syncs during bulk changes. Default: 30
*/
syncThrottleDuration?: number;
/**
* Whether to automatically download remote attachments. Default: true
*/
downloadAttachments?: boolean;
/**
* Maximum archived attachments before cleanup. Default: 100
*/
archivedCacheLimit?: number;

/** Handler for upload, download and delete errors */
errorHandler?: AttachmentErrorHandler;
}

/**
* AttachmentQueue manages the lifecycle and synchronization of attachments
* between local and remote storage.
Expand All @@ -21,7 +73,7 @@ import { WatchedAttachmentItem } from './WatchedAttachmentItem.js';
* @experimental
* @alpha This is currently experimental and may change without a major version bump.
*/
export class AttachmentQueue implements AttachmentQueue {
export class AttachmentQueue {
/** Timer for periodic synchronization operations */
private periodicSyncTimer?: ReturnType<typeof setInterval>;

Expand Down Expand Up @@ -100,49 +152,7 @@ export class AttachmentQueue implements AttachmentQueue {
downloadAttachments = true,
archivedCacheLimit = 100,
errorHandler
}: {
/**
* PowerSync database instance
*/
db: AbstractPowerSyncDatabase;
/**
* Remote storage adapter for upload/download operations
*/
remoteStorage: RemoteStorageAdapter;
/**
* Local storage adapter for file persistence
*/
localStorage: LocalStorageAdapter;
/**
* Callback for monitoring attachment changes in your data model
*/
watchAttachments: (onUpdate: (attachment: WatchedAttachmentItem[]) => Promise<void>, signal: AbortSignal) => void;
/**
* Name of the table to store attachment records. Default: 'ps_attachment_queue'
*/
tableName?: string;
/**
* Logger instance. Defaults to db.logger
*/
logger?: ILogger;
/**
* Periodic polling interval in milliseconds for retrying failed uploads/downloads. Default: 30000
*/
syncIntervalMs?: number;
/**
* Throttle duration in milliseconds for the reactive watch query that detects attachment changes. Prevents rapid-fire syncs during bulk changes. Default: 30
*/
syncThrottleDuration?: number;
/**
* Whether to automatically download remote attachments. Default: true
*/
downloadAttachments?: boolean;
/**
* Maximum archived attachments before cleanup. Default: 100
*/
archivedCacheLimit?: number;
errorHandler?: AttachmentErrorHandler;
}) {
}: AttachmentQueueOptions) {
this.db = db;
this.remoteStorage = remoteStorage;
this.localStorage = localStorage;
Expand Down
Loading