This repository demonstrates two different approaches to implementing multi-tenancy in a Node.js/Express application using PostgreSQL and Prisma ORM. It provides working examples of both shared database with separate schemas and shared database with shared schema architectures.
Multi-tenancy is an architecture where a single instance of software serves multiple tenants (customers/organizations). This project showcases two common approaches:
In this approach:
- Each tenant gets their own dedicated PostgreSQL schema
- Data is physically isolated at the schema level
- Stronger data isolation and security
- More complex to manage but offers better customization per tenant
- Easier to backup/restore individual tenant data
- Better for compliance requirements where data isolation is crucial
In this approach:
- All tenants share the same database schema
- Data is logically isolated using a
tenantIdcolumn - Simpler to manage and maintain
- More efficient resource utilization
- Easier to implement and update
- Better for scenarios where strict data isolation isn't required
| Feature | Separate Schemas | Shared Schema |
|---|---|---|
| Data Isolation | Physical (schema-level) | Logical (tenantId) |
| Resource Usage | Higher (separate schemas) | Lower (shared tables) |
| Maintenance | More complex | Simpler |
| Customization | Easier per tenant | Limited |
| Backup/Restore | Per tenant possible | All tenants together |
| Performance | May vary per tenant | Consistent across tenants |
| Security | Stronger isolation | Requires careful filtering |
.
├── docker/ # Docker configuration files
│ └── postgres/ # PostgreSQL initialization scripts
├── media/ # Architecture diagrams
├── shared-db-seperate-schema/ # Separate schema implementation
└── shared-db-shared-schema/ # Shared schema implementation
- Node.js v18+
- PostgreSQL 13+
- Docker & Docker Compose (for local development)
- For Separate Schema Architecture:
docker compose --profile express-seperate-schema upTo stop:
docker compose --profile express-seperate-schema down- For Shared Schema Architecture:
docker compose --profile express-shared-schema upTo stop:
docker compose --profile express-shared-schema down-
Superadmin Management
- Create and list tenants
- Create users for tenants
- Secured with admin token
-
Tenant Authentication
- JWT-based authentication
- Secure login endpoints
- Role-based access control
-
Contact Management
- CRUD operations for contacts
- Tenant-specific data isolation
- Authenticated endpoints
- Strict data isolation is required
- Compliance requirements demand physical separation
- Tenants need schema-level customization
- Individual tenant backup/restore is important
- Resources allow for multiple schemas
- Simple multi-tenancy is needed
- Resource optimization is priority
- Maintenance simplicity is important
- Strict data isolation isn't required
- Rapid deployment and updates are needed
Feel free to submit issues, fork the repository, and create pull requests for any improvements.
This project is licensed under the MIT License - see the LICENSE file for details.

