Registry / database / sails-hook-multitenant

sails-hook-multitenant

JSON →
library0.6.2jsnpmunverified

A Sails.js 1.x hook that transforms Waterline ORM into a multitenant ORM by providing per-request datasource switching. Current stable version is 0.6.2. It is plug-and-play, non-invasive, and compatible with existing Sails models when models have `multitenant: true`. The hook supports dynamic tenant selection via a custom function that returns datasource connection details (host, port, adapter, etc.). It is maintained but has low activity; last release was in 2020. Key differentiator: full backward compatibility with standard Waterline operations, no need to rewrite model code.

npm install sails-hook-multitenant
INSTALL
IMPORT
SIG · SAILS-HOOK-MULTITE
S
sails-hook-multitenant
databasejavascriptv0.6.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sails-hook-multitenant
npm install sails-hook-multitenant --save
npm install sails-hook-multitenant --no-save
Install as a production dependency; the hook auto-registers when Sails lifts.
multitenancy config function
module.exports.multitenancy = function(req) { return Promise.resolve(datasource); }
module.exports['sails-hook-multitenant'] = ...
Define a function in config/tenancy.js (or any config file) that returns a promise with the datasource object.
datasource constructor
const Datasource = require('sails-hook-multitenant/datasource'); const ds = new Datasource(host, port, schema, adapter, user, password, database, identity);
const Datasource = require('sails-hook-multitenant').Datasource;
The Datasource class is exported from a subpath, not the main module. Available in version 0.6.x.
Model multitenant property
module.exports = { attributes: {}, multitenant: true };
module.exports = { multitenant: 'yes' };
Set `multitenant: true` in the model definition. It must be a boolean, not a string.

Shows how to configure a tenant selector function and mark a model as multitenant to enable per-request datasource switching.

// config/tenancy.js module.exports.multitenancy = function(req) { const Datasource = require('sails-hook-multitenant/datasource'); return new Promise((resolve, reject) => { // Example: fetch tenant info from request or database const tenantId = req.headers['x-tenant-id'] || 'default'; // In production, look up tenant credentials from a secure store const datasource = new Datasource( 'localhost', // host 3306, // port false, // schema 'sails-mysql', // adapter 'tenant_user', // user process.env.TENANT_PASSWORD ?? '', // password `tenant_${tenantId}`, // database `tenant_${tenantId}` // identity ); resolve(datasource); }); }; // api/models/User.js module.exports = { attributes: { name: { type: 'string', required: true }, email: { type: 'string', required: true } }, multitenant: true };
Debug
Known issues
gotchaThe hook modifies Waterline's default behavior for models with `multitenant: true`; models without this flag remain unaffected and use the default datasource.
fix
Ensure all multitenant models have `multitenant: true` in their definition.
affects: <0.6.0
breakingThe `datasource` property names changed in v0.6.0; `databaseName` was renamed to `database` and `identity` was added.
fix
Use `host`, `port`, `schema`, `adapter`, `user`, `password`, `database`, `identity` in the datasource object.
affects: >=0.6.0
deprecatedPassing a plain object to the multitenancy function without using the Datasource constructor is deprecated in favor of the Datasource class.
fix
Use `const Datasource = require('sails-hook-multitenant/datasource');` and create a new instance.
affects: >=0.6.0
gotchaThe multitenancy function must return a Promise. If it returns undefined or synchronously, the hook will throw.
fix
Wrap your tenant selection logic in `new Promise((resolve) => { ... resolve(datasource); })`.
affects: >=0.6.0
gotchaIf the multitenancy function is not defined in any config file, the hook falls back to using the default Sails datasource, potentially causing data leakage between tenants.
fix
Always define a multitenancy function in config/. Create e.g. config/tenancy.js with the required function.
affects: >=0.6.0
Errors
Common errors & fixes
Error: Unknown model multitenant property
Sails does not recognize `multitenant` as a valid model property by default.
fix
Ensure the hook is properly installed and the model has `multitenant: true` (not string). The hook reads this property at lift time.
TypeError: datasource is not a function
The multitenancy function did not return a valid datasource object or returned a non-object.
fix
Return a plain object with the required keys or use `new Datasource(...)`. Ensure function returns the value inside the Promise.
Error: Can't find module 'sails-hook-multitenant/datasource'
The Datasource module is not exported in older versions or the path is incorrect.
fix
Upgrade to v0.6.2. Use `require('sails-hook-multitenant/datasource')` (with a forward slash, not a dot).
Error: Hook loader error: hook is not a function
The module was improperly required or installed with an incompatible version.
fix
Install with `npm install sails-hook-multitenant@0.6.2 --save`. Remove any previous versions and reinstall.
Upgrade
Version history
0.6.2latest on npm
Audit
Dependencies
sailsrequiredRuntime dependency: the hook requires Sails.js 1.x to run
waterlinerequiredRuntime dependency: the hook extends Waterline ORM's behavior
Agent activity
13 hits · last 30 days
node
10
Meta
2
Resources
sails-hook-multitenant — npm install sails-hook-multitenant · libregistry