Multi-Tenant Data Isolation
User Intent
Operation
Multi-Tenant Pattern (Recommended)
Strategy: Per-User Scoping in Single Project
import { Graphlit, Types } from 'graphlit-client';
// 1. Admin client (no user scoping) for user management
const adminClient = new Graphlit({
organizationId: process.env.GRAPHLIT_ORGANIZATION_ID!,
environmentId: process.env.GRAPHLIT_ENVIRONMENT_ID!,
jwtSecret: process.env.GRAPHLIT_JWT_SECRET!,
});
// 2. When a user signs up (e.g., via Clerk, Supabase, Auth0)
async function onUserSignUp(authUser: { id: string; name: string; email: string }) {
// Create user in Graphlit (maps to your auth provider's user)
const user = await adminClient.createUser({
name: authUser.name,
identifier: authUser.id, // Your auth provider's user ID (Clerk, Supabase, etc.)
type: Types.UserTypes.Human,
});
// Store user.createUser.id in your database alongside auth user
await db.users.update(authUser.id, {
graphlitUserId: user.createUser.id
});
}
// 3. On each user request, create scoped client
async function handleUserRequest(authUserId: string, request: any) {
// Get Graphlit user ID from your database
const graphlitUserId = await db.users.getGraphlitUserId(authUserId);
// Create user-scoped client
const userClient = new Graphlit({
organizationId: process.env.GRAPHLIT_ORGANIZATION_ID!,
environmentId: process.env.GRAPHLIT_ENVIRONMENT_ID!,
jwtSecret: process.env.GRAPHLIT_JWT_SECRET!,
userId: graphlitUserId, // ← ALL operations now scoped to this user
});
// All content, conversations, and feeds are automatically isolated
const results = await userClient.queryContents({
search: request.query,
});
return results; // Only returns this user's content
}What Gets Scoped?
Complete Example: Multi-Tenant SaaS
Multi-Tenant Benefits
Implementation Checklist
Alternative: Legacy ownerId (Deprecated)
Last updated