This is a simple example of how to build a multi tenant REST API. Using knextancy with express and PostgreSQL database.
Basically it has examples for:
- Execute migration and seed tasks by tenant
- Inject a knex instance with tenant context into every express request (
req.knex
) - Execute SQL queries with multi teant support
- Setup knextancy for tests
This is how your database is gonna look like:
The tenant is defined based in the request header x-client-id
. So any request will need to include it to get it working properly.
# in case you have not installed the deps yet
docker-compose run --service-ports --rm local npm install
docker-compose run --service-ports --rm local
-
You will need to startup a PostgreSQL database
-
Configure some environment variables
- DB_PORT
tcp://host:port
(exampletcp://localhost:5432
) - DB_USER
- DB_PASSWORD
- DB_DATABASE
- DB_PORT
# in case you have not installed yet
npm install
npm start
# in case you have not installed the deps yet
docker-compose run --rm test npm install
docker-compose run --rm test
The same configuration to running the service locally.
# in case you have not installed the deps yet
npm install
npm test