dipswitch
Dipswitch is a multi-tenant feature toggle library.
Installation (via npm)
$ npm install dipswitch --save
If you wish to use redis to store this data, also install dipswitch-redis. Otherwise, an in-memory store will be used.
Usage
The Basics
var Dipswitch = require('dipswitch'),
dipswitch = new Dipswitch({
redis: require('redis').createClient(),
getTenantId: function (tenant) {
return tenant.tenantId;
},
getUserId: function (user) {
return user.userId;
},
getUserGroups: function (user) {
if (user.isAdmin) {
return ['admins'];
}
return [];
}
});
dipswitch.features.add('EXAMPLE_FEATURE').then(function () { });
dipswitch.features.remove('EXAMPLE_FEATURE').then(function () { });
dipswitch.features.add(['FEATURE_1', 'FEATURE_2']).then(function () { });
dipswitch.features.remove(['EXAMPLE_FEATURE1', 'EXAMPLE_FEATURE2']).then(function () { });
dipswitch.features.find().then(function (features) {
});
dipswitch.features.enable('EXAMPLE_FEATURE').then(function () { });
dipswitch.tenant(tenant).enable('EXAMPLE_FEATURE').then(function () { });
dipswitch.tenant(tenant).group('admins').enable('EXAMPLE_FEATURE').then(function () { });
dipswitch.group('admins').enable('EXAMPLE_FEATURE').then(function () { });
dipswitch.tenant(tenant).user(user).enable('EXAMPLE_FEATURE').then(function () { });
dipswitch.user(user).enable('EXAMPLE_FEATURE').then(function () { });
dipswitch.features.enable('LOL_OMG_CATFACTS', {
start: new Date(2015, 3, 1),
end: new Date(2015, 3, 2)
}).then(function () { });
dipswitch.tenant(tenant).user(user).features().then(function (features) {
console.log(features.EXAMPLE_FEATURE);
});
dipswitch.tenant(tenant).user(user).feature('EXAMPLE_FEATURE').then(function (isEnabled) {
console.log(isEnabled);
});
dipswitch.tenant(tenant).user(user).disable('EXAMPLE_FEATURE').then(function () { });
Unregistering Out-of-Date Features
Rather than having to have an admin UI for unregistering features you no longer care about, you can use a bit of underscore
/lodash
magic to automate the process:
var _ = require('underscore'),
Dipswitch = require('dipswitch'),
dipswitch = new Dipswitch(options);
var desiredFeatures = ['FEATURE_1', 'FEATURE_3'];
dipswitch.register(desiredFeatures)
.then(function () {
return dipswitch.features();
})
.then(function (registeredFeatures) {
var outOfDateFeatures = _.without(registeredFeatures, desiredFeatures);
return dipswitch.unregister(outOfDateFeatures);
});
Interacting With Unregistered Features
When enabling or disabling a feature that has never been registered, that feature is automatically registered. When testing to see the value of a feature that isn't registered, the result will be false
.
License
MIT License
Author
Lanetix (engineering@lanetix.com)