|
| 1 | +import { IOrganizationEnrichmentCache, OrganizationEnrichmentSource } from '@crowd/types' |
| 2 | + |
| 3 | +import { QueryExecutor } from '../../queryExecutor' |
| 4 | + |
| 5 | +export async function insertOrganizationEnrichmentCache<T>( |
| 6 | + qx: QueryExecutor, |
| 7 | + organizationId: string, |
| 8 | + data: T, |
| 9 | + source: OrganizationEnrichmentSource, |
| 10 | +): Promise<void> { |
| 11 | + await qx.result( |
| 12 | + `insert into "organizationEnrichmentCache" ("organizationId", "data", "source", "createdAt", "updatedAt") |
| 13 | + values ($(organizationId), $(data), $(source), now(), now()) |
| 14 | + on conflict ("organizationId", "source") do update set "data" = $(data), "updatedAt" = now()`, |
| 15 | + { organizationId, data, source }, |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +export async function findOrganizationEnrichmentCache<T>( |
| 20 | + qx: QueryExecutor, |
| 21 | + organizationId: string, |
| 22 | + source: OrganizationEnrichmentSource, |
| 23 | +): Promise<IOrganizationEnrichmentCache<T> | null> { |
| 24 | + return qx.selectOneOrNone( |
| 25 | + `select * from "organizationEnrichmentCache" where "organizationId" = $(organizationId) and "source" = $(source)`, |
| 26 | + { organizationId, source }, |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +export async function updateOrganizationEnrichmentCache<T>( |
| 31 | + qx: QueryExecutor, |
| 32 | + organizationId: string, |
| 33 | + data: T, |
| 34 | + source: OrganizationEnrichmentSource, |
| 35 | +): Promise<void> { |
| 36 | + await qx.result( |
| 37 | + `update "organizationEnrichmentCache" set "data" = $(data), "updatedAt" = now() where "organizationId" = $(organizationId) and "source" = $(source)`, |
| 38 | + { organizationId, data, source }, |
| 39 | + ) |
| 40 | +} |
0 commit comments