Skip to content

@powersync/attachments deletes synced items from storage when greater than cacheLimit #716

@riisi

Description

@riisi

My expectation of the way cache should work (it's a little unclear from the docs) is that the cache is a local cache, and @powersync/attachments should manage the size. The full set of current (not-orphaned/archived) data should remain on remote.

In testing, I found that if cacheLimit is set to a low value, e.g., 2, then only 2 items are preserved in (remote) storage.
So the cacheLimit effectively applies to both local and remote files.

Inspecting the source seems to confirm this -

The delete method deletes from both local and remote:

async delete(record: AttachmentRecord, tx?: Transaction): Promise<void> {
const deleteRecord = async (tx: Transaction) => {
await tx.execute(
`DELETE
FROM ${this.table}
WHERE id = ?`,
[record.id]
);
};
if (tx) {
await deleteRecord(tx);
} else {
await this.powersync.writeTransaction(deleteRecord);
}
const localFilePathUri = this.getLocalUri(record.local_uri || this.getLocalFilePathSuffix(record.filename));
try {
// Delete file on storage
await this.storage.deleteFile(localFilePathUri, {
filename: record.filename
});
} catch (e) {
this.logger.error(e);
}
}

delete() is called from expire() to delete the oldest synced or archived attachment:

async expireCache() {
const res = await this.powersync.getAll<AttachmentRecord>(`SELECT * FROM ${this.table}
WHERE
state = ${AttachmentState.SYNCED} OR state = ${AttachmentState.ARCHIVED}
ORDER BY
timestamp DESC
LIMIT 100 OFFSET ${this.options.cacheLimit}`);
if (res.length == 0) {
return;
}
this.logger.debug(`Deleting ${res.length} attachments from cache...`);
await this.powersync.writeTransaction(async (tx) => {
for (const record of res) {
await this.delete(record, tx);
}
});
}

Am I missing something here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions