Skip to content

Commit 6e797bb

Browse files
committed
feat: prepare organization enrichment and cache tables
1 parent ba0e1f3 commit 6e797bb

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
drop table "organizationEnrichments";
2+
drop table "organizationEnrichmentCache";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
create table "organizationEnrichments"(
2+
"organizationId" uuid primary key,
3+
"lastTriedAt" timestamp with time zone,
4+
"lastUpdatedAt" timestamp with time zone
5+
);
6+
7+
create table "organizationEnrichmentCache" (
8+
"organizationId" uuid not null references organizations(id) on delete no action on update no action,
9+
"data" jsonb not null,
10+
"source" text not null,
11+
"createdAt" timestamp with time zone not null,
12+
"updatedAt" timestamp with time zone not null,
13+
primary key ("organizationId", source)
14+
);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { 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+
) {
11+
return 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+
) {
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+
) {
36+
return qx.result(
37+
`update "organizationEnrichmentCache" set "data" = $(data), "updatedAt" = now() where "organizationId" = $(organizationId) and "source" = $(source)`,
38+
{ organizationId, data, source },
39+
)
40+
}

services/libs/types/src/enums/enrichment.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ export enum MemberEnrichmentMaterializedView {
1717
ENTITY_UPDATES_ANALYSIS = 'memberEnrichmentMonitoringEntityUpdates',
1818
LLM_USAGE_ANALYSIS = 'memberEnrichmentMonitoringLLMQueries',
1919
}
20+
21+
export enum OrganizationEnrichmentSource {
22+
CLEARBIT = 'clearbit',
23+
}

0 commit comments

Comments
 (0)