Prisma Storage Adapter

A high-performance Control Plane storage layer powered entirely by Prisma ORM. Store and manage your tenants, domains, and organizations using your existing Prisma Client.

@node-tenant/tenant-storage-prisma

Overview

What is the Control Plane? In a multi-tenant system, you have two types of data: the data belonging to your customers (like their user accounts or posts), and the "master list" of who your customers actually are. This master list is called the Control Plane.

What does this package do? The central TenantManager needs to know how to save and read this master list. If you are using Prisma ORM to talk to your database, you install this adapter. It acts as a translator between the TenantManager and your Prisma Client.

Real-world Analogy

Imagine the TenantManager is a librarian who needs to look up which person borrowed which book. But the librarian only speaks English, and the filing cabinet (your database) is written in Spanish. This tenant-storage-prisma adapter is the Translator standing between them!

Tip: This package does not secure your customers' data. It is only used for saving the list of customers themselves. For securing user data, see the tenant-prisma package.

Installation

Install this package alongside your existing Prisma setup. You must also have @prisma/client installed.

npm install @node-tenant/tenant-storage-prisma

Detailed Setup Example

How to use it: You simply initialize your standard Prisma Client, pass it through the createPrismaStorage function, and hand the result to your TenantManager.

import { PrismaClient } from '@prisma/client';
import { TenantManager } from '@node-tenant/tenant-core';
// Import the Prisma storage adapter
import { createPrismaStorage } from '@node-tenant/tenant-storage-prisma';

// 1. Initialize your normal Prisma Client
const prisma = new PrismaClient();

async function startServer() {
  await prisma.$connect();
  
  // 2. Wrap your Prisma client so the Manager can understand it
  const controlPlaneStorage = createPrismaStorage(prisma);

  // 3. Give it to the TenantManager
  const tenantManager = new TenantManager({
    storage: controlPlaneStorage,
  });

  // Test it out!
  // Behind the scenes, this will run: prisma.tenant.findMany()
  const allCustomers = await tenantManager.getAllTenants();
  console.log(`You currently have ${allCustomers.length} customers.`);
}

startServer().catch(console.error);

Line-by-Line Breakdown

  • Line 8: We create a standard PrismaClient. This connects to your database normally.
  • Line 14: createPrismaStorage takes your client and wraps it in a standardized interface that the core system requires.
  • Line 17: The TenantManager now knows how to save things to Prisma!
  • Line 23: Because they are connected, asking the manager for tenants will automatically execute the correct Prisma SQL queries for you.