Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2024-07-04 - Bypass Redundant DB Queries in React Cache Methods

**Learning:** When using React's `cache()` to memoize data-fetching methods like `getWorkspaceExperienceBySlug(slug)`, the cache keys strictly off the provided arguments (e.g., `slug`). If an upstream function (like `resolveTenantAccess`) has already fetched the necessary objects (e.g., an `Organization` record) via a separate database query, passing these objects as optional arguments into the cached method to avoid an N+1 query will actually fragment the cache by changing the cache key.
**Action:** Instead of modifying the signature of the cached method, bypass it entirely when the data is already available in the application layer. Directly call the internal synchronous compiler (e.g., `compileWorkspaceExperience`) with the pre-fetched objects to bypass redundant DB queries, and retain the original async cached method only as a fallback.
6 changes: 3 additions & 3 deletions lib/application/auth/tenant-access-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export interface MembershipSelectionOrganization {
slug: string;
}

export function selectMembershipOrganization(input: {
export function selectMembershipOrganization<T extends MembershipSelectionOrganization>(input: {
memberships: MembershipSelectionMember[];
organizationsById: Map<string, MembershipSelectionOrganization>;
organizationsById: Map<string, T>;
requestedSlug?: string | null;
}) {
}): T | null {
const requestedSlug = input.requestedSlug?.trim() || null;

for (const membership of input.memberships) {
Expand Down
38 changes: 25 additions & 13 deletions lib/application/auth/tenant-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { selectMembershipOrganization } from "@/lib/application/auth/tenant-acce
import { getWorkspaceExperienceBySlug } from "@/lib/application/workspace/workspace-service";
import { OrganizationMemberRepository } from "@/lib/infrastructure/supabase/organization-member-repository";
import { OrganizationRepository } from "@/lib/infrastructure/supabase/organization-repository";
import { compileWorkspaceExperience } from "@/lib/platform/workspace/compiler";
import type { Organization } from "@/lib/domain/runtime";
import { resolveWorkspaceTemplateForEmailDomain } from "@/lib/platform/workspace/registry";
import { orgScopedHref } from "@/lib/presentation/org-slug";

Expand Down Expand Up @@ -96,39 +98,49 @@ export async function resolveTenantAccess(

const existingMemberships = await members.listByEmail(normalized);
if (existingMemberships.length) {
const organizationsById = new Map(
const organizationsById = new Map<string, Organization>(
(await organizations.listByIds(existingMemberships.map((member) => member.organizationId))).map((organization) => [
organization.id,
organization,
]),
);

const organization = selectMembershipOrganization({
const organization = selectMembershipOrganization<Organization>({
memberships: existingMemberships,
organizationsById,
requestedSlug,
});

if (organization) {
const workspace = await getWorkspaceExperienceBySlug(organization.slug);
return existingWorkspaceAccess({
slug: organization.slug,
name: organization.name,
workspace: workspace.workspace,
accessMethod: "membership",
});
const compiled = compileWorkspaceExperience({ slug: organization.slug, organization });
if (compiled) {
return existingWorkspaceAccess({
slug: organization.slug,
name: organization.name,
workspace: compiled.workspace,
accessMethod: "membership",
});
} else {
const workspace = await getWorkspaceExperienceBySlug(organization.slug);
return existingWorkspaceAccess({
slug: organization.slug,
name: organization.name,
workspace: workspace.workspace,
accessMethod: "membership",
});
}
}
}

const [, emailDomain = ""] = normalized.split("@");
if (emailDomain) {
const workspaceOrganization = await organizations.findFirstByWorkspaceEmailDomain(emailDomain);
if (workspaceOrganization && (!requestedSlug || workspaceOrganization.slug === requestedSlug)) {
const workspace = await getWorkspaceExperienceBySlug(workspaceOrganization.slug);
const compiled = compileWorkspaceExperience({ slug: workspaceOrganization.slug, organization: workspaceOrganization });
return existingWorkspaceAccess({
slug: workspaceOrganization.slug,
name: workspaceOrganization.name,
workspace: workspace.workspace,
workspace: compiled.workspace,
accessMethod: "domain_template",
});
}
Expand All @@ -143,11 +155,11 @@ export async function resolveTenantAccess(
if (guessed.slug) {
const existingOrganization = await organizations.findBySlug(guessed.slug).catch(() => null);
if (existingOrganization) {
const workspace = await getWorkspaceExperienceBySlug(existingOrganization.slug);
const compiled = compileWorkspaceExperience({ slug: existingOrganization.slug, organization: existingOrganization });
return existingWorkspaceAccess({
slug: existingOrganization.slug,
name: existingOrganization.name,
workspace: workspace.workspace,
workspace: compiled.workspace,
accessMethod: "domain_template",
});
}
Expand Down
Loading