Skip to content

Commit

Permalink
feat: check for empty cachtags (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
celine-s authored Sep 27, 2024
1 parent fce5c77 commit e8aa483
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cache-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export const generateQueryId = <TVariables = unknown>(document: DocumentNode, va
*/

export const storeQueryCacheTags = async (queryId: string, cacheTags: CacheTag[]) => {
if (!cacheTags?.length) {
return;
}

await sql.query(
`INSERT INTO query_cache_tags VALUES ${cacheTags.map((cacheTag) => `('${queryId}', '${cacheTag}')`).join()} ON CONFLICT DO NOTHING`,
);
Expand All @@ -55,6 +59,9 @@ export const storeQueryCacheTags = async (queryId: string, cacheTags: CacheTag[]
*/

export const queriesReferencingCacheTags = async (cacheTags: CacheTag[]): Promise<string[]> => {
if (!cacheTags?.length) {
return [];
}
const { rows }: { rows: { query_id: string }[] } = await sql.query(
`SELECT DISTINCT query_id FROM query_cache_tags WHERE cache_tag IN (${cacheTags.map((cacheTag) => `'${cacheTag}'`).join(', ')})`,
);
Expand All @@ -69,5 +76,8 @@ export const queriesReferencingCacheTags = async (cacheTags: CacheTag[]): Promis
*/

export const deleteQueries = async (queryIds: string[]) => {
if (!queryIds?.length) {
return;
}
await sql.query(`DELETE FROM query_cache_tags WHERE query_id IN (${queryIds.map((id) => `'${id}'`).join(', ')})`);
};

0 comments on commit e8aa483

Please sign in to comment.