Skip to content

Commit

Permalink
Sole tenancy cache
Browse files Browse the repository at this point in the history
  • Loading branch information
N2D4 committed Feb 7, 2025
1 parent fae433a commit 6007aa8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion apps/backend/src/lib/tenancies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export function tenancyPrismaToCrud(prisma: Prisma.TenancyGetPayload<{ include:

export type Tenancy = Awaited<ReturnType<typeof tenancyPrismaToCrud>>;

/**
* while not necessary, this cache just makes performance a little better
*
* eventually, we'll nicely pass around tenancies and won't need this function anymore, so the cache is a good temp
* solution
*/
const soleTenanciesCache = new Map<string, Tenancy>();

/**
* @deprecated This is a temporary function for the situation where every project has exactly one tenancy. Later,
* we will support multiple tenancies per project, and all uses of this function will be refactored.
Expand All @@ -43,7 +51,7 @@ export async function getSoleTenancyFromProject(projectId: string): Promise<Tena
*/
export async function getSoleTenancyFromProject(projectId: string, returnNullIfNotFound: boolean): Promise<Tenancy | null>;
export async function getSoleTenancyFromProject(projectId: string, returnNullIfNotFound: boolean = false) {
const tenancy = await getTenancyFromProject(projectId, 'main', null);
const tenancy = soleTenanciesCache.get(projectId) ?? await getTenancyFromProject(projectId, 'main', null);
if (!tenancy) {
if (returnNullIfNotFound) return null;

Expand All @@ -53,6 +61,7 @@ export async function getSoleTenancyFromProject(projectId: string, returnNullIfN
}
throw new StackAssertionError(`No tenancy found for project ${projectId}`, { projectId });
}
soleTenanciesCache.set(projectId, tenancy);
return tenancy;
}

Expand Down

0 comments on commit 6007aa8

Please sign in to comment.