db/unattached-media: refactor queries, DRY
This commit is contained in:
parent
43499f2dfd
commit
0d343fa190
|
@ -10,6 +10,7 @@ interface UnattachedMedia {
|
|||
uploaded_at: Date;
|
||||
}
|
||||
|
||||
/** Add unattached media into the database. */
|
||||
async function insertUnattachedMedia(media: Omit<UnattachedMedia, 'id' | 'uploaded_at'>) {
|
||||
const result = {
|
||||
id: uuid62.v4(),
|
||||
|
@ -24,8 +25,8 @@ async function insertUnattachedMedia(media: Omit<UnattachedMedia, 'id' | 'upload
|
|||
return result;
|
||||
}
|
||||
|
||||
/** Find attachments that exist but aren't attached to any events. */
|
||||
function getUnattachedMedia(until: Date) {
|
||||
/** Select query for unattached media. */
|
||||
function selectUnattachedMediaQuery() {
|
||||
return db.selectFrom('unattached_media')
|
||||
.select([
|
||||
'unattached_media.id',
|
||||
|
@ -33,27 +34,27 @@ function getUnattachedMedia(until: Date) {
|
|||
'unattached_media.url',
|
||||
'unattached_media.data',
|
||||
'unattached_media.uploaded_at',
|
||||
])
|
||||
]);
|
||||
}
|
||||
|
||||
/** Find attachments that exist but aren't attached to any events. */
|
||||
function getUnattachedMedia(until: Date) {
|
||||
return selectUnattachedMediaQuery()
|
||||
.leftJoin('tags', 'unattached_media.url', 'tags.value')
|
||||
.where('uploaded_at', '<', until)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Delete unattached media by URL. */
|
||||
function deleteUnattachedMediaByUrl(url: string) {
|
||||
return db.deleteFrom('unattached_media')
|
||||
.where('url', '=', url)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Get unattached media by IDs. */
|
||||
function getUnattachedMediaByIds(ids: string[]) {
|
||||
return db.selectFrom('unattached_media')
|
||||
.select([
|
||||
'unattached_media.id',
|
||||
'unattached_media.pubkey',
|
||||
'unattached_media.url',
|
||||
'unattached_media.data',
|
||||
'unattached_media.uploaded_at',
|
||||
])
|
||||
return selectUnattachedMediaQuery()
|
||||
.where('id', 'in', ids)
|
||||
.execute();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue