CosmosDB Service Module

Last updated: 01/20/2026
About this sample

This reference documents a data access layer for a multi-tenant SaaS platform, written for backend developers integrating with the system. It covers 60+ methods across 11 service domains — organizations, teams, projects, participants, documents, synthesis, feedback, billing, authentication, and more — structured from source code analysis and organized by domain rather than alphabetically.

cosmosService.js is a multi-tenant data access layer for Azure CosmosDB (NoSQL API). It provides structured APIs for organizations, teams, projects, research data, synthesis, documents, billing, and user feedback within the InsightHub platform.

Use this module to read and write all application data. It abstracts CosmosDB container operations behind typed service objects and exports utility functions for authentication, plan enforcement, and usage tracking.

Container architecture

ContainerPartition keyStores
research/projectIdProjects, participants, documents, synthesis data, feedback, survey templates
organizations/idOrganization settings, plans, billing, usage
teams/organizationIdTeams, team members, org members, invitations

Dependencies

DependencyPurpose
@azure/cosmosCosmosDB client
cryptoSecure invitation token generation
./emailServiceInvitation and welcome emails (lazy-loaded)
./shared/planLimitsPlan configuration and limit helpers

Environment variables

VariableRequiredDefaultDescription
COSMOS_DB_ENDPOINTYesCosmosDB account endpoint URL
COSMOS_DB_KEYYesCosmosDB account key
COSMOS_DB_DATABASENo"insighthub"Database name
COSMOS_DB_CONTAINERNo"research"Research container name
COSMOS_DB_ORGANIZATIONS_CONTAINERNo"organizations"Organizations container name
COSMOS_DB_TEAMS_CONTAINERNo"teams"Teams container name
SKIP_AUTHNoSet to "true" to bypass authentication in local development
DEV_TENANT_IDNo"local-dev-tenant"Tenant ID used when SKIP_AUTH is "true"
DEV_USER_IDNo"local-dev-user"User ID used when SKIP_AUTH is "true"
DEV_USER_EMAILNo"dev@localhost"User email used when SKIP_AUTH is "true"
DEV_ORG_IDNo"dev-org-001"Organization ID used when SKIP_AUTH is "true"

Usage

const {
  organizations,
  teams,
  projects,
  participants,
  validateOrgAccess,
  checkLimit,
  initializeDatabase
} = require('./cosmosService');

// Initialize containers before first use
await initializeDatabase();