Skip to content

Commit 160b2ad

Browse files
committed
refactor(attachments): streamline AttachmentQueue and transport adapter interfaces
This commit refines the AttachmentQueue and transport adapter interfaces by consolidating configuration options and enhancing type safety. The `AttachmentQueueOptions` now requires either a `remoteStorage` or a `transportAdapter`, ensuring clarity in usage. Additionally, the `saveFile` and `saveFileFromUri` methods have been updated to utilize a unified `SaveAttachmentOptions` type, improving consistency across attachment operations. Documentation in the README.md has also been updated to reflect these changes.
1 parent 16c6a3b commit 160b2ad

7 files changed

Lines changed: 117 additions & 177 deletions

File tree

packages/attachments-storage-react-native/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ const localStorage = new ExpoFileSystemStorageAdapter('/custom/path/to/attachmen
8585

8686
## Streaming transport adapters
8787

88-
A transport adapter owns **all** remote operations (`upload` / `download` / `delete`) and transfers bytes natively — the file never enters the JS heap. When a `transportAdapter` is provided it fully replaces `remoteStorage` (which is no longer required); implement remote delete via the `deleteFile` callback.
88+
A transport adapter owns **all** remote operations (`upload` / `download` / `delete`) and transfers bytes natively — the file never enters the JS heap. Implement remote delete via the `deleteFile` callback.
89+
90+
The queue takes **exactly one** remote mechanism: either `remoteStorage` or `transportAdapter`. A `transportAdapter` replaces `remoteStorage` entirely, so none is needed alongside it.
8991

9092
Both transports are backend-agnostic: you supply resolver callbacks that map an attachment to a request (typically a presigned URL from your backend).
9193

packages/attachments-storage-react-native/src/ExpoFileSystemTransportAdapter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface ExpoFileSystemTransportAdapterOptions {
5151
deleteFile: (attachment: AttachmentRecord) => Promise<void>;
5252
}
5353

54+
/** The `expo-file-system` legacy API surface this adapter resolves and uses at runtime. */
5455
type ExpoUploadResult = { status: number; body?: string };
5556
type ExpoDownloadResult = { status: number; uri: string };
5657

packages/common/etc/common.api.md

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,11 @@ export class AttachmentQueue {
9696
generateAttachmentId(): Promise<string>;
9797
readonly localStorage: LocalStorageAdapter;
9898
readonly logger: PowerSyncLogger;
99-
readonly remoteStorage?: RemoteStorageAdapter;
100-
saveFile(input: {
99+
saveFile(options: SaveAttachmentOptions & {
101100
data: AttachmentData;
102-
fileExtension: string;
103-
mediaType?: string;
104-
metaData?: string;
105-
id?: string;
106-
updateHook?: (transaction: Transaction, attachment: AttachmentRecord) => Promise<void>;
107101
}): Promise<AttachmentRecord>;
108-
saveFileFromUri(input: {
102+
saveFileFromUri(options: SaveAttachmentOptions & {
109103
localUri: string;
110-
fileExtension: string;
111-
mediaType?: string;
112-
metaData?: string;
113-
id?: string;
114-
updateHook?: (transaction: Transaction, attachment: AttachmentRecord) => Promise<void>;
115104
}): Promise<AttachmentRecord>;
116105
startSync(): Promise<void>;
117106
stopSync(): Promise<void>;
@@ -124,20 +113,13 @@ export class AttachmentQueue {
124113
}
125114

126115
// @alpha
127-
export interface AttachmentQueueOptions {
128-
archivedCacheLimit?: number;
129-
db: CommonPowerSyncDatabase;
130-
downloadAttachments?: boolean;
131-
errorHandler?: AttachmentErrorHandler;
132-
localStorage: LocalStorageAdapter;
133-
logger?: PowerSyncLogger;
134-
remoteStorage?: RemoteStorageAdapter;
135-
syncIntervalMs?: number;
136-
syncThrottleDuration?: number;
137-
tableName?: string;
138-
transportAdapter?: AttachmentTransportAdapter;
139-
watchAttachments: (onUpdate: (attachment: WatchedAttachmentItem[]) => Promise<void>, signal: AbortSignal) => void;
140-
}
116+
export type AttachmentQueueOptions = BaseAttachmentQueueOptions & ({
117+
remoteStorage: RemoteStorageAdapter;
118+
transportAdapter?: never;
119+
} | {
120+
transportAdapter: AttachmentTransportAdapter;
121+
remoteStorage?: never;
122+
});
141123

142124
// @alpha
143125
export interface AttachmentRecord {
@@ -194,6 +176,20 @@ export interface AttachmentTransportAdapter {
194176
upload(attachment: LocatedAttachmentRecord): Promise<void>;
195177
}
196178

179+
// @alpha
180+
export interface BaseAttachmentQueueOptions {
181+
archivedCacheLimit?: number;
182+
db: CommonPowerSyncDatabase;
183+
downloadAttachments?: boolean;
184+
errorHandler?: AttachmentErrorHandler;
185+
localStorage: LocalStorageAdapter;
186+
logger?: PowerSyncLogger;
187+
syncIntervalMs?: number;
188+
syncThrottleDuration?: number;
189+
tableName?: string;
190+
watchAttachments: (onUpdate: (attachment: WatchedAttachmentItem[]) => Promise<void>, signal: AbortSignal) => void;
191+
}
192+
197193
// @public (undocumented)
198194
export type BaseColumnType<T extends number | string | null> = {
199195
type: ColumnType;
@@ -258,17 +254,6 @@ export interface BatchedUpdateNotification {
258254
tables: string[];
259255
}
260256

261-
// @alpha
262-
export class BufferedAttachmentTransport implements AttachmentTransportAdapter {
263-
constructor(localStorage: LocalStorageAdapter, remoteStorage: RemoteStorageAdapter);
264-
// (undocumented)
265-
delete(attachment: AttachmentRecord): Promise<void>;
266-
// (undocumented)
267-
download(attachment: LocatedAttachmentRecord): Promise<void>;
268-
// (undocumented)
269-
upload(attachment: LocatedAttachmentRecord): Promise<void>;
270-
}
271-
272257
// @public (undocumented)
273258
export class Column {
274259
constructor(options: ColumnOptions);
@@ -944,6 +929,15 @@ export function sanitizeSQL(strings: TemplateStringsArray, ...values: any[]): st
944929
// @alpha
945930
export function sanitizeUUID(uuid: string): string;
946931

932+
// @alpha
933+
export interface SaveAttachmentOptions {
934+
fileExtension: string;
935+
id?: string;
936+
mediaType?: string;
937+
metaData?: string;
938+
updateHook?: (transaction: Transaction, attachment: AttachmentRecord) => Promise<void>;
939+
}
940+
947941
// Warning: (ae-forgotten-export) The symbol "SchemaType" needs to be exported by the entry point index.d.ts
948942
//
949943
// @public

0 commit comments

Comments
 (0)