Skip to content

Multi-Tenancy ​

Cognipeer is a fully multi-tenant platform with complete data isolation per company. Each tenant gets a dedicated database — either a separate SQLite file (default) or a separate MongoDB database.

Database Backends ​

SQLite (default) ​

Each tenant gets its own .sqlite file. No external services required:

data/
├── console_main.sqlite       ← Shared database
│   ├── tenants               ← Tenant registry
│   └── tenant_user_directory ← Cross-tenant email lookup
├── tenant_acme.sqlite        ← Tenant "acme"
│   ├── users
│   ├── api_tokens
│   ├── models
│   ├── providers
│   ├── vector_indexes
│   ├── guardrails
│   ├── prompts
│   ├── memory_stores
│   ├── rag_modules
│   └── ...
└── tenant_globex.sqlite      ← Tenant "globex"

Enable with: DB_PROVIDER=sqlite (default — no env var needed).

MongoDB ​

MongoDB Server
├── console_main              ← Shared/Main database
│   ├── tenants               ← Tenant registry
│   └── tenant_user_directory ← Cross-tenant email lookup
├── tenant_acme               ← Tenant "acme" database
│   ├── users
│   ├── api_tokens
│   ├── models
│   ├── providers
│   ├── vector_indexes
│   ├── guardrails
│   ├── prompts
│   ├── memory_stores
│   ├── rag_modules
│   └── ...
├── tenant_globex             ← Tenant "globex" database
│   └── ...
└── ...

Enable with: DB_PROVIDER=mongodb and set MONGODB_URI.

Note: The database abstraction layer ensures all application code works identically with both backends. Switch by changing a single environment variable.

Tenant Model ​

Each tenant has:

FieldDescription
companyNameDisplay name
slugURL-friendly identifier (unique)
dbNameDatabase name (tenant_{slug})
licenseTypeActive license tier
ownerIdOwner user ID

Projects & Members ​

Inside a tenant, work is scoped into projects and access is granted to members. Both are managed from the dashboard.

Projects (Settings → Projects) carry the active-project context that every feature reads — switching the header project pill changes the scope of models, vector stores, agents, and usage:

Projects

People are managed in one place, the Principals tab of the IAM center (Settings → IAM center): it lists the tenant's people, service accounts, groups and API tokens, each person with a role (owner, admin, project_admin, user) and a status (active, invited, disabled). Owners and admins invite people, change roles, disable or delete accounts, manage group memberships and project grants, and mint or revoke tokens from the list and from a person's drawer. People who are neither owners nor admins and hold no iam permission do not see the IAM center; they manage their own profile, password and tokens under My account.

A user created as a Programmatic User (canLogin: false, no password, email optional) shows a "No login" status and a "Programmatic" badge next to its role; see Authentication for what that flag means.

A member's role, their own service permissions, the grants of their groups, the permissions set on them inside a project and the scope of their API tokens together decide what they can do; Authorization (IAM) explains how they combine.

Removing a member from a project (DELETE /api/projects/{id}/members, or the client API equivalent) clears the membership completely, both the project membership record and the legacy project list on the user, in a single call. API tokens that user holds for the project stop working on the next request; see Project Access.

Adding people ​

Invite person on the Principals tab creates a member (with a Send invite switch that controls whether they get an email to set their own password; without it you hand over an invitation link or a one-time password) or a Programmatic User with no password. A password reset can be triggered for any person from their row, independent of the invite flow.

Bulk import ​

For onboarding many people at once, Import on the Principals tab accepts a CSV upload instead of adding rows one at a time. Failed rows are reported individually rather than aborting the whole import: the result shows how many were created, how many already existed and were skipped, and how many were invalid.

Access inside a project ​

Each member (a person, or a group assigned to the project) can carry a level per service for a project. These project service permissions only ever lower what the workspace level allows, and API tokens run under the project limits of the project they are bound to. Groups raise access at the workspace level and open the projects they are assigned to. The rules, and the custom policies that add exceptions, are in Authorization (IAM).

Database Abstraction ​

All database access goes through the abstraction layer — never import MongoDB directly:

typescript
import { getDatabase, getTenantDatabase } from '@/lib/database';

// Main database (tenant registry)
const db = await getDatabase();
const tenant = await db.findTenantBySlug('acme');

// Tenant database (user data, configs, etc.)
const tenantDb = await getTenantDatabase('tenant_acme');
const users = await tenantDb.listUsers();

switchToTenant Pattern ​

typescript
const db = await getDatabase();
await db.switchToTenant(`tenant_${slug}`);
// All subsequent queries are scoped to this tenant
const user = await db.findUserByEmail(email);

Tenant Context in Routes ​

Middleware injects tenant identity into request headers:

HeaderDescription
x-tenant-idTenant ObjectId
x-tenant-slugTenant slug
x-tenant-db-nameFull database name (tenant_acme)

Dashboard Routes ​

typescript
export const GET = withRequestContext(async (request) => {
  const tenantDbName = request.headers.get('x-tenant-db-name')!;
  const tenantId = request.headers.get('x-tenant-id')!;
  
  // All queries scoped to this tenant
  const db = await getTenantDatabase(tenantDbName);
  const models = await db.listModels();
  
  return NextResponse.json({ data: models });
});

Client API Routes ​

typescript
export async function POST(request: NextRequest) {
  const ctx = await requireApiToken(request);
  // ctx.tenantDbName, ctx.tenantId, ctx.tenantSlug — all available
  // Database already switched to tenant
}

Tenant Creation ​

When a user registers, the system:

  1. Creates a tenant record in console_main.tenants
  2. Creates a new database tenant_{slug}
  3. Creates the user as owner in the tenant database
  4. Initializes default collections and indexes
  5. Generates a JWT with tenant info embedded

Security Rules ​

  • Never create query patterns that can return cross-tenant data
  • Always call switchToTenant() before user/token operations
  • Use middleware-injected headers as the source of truth for tenant identity
  • Never trust client-supplied tenant identifiers without verification

Studio · Pulse · Console · Agent SDK and more — the Cognipeer documentation hub · Contact Support