From f7984600e1639f09fe11bafec1bd4f5366905174 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Sun, 7 Jul 2024 20:57:02 -0500 Subject: [PATCH] Add query for tagging multiple photos arbitrarily --- src/photo/db/query.ts | 13 +++++++++++++ src/services/postgres.ts | 9 +++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/photo/db/query.ts b/src/photo/db/query.ts index 1c171580..be2c644e 100644 --- a/src/photo/db/query.ts +++ b/src/photo/db/query.ts @@ -243,6 +243,19 @@ export const renamePhotoTagGlobally = (tag: string, updatedTag: string) => WHERE ${tag}=ANY(tags) `, 'renamePhotoTagGlobally'); +export const addTagsToPhotos = (tags: string[], photoIds: string[]) => + safelyQueryPhotos(() => sql` + UPDATE photos + SET tags = ( + SELECT array_agg(DISTINCT elem) + FROM unnest( + array_cat(tags, ARRAY${convertArrayToPostgresString(tags, 'brackets')}) + ) AS elem + ) + WHERE id IN ${convertArrayToPostgresString(photoIds, 'brackets')} + LIMIT ${photoIds.length} + `, 'addTagsToPhotos'); + export const deletePhoto = (id: string) => safelyQueryPhotos(() => sql` DELETE FROM photos WHERE id=${id} diff --git a/src/services/postgres.ts b/src/services/postgres.ts index e2a239de..86a8e1fa 100644 --- a/src/services/postgres.ts +++ b/src/services/postgres.ts @@ -41,8 +41,13 @@ export const sql = ( return query(result, values); }; -export const convertArrayToPostgresString = (array?: string[]) => array - ? `{${array.join(',')}}` +export const convertArrayToPostgresString = ( + array?: string[], + type: 'braces' | 'brackets' = 'braces', +) => array + ? type === 'braces' + ? `{${array.join(',')}}` + : `[${array.map(i => `'${i}'`).join(',')}]` : null; const isTemplateStringsArray = (