Add query for tagging multiple photos arbitrarily

This commit is contained in:
Sam Becker 2024-07-07 20:57:02 -05:00
parent ac5f709c3d
commit f7984600e1
2 changed files with 20 additions and 2 deletions

View File

@ -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}

View File

@ -41,8 +41,13 @@ export const sql = <T extends QueryResultRow>(
return query<T>(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 = (