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 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-tenancy

v1.2.5

Published

Making multi-tenancy easier with Node.js & typescript

Readme

Tenancy for Node.js

A package to implement multi-tenant apps in Node.js or TypeScript with ease. Inspired by Tenancy for Laravel

Table of Contents

Support

| Packages | Version | |-------------------------------------------------------------|-----------------| | typescript-eslint | 8.31.1 or later | | eslint | 9.25.1 or later | | mongodb | 6.13.1 or later | | mongoose | 8.10.1 or later | | sequelize | 6.37 or later | | sequelize-cli | 6.6 or later | | Redis | 4.7 or later | | Rabbitmq (amqplib) | 0.10.5 or later |


Install

npm i node-tenancy

Usage

Env variables

Here are some env variables to include in you .env file.

QUEUE_DRIVER=rabbitmq
DB_DRIVER=mongodb
DB_CONNECTION=mongodb://127.0.0.1:27017/database
RABBITMQ_CONNECTION=amqp://user:[email protected]

Implementation

So we will provide some steps to begin your SaaS app with ease.

1. Middlewares

Middlewares configure database connections so a request can be executed for each tenant based on domains registered to each tenant.

  • Tenancy Middleware should be used in tenancy tenantRoute.js.
const express = require('express');
const router = express.Router();
const tenancy = require('node-tenancy');

router.use(tenancy.initializeTenancyMiddleware);

router.get('/get', function (Request, Response) {
  return Response.status(200).json("Hello");
});
  • Central Middleware should be used in tenancy centralRoutes.js.
const express = require('express');
const router = express.Router();
const tenancy = require('node-tenancy');

router.use(tenancy.initializeCentralMiddleware);

router.get('/get', function (Request, Response) {
  return Response.status(200).json({
    'tenant_id': tenancy.config.getConfig().tenant_id
  });
});

2. Queue connection

Check out Rabbitmq guide to know more.

Check out Redis guide to know more.


3. Using Mongoose

Please read Mongoose guide to know in detail mongoose implementation.

4. Using SQL (with sequelize)

To make it more versatile, we have added sequelize, which supports multiple relational databases. Read more about it here Sequelize guide.

Column names cannot be changed:

Tenant table/collection:

db_connection, db_name, db_options

Domain table: domain

In case you are using mongodb, we assume that domains are an array inside tenants collection

TypeScript Support

See Bun Example here

Tenancy now includes TypeScript type definitions. Example usage:

import {config, TenantSchema, db} from 'node-tenancy';
import {Schema} from 'mongoose';

// Define schemas with TypeScript types
interface User {
    username: string;
    email: string;
    active: boolean;
    createdAt: Date;
}

const userSchema = new Schema<User>({
    username: String,
    email: {type: String, required: true},
    active: {type: Boolean, default: true},
    createdAt: {type: Date, default: Date.now}
});

// Configure tenancy
config.setConfig({
    central_domains: ["admin.myapp.com"],
    tenant_schemas: {
        "User": userSchema
    },
    central_schemas: {
        "Tenant": TenantSchema
    }
});