Get unlimited public & private packages + team-based management with npm Teams.Learn more »

knex-tenanty

0.0.1-beta.1 • Public • Published

knex-tenanty

Build Status

Knex middleware to handle multiple tenants.

You can run your application for several clients and the database will be created in different schemas.

Example usage

KnexTenanty(
  /* knex config object */,
  /* function to extract client-id from request object */
);

Sample project

https://github.com/brunolm/knex-mt

Setting up the middleware

import * as express from 'express';
 
import { KnexTenanty } from 'knex-tenanty';
import knexSettings from '../knexfile';
 
const app = express.Router();
 
const settings = knexSettings[process.env.NODE_ENV];
const extractClientId = (req) => req.headers['x-client-id'];
 
const middleware = KnexTenanty(settings, extractClientId);
app.use(middleware);
 
export default app;

Using knex to access the database

app.get('/', async (req, res) => {
  console.log('route /');
  const knex = req['knex'] as Knex.QueryBuilder;
 
  const values = await knex.from('test');
  res.send(values);
});
 
app.get('/insert', async (req, res) => {
  console.log('route /insert');
  const knex = req['knex'] as Knex.QueryBuilder;
 
  const values = await knex.insert({ name: req.query.name || 'bruno' }).into('test');
  res.send(values);
});

Install

npm i knex-tenanty

DownloadsWeekly Downloads

1

Version

0.0.1-beta.1

License

ISC

Last publish

Collaborators

  • avatar