Skip to content
forked from johnabil/tenancy

Automatic multi-tenancy for Node js. We make it ASAP ( as simple as possible ).

License

Notifications You must be signed in to change notification settings

aymann90/tenancy

 
 

Repository files navigation

Tenancy for Node.js

a package to implement multi tenant apps in node with ease. Trying to make it like Tenancy for Laravel

Table of Contents

Support

Packages Version
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:password@127.0.0.1

Implementation

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

1. Middlewares

Middlewares configure database connections so 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 can not be changed:

Tenant table/collection:

db_connection, db_name, db_options

Domain table: domain

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

TypeScript Support

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
    }
});

About

Automatic multi-tenancy for Node js. We make it ASAP ( as simple as possible ).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%