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 = (