@node-tenant/tenant-storage-prisma
v0.1.2
Published
Prisma storage adapter for @node-tenant/tenant
Downloads
198
Maintainers
Keywords
Readme
@node-tenant/tenant-storage-prisma
Prisma storage adapter for the
@node-tenantecosystem — manages tenant metadata, relationships, and configuration using Prisma ORM.🌐 Website: node-tenant.com
Overview
@node-tenant/tenant-storage-prisma implements the TenantStorage interface from @node-tenant/tenant-core. It acts as the "Control Plane", storing your tenants, their configurations, statuses, and cross-tenant relationships.
Note: This package does not handle tenant data isolation (that's the job of the DatabaseAdapter or Mongoose routers). This package only manages the list of tenants themselves.
Table of Contents
Installation
npm install @node-tenant/tenant-core @node-tenant/tenant-storage-prisma @prisma/clientPeer dependency:
@prisma/client >= 5.0.0must be installed in your project.
Prisma Schema
This package requires a specific set of models in your schema.prisma file. A pre-defined schema is included in this package at node_modules/@node-tenant/tenant-storage-prisma/schema.prisma.
You can manually copy these models into your own schema.prisma:
model Tenant {
id String @id @default(uuid())
slug String @unique
name String
status String // e.g., 'active', 'suspended', 'inactive'
strategy String // e.g., 'tenantId', 'database', 'schema'
config Json? @default("{}")
organizationId String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model TenantRelationship {
id String @id @default(uuid())
sourceTenantId String
targetTenantId String
type String // e.g., 'partner', 'parent_child'
metadata Json? @default("{}")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([sourceTenantId, targetTenantId, type])
}
model TenantShare {
id String @id @default(uuid())
resourceType String
resourceId String
ownerTenantId String
targetTenantId String
permission String // e.g., 'read', 'write', 'admin'
metadata Json? @default("{}")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([resourceType, resourceId, ownerTenantId, targetTenantId])
}After adding these to your schema, run:
npx prisma generate
npx prisma db push # Or create a migrationQuick Start
import { PrismaClient } from '@prisma/client';
import { TenantManager } from '@node-tenant/tenant-core';
import { createPrismaStorage } from '@node-tenant/tenant-storage-prisma';
// 1. Initialize your Prisma Client
const prisma = new PrismaClient();
// 2. Create the Storage Adapter
const storage = createPrismaStorage(prisma);
// 3. Pass to the TenantManager
const manager = new TenantManager({ storage });
// Now you can manage tenants!
async function setup() {
const tenant = await manager.createTenant({
slug: 'acme-corp',
name: 'Acme Corporation',
status: 'active',
strategy: 'tenantId',
});
console.log('Created tenant:', tenant);
}API Reference
createPrismaStorage(prismaClient)
Creates a TenantStorage compatible adapter.
import { createPrismaStorage } from '@node-tenant/tenant-storage-prisma';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
const storage = createPrismaStorage(prisma);| Parameter | Type | Required | Description |
|---|---|---|---|
| prismaClient | PrismaClient | ✅ | An initialized PrismaClient instance |
Returns: TenantStorage object containing all required methods (getTenantById, getTenantBySlug, listTenants, createTenant, updateTenant, deleteTenant, createRelationship, etc.)
License
MIT © Anupam Vishwakarma
