From a1b0deaba01d0226b28c65c46f5eb4e121070493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 23 Oct 2014 10:32:13 +0200 Subject: [PATCH] Fixed failing validations --- unleash-server/README.md | 12 ++++++++++++ unleash-server/lib/eventDb.js | 19 +++++++++++++++++++ unleash-server/lib/eventStore.js | 11 +---------- unleash-server/lib/featureApi.js | 6 +++++- unleash-server/test/eventStoreSpec.js | 6 +++++- 5 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 unleash-server/lib/eventDb.js diff --git a/unleash-server/README.md b/unleash-server/README.md index 359a20c8d7..c2b6a1c288 100644 --- a/unleash-server/README.md +++ b/unleash-server/README.md @@ -28,3 +28,15 @@ npm test 3. Run `db-migrate create your-migration-name` and edit the generated file to run the above SQL files. 4. Run `npm run db-migrate-up`. 5. Generate LB artifact using `scripts/generate-liquibase-artifact` (TODO: make this internal) + +### Create a local unleash-db on postgres +´´´sql +CREATE USER unleash_user WITH PASSWORD 'passord'; +CREATE DATABASE unleash; +GRANT ALL PRIVILEGES ON DATABASE unleash to unleash_user; +´´´ + +Then set up your DATABASE_URI env.var: +``` +export DATABASE_URL=postgres://unleash_user:passord@localhost:5432/unleash +``` diff --git a/unleash-server/lib/eventDb.js b/unleash-server/lib/eventDb.js new file mode 100644 index 0000000000..e89f9d5587 --- /dev/null +++ b/unleash-server/lib/eventDb.js @@ -0,0 +1,19 @@ +var Promise = require('bluebird'), + dbPool = require('./dbPool'); + +function storeEvent(event) { + var sql = 'INSERT INTO events(type, user, data) VALUES ($1, $2, $3)'; + var params = [event.type,event.user,event.data]; + + return new Promise(function (resolve, reject) { + dbPool.query(sql, params, function (err) { + if (err) {reject(err);} + resolve(); + }); + }); +} + + +module.exports = { + store: storeEvent() +}; \ No newline at end of file diff --git a/unleash-server/lib/eventStore.js b/unleash-server/lib/eventStore.js index a0cecaef70..846fe293f0 100644 --- a/unleash-server/lib/eventStore.js +++ b/unleash-server/lib/eventStore.js @@ -1,6 +1,5 @@ var Promise = require('bluebird'), util = require('util'), - dbPool = require('./dbPool'), EventEmitter = require('events').EventEmitter; function EventStore() { @@ -8,15 +7,7 @@ function EventStore() { } util.inherits(EventStore, EventEmitter); -EventStore.prototype.create = function (eventType, user, eventData) { - var event = { - id: 1, - created: '2014-08-01 12:22:00', - type: eventType, - user: user, - data: eventData - }; - +EventStore.prototype.create = function (event) { return new Promise(function (resolve) { this.emit(event.type, event); resolve(); diff --git a/unleash-server/lib/featureApi.js b/unleash-server/lib/featureApi.js index 5a80b2d6e5..884810a64b 100644 --- a/unleash-server/lib/featureApi.js +++ b/unleash-server/lib/featureApi.js @@ -30,7 +30,11 @@ module.exports = function (app) { //Todo: error-msg: feature name is already in use res.status(403).end(); } else { - eventStore.create(eventType.featureCreated, user, newFeature).then(function() { + eventStore.create({ + type: eventType.featureCreated, + user: user, + data: newFeature + }).then(function() { res.status(201).end(); }); } diff --git a/unleash-server/test/eventStoreSpec.js b/unleash-server/test/eventStoreSpec.js index 512273920b..aaf5b04b3c 100644 --- a/unleash-server/test/eventStoreSpec.js +++ b/unleash-server/test/eventStoreSpec.js @@ -18,7 +18,11 @@ describe('EventStore', function () { 'description': 'Feature description' }; - eventStore.create(eventType.featureCreated,'ole',eventData); + eventStore.create({ + type: eventType.featureCreated, + user: 'ole', + data: eventData + }); }); }); });