diff --git a/unleash-server/lib/featureDb.js b/unleash-server/lib/featureDb.js index 522146f011..f429028669 100644 --- a/unleash-server/lib/featureDb.js +++ b/unleash-server/lib/featureDb.js @@ -1,15 +1,8 @@ var eventStore = require('./eventStore'), eventType = require('./eventType'), - featuresMock = require('./featuresMock'), logger = require('./logger'), Promise = require('bluebird'), dbPool = require('./dbPool'); -/* - name varchar(255) PRIMARY KEY NOT NULL, - enabled integer default 0, - strategy_name varchar(255) references strategies(name), - parameters json - */ eventStore.on(eventType.featureCreated, function (event) { var sql = 'INSERT INTO features(name, enabled, strategy_name, parameters) VALUES ($1, $2, $3, $4)'; @@ -29,7 +22,22 @@ eventStore.on(eventType.featureCreated, function (event) { ); function getFeatures() { - return Promise.resolve(featuresMock); + var sql = 'SELECT name, enabled, strategy_name as strategy, parameters FROM features ORDER BY created_at'; + return new Promise(function (resolve, reject) { + dbPool.query(sql, function(err, res) { + if(err) {reject(err);} + resolve(res.rows.map(mapToToggle)); + }); + }); +} + +function mapToToggle(row) { + return { + name: row.name, + enabled: row.enabled > 0, + strategy: row.strategy, + parameters: row.parameters + }; } module.exports = { diff --git a/unleash-server/test/featureApiSpec.js b/unleash-server/test/featureApiSpec.js index fa4b572278..8554518b76 100644 --- a/unleash-server/test/featureApiSpec.js +++ b/unleash-server/test/featureApiSpec.js @@ -14,6 +14,7 @@ describe('The api', function () { }); mockery.registerSubstitute('./eventDb', '../test/eventDbMock'); + mockery.registerSubstitute('./featureDb', '../test/featureDbMock'); server = require('../server'); diff --git a/unleash-server/test/featureDbMock.js b/unleash-server/test/featureDbMock.js new file mode 100644 index 0000000000..7b9548aa21 --- /dev/null +++ b/unleash-server/test/featureDbMock.js @@ -0,0 +1,36 @@ +var Promise = require("bluebird"); + +var features = [ + { + "name": "featureX", + "description": "the #1 feature", + "enabled": true, + "strategy": "default" + }, + { + "name": "featureY", + "description": "soon to be the #1 feature", + "enabled": false, + "strategy": "baz", + "parameters": { + "foo": "bar" + } + }, + { + "name": "featureZ", + "description": "terrible feature", + "enabled": true, + "strategy": "baz", + "parameters": { + "foo": "rab" + } + } +]; + +module.exports = { + getFeatures: function() { + return new Promise(function (resolve) { + resolve(features); + }); + } +}; \ No newline at end of file