From 03086cdd86aace06c54a56de58f553c69ef8f4e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 23 Oct 2014 13:38:02 +0200 Subject: [PATCH] 18 store feature when featureCreated-event is emitted --- unleash-server/lib/featureDb.js | 23 +++++++++++++++++-- .../migrations/sql/001-initial-schema.up.sql | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/unleash-server/lib/featureDb.js b/unleash-server/lib/featureDb.js index 282ec66ec7..522146f011 100644 --- a/unleash-server/lib/featureDb.js +++ b/unleash-server/lib/featureDb.js @@ -2,10 +2,29 @@ var eventStore = require('./eventStore'), eventType = require('./eventType'), featuresMock = require('./featuresMock'), logger = require('./logger'), - Promise = require('bluebird'); + 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) { - logger.info('feature created event recieved', event); + var sql = 'INSERT INTO features(name, enabled, strategy_name, parameters) VALUES ($1, $2, $3, $4)'; + var params = [ + event.data.name, + event.data.enabled ? 1 : 0, + event.data.strategy, + event.data.parameters + ]; + + dbPool.query(sql, params, function(err) { + if(err) { + logger.error('Could not insert feature, error was: ', err); + } + }); } ); diff --git a/unleash-server/migrations/sql/001-initial-schema.up.sql b/unleash-server/migrations/sql/001-initial-schema.up.sql index a4d6e72056..5af3577f36 100644 --- a/unleash-server/migrations/sql/001-initial-schema.up.sql +++ b/unleash-server/migrations/sql/001-initial-schema.up.sql @@ -8,7 +8,7 @@ CREATE TABLE features ( created_at timestamp default now(), name varchar(255) PRIMARY KEY NOT NULL, enabled integer default 0, - strategy_name varchar(255) references strategies(name), + strategy_name varchar(255), parameters json );