# knex-tenanty

> Multi tenant knex middleware. Allows for multiple clients using the same application saving data in different schemas.

Latest version **0.0.1-beta.1** (published 2017-10-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install knex-tenanty
pnpm add knex-tenanty
yarn add knex-tenanty
bun add knex-tenanty
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1-beta.1 |
| Published | 2017-10-04 |
| First published | 2017-10-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >8.0.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Bruno Leonardo Michels |
| Maintainers | brunolm |
| Keywords | knex, multi-tenant, tenant |

## Links

- npm: https://www.npmjs.com/package/knex-tenanty
- Repository: https://github.com/brunolm/knex-tenanty
- Homepage: https://github.com/brunolm/knex-tenanty#readme
- Issues: https://github.com/brunolm/knex-tenanty/issues
- npm.io page: https://npm.io/package/knex-tenanty

## Dependencies (2)

- [knex](https://npm.io/package/knex.md) ^0.13.0
- [lru-cache](https://npm.io/package/lru-cache.md) ^4.1.1

## Recent versions

- 0.0.1-beta.1 (latest) — 2017-10-04
- 0.0.1-beta — 2017-10-04

## README

# knex-tenanty

[![Build Status](https://travis-ci.org/brunolm/knex-tenanty.svg?branch=master)](https://travis-ci.org/brunolm/knex-tenanty)

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

```ts
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

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

---
_Source: https://npm.io/package/knex-tenanty · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
