Filtering Connections in PostGraphile by Tenants
This plugin targets the beta release of PostGraphile v4.
Bug reports and pull requests are very much welcome.
The main drawback of Multi Tenant with shared-tables is the need to append the tenant filter condition onto every single query in the application layer.
One possible way to isolate tenants is to add tenant_id
to every table, and scope every request with that field.
This plugin will filter tables (Connections) by looking for table with column name specified by tenantColumnName
at graphileBuildOptions.
Currently, the collections will be filtered by using tenant_id
(stored in jwt).
Add the tenant_id
in your schema, i.e :
create type store_example.jwt_token as (
tenant_id text
role text,
person_id integer
);
- optional tables autofilter by
connectionMultiTenantAllowedTables
graphileBuildOptions. - add test
postgraphile --append-plugins `pwd`/path/to/this/plugin/index.js
const express = require("express");
const { postgraphile } = require("postgraphile");
const PostGraphileConnectionMultiTenantPlugin = require("postgraphile-plugin-connection-multi-tenant");
const app = express();
app.use(
postgraphile(pgConfig, schema, {
graphiql: true,
appendPlugins: [PostGraphileConnectionMultiTenantPlugin],
})
);
app.listen(5000);
When using PostGraphile as a library, the following plugin options can be passed via graphileBuildOptions
:
Target column :
postgraphile(pgConfig, schema, {
...
graphileBuildOptions: {
tenantColumnName: 'tenantId',
},
})