@@ -91,6 +91,10 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
91
91
return this . options . powersync ;
92
92
}
93
93
94
+ get logger ( ) {
95
+ return this . powersync . logger ?? console ;
96
+ }
97
+
94
98
protected get storage ( ) {
95
99
return this . options . storage ;
96
100
}
@@ -123,7 +127,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
123
127
async watchAttachmentIds ( ) {
124
128
this . onAttachmentIdsChange ( async ( ids ) => {
125
129
const _ids = `${ ids . map ( ( id ) => `'${ id } '` ) . join ( ',' ) } ` ;
126
- console . debug ( `Queuing for sync, attachment IDs: [${ _ids } ]` ) ;
130
+ this . logger . debug ( `Queuing for sync, attachment IDs: [${ _ids } ]` ) ;
127
131
128
132
if ( this . initialSync ) {
129
133
this . initialSync = false ;
@@ -151,11 +155,11 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
151
155
id : id ,
152
156
state : AttachmentState . QUEUED_SYNC
153
157
} ) ;
154
- console . debug ( `Attachment (${ id } ) not found in database, creating new record` ) ;
158
+ this . logger . debug ( `Attachment (${ id } ) not found in database, creating new record` ) ;
155
159
await this . saveToQueue ( newRecord ) ;
156
160
} else if ( record . local_uri == null || ! ( await this . storage . fileExists ( this . getLocalUri ( record . local_uri ) ) ) ) {
157
161
// 2. Attachment in database but no local file, mark as queued download
158
- console . debug ( `Attachment (${ id } ) found in database but no local file, marking as queued download` ) ;
162
+ this . logger . debug ( `Attachment (${ id } ) found in database but no local file, marking as queued download` ) ;
159
163
await this . update ( {
160
164
...record ,
161
165
state : AttachmentState . QUEUED_DOWNLOAD
@@ -241,7 +245,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
241
245
filename : record . filename
242
246
} ) ;
243
247
} catch ( e ) {
244
- console . error ( e ) ;
248
+ this . logger . error ( e ) ;
245
249
}
246
250
}
247
251
@@ -267,7 +271,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
267
271
const localFilePathUri = this . getLocalUri ( record . local_uri ) ;
268
272
try {
269
273
if ( ! ( await this . storage . fileExists ( localFilePathUri ) ) ) {
270
- console . warn ( `File for ${ record . id } does not exist, skipping upload` ) ;
274
+ this . logger . warn ( `File for ${ record . id } does not exist, skipping upload` ) ;
271
275
await this . update ( {
272
276
...record ,
273
277
state : AttachmentState . QUEUED_DOWNLOAD
@@ -285,11 +289,11 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
285
289
} ) ;
286
290
// Mark as uploaded
287
291
await this . update ( { ...record , state : AttachmentState . SYNCED } ) ;
288
- console . debug ( `Uploaded attachment "${ record . id } " to Cloud Storage` ) ;
292
+ this . logger . debug ( `Uploaded attachment "${ record . id } " to Cloud Storage` ) ;
289
293
return true ;
290
294
} catch ( e : any ) {
291
295
if ( e . error == 'Duplicate' ) {
292
- console . debug ( `File already uploaded, marking ${ record . id } as synced` ) ;
296
+ this . logger . debug ( `File already uploaded, marking ${ record . id } as synced` ) ;
293
297
await this . update ( { ...record , state : AttachmentState . SYNCED } ) ;
294
298
return false ;
295
299
}
@@ -300,7 +304,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
300
304
return true ;
301
305
}
302
306
}
303
- console . error ( `UploadAttachment error for record ${ JSON . stringify ( record , null , 2 ) } ` ) ;
307
+ this . logger . error ( `UploadAttachment error for record ${ JSON . stringify ( record , null , 2 ) } ` ) ;
304
308
return false ;
305
309
}
306
310
}
@@ -314,7 +318,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
314
318
}
315
319
const localFilePathUri = this . getLocalUri ( record . local_uri ) ;
316
320
if ( await this . storage . fileExists ( localFilePathUri ) ) {
317
- console . debug ( `Local file already downloaded, marking "${ record . id } " as synced` ) ;
321
+ this . logger . debug ( `Local file already downloaded, marking "${ record . id } " as synced` ) ;
318
322
await this . update ( { ...record , state : AttachmentState . SYNCED } ) ;
319
323
return true ;
320
324
}
@@ -345,7 +349,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
345
349
media_type : fileBlob . type ,
346
350
state : AttachmentState . SYNCED
347
351
} ) ;
348
- console . debug ( `Downloaded attachment "${ record . id } "` ) ;
352
+ this . logger . debug ( `Downloaded attachment "${ record . id } "` ) ;
349
353
return true ;
350
354
} catch ( e ) {
351
355
if ( this . options . onDownloadError ) {
@@ -355,7 +359,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
355
359
return true ;
356
360
}
357
361
}
358
- console . error ( `Download attachment error for record ${ JSON . stringify ( record , null , 2 ) } ` , e ) ;
362
+ this . logger . error ( `Download attachment error for record ${ JSON . stringify ( record , null , 2 ) } ` , e ) ;
359
363
}
360
364
return false ;
361
365
}
@@ -396,7 +400,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
396
400
if ( ! record ) {
397
401
return ;
398
402
}
399
- console . debug ( `Uploading attachments...` ) ;
403
+ this . logger . debug ( `Uploading attachments...` ) ;
400
404
while ( record ) {
401
405
const uploaded = await this . uploadAttachment ( record ) ;
402
406
if ( ! uploaded ) {
@@ -405,9 +409,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
405
409
}
406
410
record = await this . getNextUploadRecord ( ) ;
407
411
}
408
- console . debug ( 'Finished uploading attachments' ) ;
412
+ this . logger . debug ( 'Finished uploading attachments' ) ;
409
413
} catch ( error ) {
410
- console . error ( 'Upload failed:' , error ) ;
414
+ this . logger . error ( 'Upload failed:' , error ) ;
411
415
} finally {
412
416
this . uploading = false ;
413
417
}
@@ -464,7 +468,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
464
468
465
469
this . downloading = true ;
466
470
try {
467
- console . debug ( `Downloading ${ this . downloadQueue . size } attachments...` ) ;
471
+ this . logger . debug ( `Downloading ${ this . downloadQueue . size } attachments...` ) ;
468
472
while ( this . downloadQueue . size > 0 ) {
469
473
const id = this . downloadQueue . values ( ) . next ( ) . value ;
470
474
this . downloadQueue . delete ( id ) ;
@@ -474,9 +478,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
474
478
}
475
479
await this . downloadRecord ( record ) ;
476
480
}
477
- console . debug ( 'Finished downloading attachments' ) ;
481
+ this . logger . debug ( 'Finished downloading attachments' ) ;
478
482
} catch ( e ) {
479
- console . error ( 'Downloads failed:' , e ) ;
483
+ this . logger . error ( 'Downloads failed:' , e ) ;
480
484
} finally {
481
485
this . downloading = false ;
482
486
}
@@ -518,7 +522,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
518
522
return ;
519
523
}
520
524
521
- console . debug ( `Deleting ${ res . length } attachments from cache...` ) ;
525
+ this . logger . debug ( `Deleting ${ res . length } attachments from cache...` ) ;
522
526
await this . powersync . writeTransaction ( async ( tx ) => {
523
527
for ( const record of res ) {
524
528
await this . delete ( record , tx ) ;
@@ -527,7 +531,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
527
531
}
528
532
529
533
async clearQueue ( ) : Promise < void > {
530
- console . debug ( `Clearing attachment queue...` ) ;
534
+ this . logger . debug ( `Clearing attachment queue...` ) ;
531
535
await this . powersync . writeTransaction ( async ( tx ) => {
532
536
await tx . execute ( `DELETE FROM ${ this . table } ` ) ;
533
537
} ) ;
0 commit comments