From 3a146ca740f75e968c672d9b5333e1d830fd7315 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 4 Jul 2026 10:32:03 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[performance=20improvement]?= =?UTF-8?q?=20Bypass=20redundant=20DB=20queries=20in=20tenant=20access=20r?= =?UTF-8?q?esolution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 💡 What: Replaced `getWorkspaceExperienceBySlug` with `compileWorkspaceExperience` in `resolveTenantAccess` when the `Organization` is already fetched. Updated `selectMembershipOrganization` to use a generic type to preserve the full Organization object. 🎯 Why: To avoid redundant database queries when resolving tenant access. 📊 Impact: Eliminates an N+1 query pattern / redundant database fetch during authentication, improving login/auth performance. 🔬 Measurement: Observe reduced DB queries in the authentication flow. Co-authored-by: brycejohnson1417 <257422776+brycejohnson1417@users.noreply.github.com> --- .jules/bolt.md | 4 ++ .../auth/tenant-access-selection.ts | 6 +-- lib/application/auth/tenant-access.ts | 38 ++++++++++++------- 3 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..48588f3 --- /dev/null +++ b/.jules/bolt.md @@ -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. diff --git a/lib/application/auth/tenant-access-selection.ts b/lib/application/auth/tenant-access-selection.ts index 75e45ac..93362ca 100644 --- a/lib/application/auth/tenant-access-selection.ts +++ b/lib/application/auth/tenant-access-selection.ts @@ -8,11 +8,11 @@ export interface MembershipSelectionOrganization { slug: string; } -export function selectMembershipOrganization(input: { +export function selectMembershipOrganization(input: { memberships: MembershipSelectionMember[]; - organizationsById: Map; + organizationsById: Map; requestedSlug?: string | null; -}) { +}): T | null { const requestedSlug = input.requestedSlug?.trim() || null; for (const membership of input.memberships) { diff --git a/lib/application/auth/tenant-access.ts b/lib/application/auth/tenant-access.ts index 09f0d27..0346fc1 100644 --- a/lib/application/auth/tenant-access.ts +++ b/lib/application/auth/tenant-access.ts @@ -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"; @@ -96,27 +98,37 @@ export async function resolveTenantAccess( const existingMemberships = await members.listByEmail(normalized); if (existingMemberships.length) { - const organizationsById = new Map( + const organizationsById = new Map( (await organizations.listByIds(existingMemberships.map((member) => member.organizationId))).map((organization) => [ organization.id, organization, ]), ); - const organization = selectMembershipOrganization({ + const organization = selectMembershipOrganization({ 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", + }); + } } } @@ -124,11 +136,11 @@ export async function resolveTenantAccess( 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", }); } @@ -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", }); }