Menu

1 / 2

Scoping Multi-Tenant Runtime Context in Fastify Without Protecting Every Route

DEV Community: fastify·Emmanue; Nodolo·about 23 hours ago
#boojyIKG
#devtofastify#devto#dev#fullscreen#tenant
Reading 0:00
15s threshold

Multi-tenancy does not always belong at the root of an application.

A production API may have routes such as:

/health /status /metrics

Enter fullscreen mode

Exit fullscreen mode

that should remain independent from tenant resolution, while application routes such as:

/users /orders /projects

Enter fullscreen mode

Exit fullscreen mode

need a tenant-aware execution boundary.

Fastify's plugin encapsulation makes this separation surprisingly natural.

While building the Fastify framework track for Ambiten, I ended up with a pattern that expresses this boundary directly in the application structure.

import Fastify from " fastify " ; import { createFastifyAdapter } from " @ambiten/adapter-fastify " ;

import { AmbitenContext , MultiTenantManager } from " @ambiten/core " ;

import { registerDemoTenants } from " ./core/tenancy " ;

export function buildApp ( logger = false ) { registerDemoTenants ();

const app = Fastify ({ logger });

// This route lives outside the // tenant-protected child plugin. app .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.