npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mtt-sql-plugin

v1.1.0

Published

Multi-tenant SQL SDK for Node.js (TypeORM-based)

Downloads

175

Readme

mtt-sql-plugin

A plugin to manage multiple sql datasources

📘 Multi-Tenant SQL SDK – Developer Integration Guide

A lightweight SDK that enables runtime multi-tenant database handling in Node.js applications using TypeORM. Supports PostgreSQL, MySQL, and SQL Server (or any TypeORM-supported SQL database).


📦 Installation

Install the SDK:

npm install mtt-sql-plugin

Install TypeORM + relevant drivers:

PostgreSQL

npm install typeorm pg

MySQL

npm install typeorm mysql2

SQL Server

npm install typeorm mssql

🚀 Quick Start

1️⃣ Import SDK

const { tenantRegistry, tenantExecution } = require("mtt-postgres-sdk");

2️⃣ Register Tenants (Per Database)

Your application creates its own TypeORM datasource and passes it to the SDK:

const { DataSource } = require("typeorm");

app.post("/tenant/register", async (req, res) => {
  const { tenantId, host, username, password, database, port = 5432 } = req.body;

  try {
    const ds = new DataSource({
      type: "postgres",        // or: mysql, mssql
      host,
      port,
      username,
      password,
      database,
      entities: ["src/entities/**/*.js"],
      synchronize: false,      // Your application manages schema
      logging: false
    });

    await ds.initialize();

    tenantRegistry.registerTenant(tenantId, ds);

    res.send(`Tenant ${tenantId} registered`);
  } catch (err) {
    res.status(500).send(err.message);
  }
});

✔ This dynamically adds the tenant at runtime ✔ You can register unlimited tenants ✔ Each tenant has its own DB connection pool


3️⃣ Tenant-Aware DB Access

Using the SDK execution wrapper:

app.post("/tenant/:tenantId/student", async (req, res) => {
  const tenantId = req.params.tenantId;
  const { name, email } = req.body;

  try {
    const savedStudent = await tenantExecution.execute(tenantId, async (em) => {
      const repo = em.getRepository("Student");
      return await repo.save({ name, email });
    });

    res.json(savedStudent);
  } catch (err) {
    res.status(500).send(err.message);
  }
});

em (EntityManager) is automatically scoped to the tenant ✔ No need to manually switch connections ✔ No global state pollution


4️⃣ Fetch Tenant-Specific Data

app.get("/tenant/:tenantId/students", async (req, res) => {
  const tenantId = req.params.tenantId;

  try {
    const students = await tenantExecution.execute(tenantId, async (em) => {
      return await em.getRepository("Student").find();
    });

    res.json(students);
  } catch (err) {
    res.status(500).send(err.message);
  }
});

🏗 Define Your Entities

Example TypeORM entity:

const { EntitySchema } = require("typeorm");

module.exports = new EntitySchema({
  name: "Student",
  tableName: "students",
  columns: {
    id: { type: Number, primary: true, generated: true },
    name: { type: String },
    email: { type: String }
  }
});

🧠 How the SDK Works

tenantRegistry

Stores all tenant datasources:

tenantRegistry.registerTenant(id, dataSource);
tenantRegistry.getDataSource(id);
tenantRegistry.removeTenant(id);
tenantRegistry.closeAll();

tenantExecution

Executes database operations inside the correct tenant context:

tenantExecution.execute("tenant1", async (entityManager) => {
  return entityManager.query("SELECT now()");
});

Internally it:

  1. Sets tenant context
  2. Retrieves the correct datasource
  3. Creates a new EntityManager
  4. Executes your function
  5. Clears tenant context safely

🛠 Multi-DB Support

Just change datasource config:

PostgreSQL

type: "postgres"

MySQL

type: "mysql"

SQL Server

type: "mssql"

Everything else stays the same — the SDK is database-agnostic.


🧹 Removing Tenant Datasource

tenantRegistry.removeTenant("tenant5");

🧹 Shutdown All Tenants

Useful on application shutdown:

await tenantRegistry.closeAll();

🚨 Important Notes

✔ SDK does not create DB or tables — your app controls that ✔ Each tenant should have its own database (recommended) ✔ Use small pool sizes for large tenant counts ✔ Works perfectly in cluster / microservices environments


📄 Example Request Payloads

Register Tenant

{
  "tenantId": "tenant1",
  "host": "localhost",
  "username": "postgres",
  "password": "password",
  "database": "tenant1",
  "port": 5432
}

Insert Student

{
  "name": "John Doe",
  "email": "[email protected]"
}

📧 Support / Issues

Report issues on GitHub: https://github.com/expertflow/multi-tenant-sql-node-sdk (replace with actual repo URL)

Example https://github.com/expertflow/SampleMttSqlAppNode