From 17daa4519ab63c3dd95b58d7636666fe249d7d75 Mon Sep 17 00:00:00 2001 From: Muhammed Thanish Date: Wed, 31 Aug 2016 13:37:05 +0530 Subject: [PATCH 1/6] Update storybook-addons API From v1.5 it include get/set Database methods --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 587e0b92fd84..4cb1a8e57939 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "@kadira/react-split-pane": "^1.4.0", "@kadira/storybook-addon-actions": "^1.0.2", "@kadira/storybook-addon-links": "^1.0.0", - "@kadira/storybook-addons": "^1.3.1", + "@kadira/storybook-addons": "^1.5.0", "@kadira/storybook-channel-pagebus": "^2.0.2", "@kadira/storybook-ui": "^3.3.2", "autoprefixer": "^6.3.7", From 00db2073e83aab64bb9e12be890f15aec9af779e Mon Sep 17 00:00:00 2001 From: Muhammed Thanish Date: Wed, 31 Aug 2016 17:43:17 +0530 Subject: [PATCH 2/6] Implement database server --- dist/server/index.js | 11 +- dist/server/middleware/datastore.js | 133 ++++++++++++++++++ .../storybook.js} | 10 +- package.json | 2 + src/server/index.js | 4 +- src/server/middleware/datastore.js | 76 ++++++++++ .../storybook.js} | 10 +- 7 files changed, 232 insertions(+), 14 deletions(-) create mode 100644 dist/server/middleware/datastore.js rename dist/server/{middleware.js => middleware/storybook.js} (90%) create mode 100644 src/server/middleware/datastore.js rename src/server/{middleware.js => middleware/storybook.js} (84%) diff --git a/dist/server/index.js b/dist/server/index.js index ad137667aeb5..6e3eaf1d512e 100755 --- a/dist/server/index.js +++ b/dist/server/index.js @@ -17,9 +17,13 @@ var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); -var _middleware = require('./middleware'); +var _storybook = require('./middleware/storybook'); -var _middleware2 = _interopRequireDefault(_middleware); +var _storybook2 = _interopRequireDefault(_storybook); + +var _datastore = require('./middleware/datastore'); + +var _datastore2 = _interopRequireDefault(_datastore); var _package = require('../../package.json'); @@ -79,7 +83,8 @@ if (_commander2.default.staticDir) { // Build the webpack configuration using the `baseConfig` // custom `.babelrc` file and `webpack.config.js` files var configDir = _commander2.default.configDir || './.storybook'; -app.use((0, _middleware2.default)(configDir)); +app.use((0, _storybook2.default)(configDir)); +app.use('/db', (0, _datastore2.default)(configDir)); app.listen.apply(app, listenAddr.concat([function (error) { if (error) { diff --git a/dist/server/middleware/datastore.js b/dist/server/middleware/datastore.js new file mode 100644 index 000000000000..57efddb1c2d6 --- /dev/null +++ b/dist/server/middleware/datastore.js @@ -0,0 +1,133 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Database = undefined; + +var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); + +var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); + +var _keys = require('babel-runtime/core-js/object/keys'); + +var _keys2 = _interopRequireDefault(_keys); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +exports.default = function (configDir) { + var dbPath = _path2.default.resolve(configDir, 'datastore.json'); + var db = new Database(dbPath); + + var router = new _express.Router(); + router.use(_bodyParser2.default.json()); + + router.post('/get', function (req, res) { + var _req$body = req.body; + var collection = _req$body.collection; + var query = _req$body.query; + var sort = _req$body.sort; + var limit = _req$body.limit; + + var out = db.get(collection, query, sort, limit); + res.send({ data: out }); + res.end(); + }); + + router.post('/set', function (req, res) { + var _req$body2 = req.body; + var collection = _req$body2.collection; + var item = _req$body2.item; + + var out = db.set(collection, item); + res.send({ data: out }); + res.end(); + }); + + return router; +}; + +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +var _express = require('express'); + +var _lowdb = require('lowdb'); + +var _lowdb2 = _interopRequireDefault(_lowdb); + +var _fileAsync = require('lowdb/lib/file-async'); + +var _fileAsync2 = _interopRequireDefault(_fileAsync); + +var _bodyParser = require('body-parser'); + +var _bodyParser2 = _interopRequireDefault(_bodyParser); + +var _uuid = require('uuid'); + +var _uuid2 = _interopRequireDefault(_uuid); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var Database = exports.Database = function () { + function Database(dbPath) { + (0, _classCallCheck3.default)(this, Database); + + this.db = (0, _lowdb2.default)(dbPath, { storage: _fileAsync2.default }); + } + + (0, _createClass3.default)(Database, [{ + key: 'get', + value: function get(collection, query, sort, limit) { + // if the database doesn't exist, add the document + // and return the inserted document as the result. + if (!this.db.has(collection).value()) { + return []; + } + // If the sort param is not given, use the DB interface + if (!sort) { + return this.db.get(collection).filter(query).take(limit).value(); + } + // The db does not support sorting by multiple keys, get all data + // and sort it by each key (and its order) and then apply the limit + var allDocs = this.db.get(collection).filter(query).value(); + var sorted = (0, _keys2.default)(sort).reduce(function (unsorted, key) { + var order = sort[key]; + var sorter = function sorter(x, y) { + return x[key] > y[key] ? order * 1 : order * -1; + }; + return unsorted.sort(sorter); + }, allDocs); + // apply the limit after sorting + return sorted.slice(0, limit); + } + }, { + key: 'set', + value: function set(collection, item) { + // if the database doesn't exist, add the item + // and return the inserted item as the result. + if (!this.db.has(collection).value()) { + this.db.set(collection, [item]).value(); + return item; + } + // if the item already exists in the database, update it + if (this.db.get(collection).find({ id: item.id }).value()) { + this.db.get(collection).find({ id: item.id }).assign(item).value(); + return item; + } + // If the item is not available in the database, insert it + var coll = this.db.get(collection).value(); + this.db.set(collection, [].concat((0, _toConsumableArray3.default)(coll), [item])).value(); + return item; + } + }]); + return Database; +}(); \ No newline at end of file diff --git a/dist/server/middleware.js b/dist/server/middleware/storybook.js similarity index 90% rename from dist/server/middleware.js rename to dist/server/middleware/storybook.js index ed95ff72c862..ecbc4a7d7954 100644 --- a/dist/server/middleware.js +++ b/dist/server/middleware/storybook.js @@ -52,22 +52,22 @@ var _webpackHotMiddleware = require('webpack-hot-middleware'); var _webpackHotMiddleware2 = _interopRequireDefault(_webpackHotMiddleware); -var _webpack3 = require('./config/webpack.config'); +var _webpack3 = require('../config/webpack.config'); var _webpack4 = _interopRequireDefault(_webpack3); -var _config = require('./config'); +var _config = require('../config'); var _config2 = _interopRequireDefault(_config); -var _index = require('./index.html'); +var _index = require('../index.html'); var _index2 = _interopRequireDefault(_index); -var _iframe = require('./iframe.html'); +var _iframe = require('../iframe.html'); var _iframe2 = _interopRequireDefault(_iframe); -var _utils = require('./utils'); +var _utils = require('../utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } \ No newline at end of file diff --git a/package.json b/package.json index 4cb1a8e57939..788be14d8142 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "babel-preset-es2016": "^6.11.3", "babel-preset-react": "^6.11.1", "babel-runtime": "^6.9.2", + "body-parser": "^1.15.2", "case-sensitive-paths-webpack-plugin": "^1.1.2", "commander": "^2.9.0", "configstore": "^2.0.0", @@ -53,6 +54,7 @@ "json-stringify-safe": "^5.0.1", "json5": "^0.5.0", "lodash.pick": "^4.2.0", + "lowdb": "^0.13.1", "postcss-loader": "0.9.1", "qs": "^6.1.0", "react-modal": "^1.2.0", diff --git a/src/server/index.js b/src/server/index.js index ea6239dd5b42..941309ed4391 100755 --- a/src/server/index.js +++ b/src/server/index.js @@ -4,7 +4,8 @@ import express from 'express'; import program from 'commander'; import path from 'path'; import fs from 'fs'; -import storybook from './middleware'; +import storybook from './middleware/storybook'; +import datastore from './middleware/datastore'; import packageJson from '../../package.json'; import { parseList, getEnvConfig } from './utils'; import { track, dontTrack } from './track_usage'; @@ -65,6 +66,7 @@ if (program.staticDir) { // custom `.babelrc` file and `webpack.config.js` files const configDir = program.configDir || './.storybook'; app.use(storybook(configDir)); +app.use('/db', datastore(configDir)); app.listen(...listenAddr, function (error) { if (error) { diff --git a/src/server/middleware/datastore.js b/src/server/middleware/datastore.js new file mode 100644 index 000000000000..901f30521136 --- /dev/null +++ b/src/server/middleware/datastore.js @@ -0,0 +1,76 @@ +import path from 'path'; +import { Router } from 'express'; +import lowdb from 'lowdb'; +import fileAsyncStorage from 'lowdb/lib/file-async'; +import bodyParser from 'body-parser'; +import uuid from 'uuid'; + +export class Database { + constructor(dbPath) { + this.db = lowdb(dbPath, { storage: fileAsyncStorage }); + } + + get(collection, query, sort, limit) { + // if the database doesn't exist, add the document + // and return the inserted document as the result. + if (!this.db.has(collection).value()) { + return []; + } + // If the sort param is not given, use the DB interface + if (!sort) { + return this.db.get(collection).filter(query).take(limit).value(); + } + // The db does not support sorting by multiple keys, get all data + // and sort it by each key (and its order) and then apply the limit + const allDocs = this.db.get(collection).filter(query).value(); + const sorted = Object.keys(sort).reduce((unsorted, key) => { + const order = sort[key]; + const sorter = (x, y) => (x[key] > y[key]) ? order * 1 : order * -1; + return unsorted.sort(sorter); + }, allDocs); + // apply the limit after sorting + return sorted.slice(0, limit); + } + + set(collection, item) { + // if the database doesn't exist, add the item + // and return the inserted item as the result. + if (!this.db.has(collection).value()) { + this.db.set(collection, [ item ]).value(); + return item; + } + // if the item already exists in the database, update it + if (this.db.get(collection).find({ id: item.id }).value()) { + this.db.get(collection).find({ id: item.id }).assign(item).value(); + return item; + } + // If the item is not available in the database, insert it + const coll = this.db.get(collection).value(); + this.db.set(collection, [ ...coll, item ]).value(); + return item; + } +} + +export default function (configDir) { + const dbPath = path.resolve(configDir, 'datastore.json'); + const db = new Database(dbPath); + + const router = new Router(); + router.use(bodyParser.json()); + + router.post('/get', function (req, res) { + const { collection, query, sort, limit } = req.body; + const out = db.get(collection, query, sort, limit); + res.send({data: out}); + res.end(); + }); + + router.post('/set', function (req, res) { + const { collection, item } = req.body; + const out = db.set(collection, item); + res.send({data: out}); + res.end(); + }); + + return router; +} diff --git a/src/server/middleware.js b/src/server/middleware/storybook.js similarity index 84% rename from src/server/middleware.js rename to src/server/middleware/storybook.js index 87cf843fa745..a1501f15d835 100644 --- a/src/server/middleware.js +++ b/src/server/middleware/storybook.js @@ -2,11 +2,11 @@ import { Router } from 'express'; import webpack from 'webpack'; import webpackDevMiddleware from 'webpack-dev-middleware'; import webpackHotMiddleware from 'webpack-hot-middleware'; -import baseConfig from './config/webpack.config'; -import loadConfig from './config'; -import getIndexHtml from './index.html'; -import getIframeHtml from './iframe.html'; -import { getHeadHtml } from './utils'; +import baseConfig from '../config/webpack.config'; +import loadConfig from '../config'; +import getIndexHtml from '../index.html'; +import getIframeHtml from '../iframe.html'; +import { getHeadHtml } from '../utils'; export default function (configDir) { // Build the webpack configuration using the `baseConfig` From cc4d678a3cbe3556b7e5dac201536f49d26d9e60 Mon Sep 17 00:00:00 2001 From: Muhammed Thanish Date: Wed, 31 Aug 2016 19:45:32 +0530 Subject: [PATCH 3/6] Add default database client --- dist/client/manager/provider.js | 11 ++++++++++- package.json | 1 + src/client/manager/provider.js | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dist/client/manager/provider.js b/dist/client/manager/provider.js index 84e13de289d4..30f1b0d560ec 100644 --- a/dist/client/manager/provider.js +++ b/dist/client/manager/provider.js @@ -46,6 +46,10 @@ var _storybookChannelPagebus = require('@kadira/storybook-channel-pagebus'); var _storybookChannelPagebus2 = _interopRequireDefault(_storybookChannelPagebus); +var _storybookDatabaseLocal = require('@kadira/storybook-database-local'); + +var _storybookDatabaseLocal2 = _interopRequireDefault(_storybookDatabaseLocal); + var _preview = require('./preview'); var _preview2 = _interopRequireDefault(_preview); @@ -58,11 +62,16 @@ var ReactProvider = function (_Provider) { function ReactProvider() { (0, _classCallCheck3.default)(this, ReactProvider); - var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(ReactProvider).call(this)); + var _this = (0, _possibleConstructorReturn3.default)(this, (ReactProvider.__proto__ || (0, _getPrototypeOf2.default)(ReactProvider)).call(this)); _this.dataId = _uuid2.default.v4(); _this.channel = (0, _storybookChannelPagebus2.default)({ key: _this.dataId }); _storybookAddons2.default.setChannel(_this.channel); + _this.database = _storybookAddons2.default.getDatabase(); + if (!_this.database) { + _this.database = (0, _storybookDatabaseLocal2.default)({ url: location.origin + '/db' }); + _storybookAddons2.default.setDatabase(_this.database); + } return _this; } diff --git a/package.json b/package.json index 788be14d8142..ba915556a662 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "@kadira/storybook-addon-links": "^1.0.0", "@kadira/storybook-addons": "^1.5.0", "@kadira/storybook-channel-pagebus": "^2.0.2", + "@kadira/storybook-database-local": "^1.0.3", "@kadira/storybook-ui": "^3.3.2", "autoprefixer": "^6.3.7", "babel-core": "^6.11.4", diff --git a/src/client/manager/provider.js b/src/client/manager/provider.js index f47d8b6a5fff..7c750c7f788a 100644 --- a/src/client/manager/provider.js +++ b/src/client/manager/provider.js @@ -4,6 +4,7 @@ import React from 'react'; import { Provider } from '@kadira/storybook-ui'; import addons from '@kadira/storybook-addons'; import createChannel from '@kadira/storybook-channel-pagebus'; +import createDatabase from '@kadira/storybook-database-local'; import Preview from './preview'; export default class ReactProvider extends Provider { @@ -12,6 +13,11 @@ export default class ReactProvider extends Provider { this.dataId = UUID.v4(); this.channel = createChannel({ key: this.dataId }); addons.setChannel(this.channel); + this.database = addons.getDatabase(); + if (!this.database) { + this.database = createDatabase({ url: `${location.origin}/db` }); + addons.setDatabase(this.database); + } } getPanels() { From a2e6a1cea6fcfbee6af14493fd2a3a26ad1c81a1 Mon Sep 17 00:00:00 2001 From: Muhammed Thanish Date: Wed, 31 Aug 2016 19:49:54 +0530 Subject: [PATCH 4/6] Fix lint errors --- dist/server/middleware/datastore.js | 11 +++-------- src/server/middleware/datastore.js | 16 ++++++++-------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/dist/server/middleware/datastore.js b/dist/server/middleware/datastore.js index 57efddb1c2d6..aab8ee1212f3 100644 --- a/dist/server/middleware/datastore.js +++ b/dist/server/middleware/datastore.js @@ -71,10 +71,6 @@ var _bodyParser = require('body-parser'); var _bodyParser2 = _interopRequireDefault(_bodyParser); -var _uuid = require('uuid'); - -var _uuid2 = _interopRequireDefault(_uuid); - function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var Database = exports.Database = function () { @@ -100,11 +96,10 @@ var Database = exports.Database = function () { // and sort it by each key (and its order) and then apply the limit var allDocs = this.db.get(collection).filter(query).value(); var sorted = (0, _keys2.default)(sort).reduce(function (unsorted, key) { - var order = sort[key]; - var sorter = function sorter(x, y) { + return unsorted.sort(function (x, y) { + var order = sort[key]; return x[key] > y[key] ? order * 1 : order * -1; - }; - return unsorted.sort(sorter); + }); }, allDocs); // apply the limit after sorting return sorted.slice(0, limit); diff --git a/src/server/middleware/datastore.js b/src/server/middleware/datastore.js index 901f30521136..a389de53dc5f 100644 --- a/src/server/middleware/datastore.js +++ b/src/server/middleware/datastore.js @@ -3,7 +3,6 @@ import { Router } from 'express'; import lowdb from 'lowdb'; import fileAsyncStorage from 'lowdb/lib/file-async'; import bodyParser from 'body-parser'; -import uuid from 'uuid'; export class Database { constructor(dbPath) { @@ -24,9 +23,10 @@ export class Database { // and sort it by each key (and its order) and then apply the limit const allDocs = this.db.get(collection).filter(query).value(); const sorted = Object.keys(sort).reduce((unsorted, key) => { - const order = sort[key]; - const sorter = (x, y) => (x[key] > y[key]) ? order * 1 : order * -1; - return unsorted.sort(sorter); + return unsorted.sort(function (x, y) { + const order = sort[key]; + return (x[key] > y[key]) ? order * 1 : order * -1; + }); }, allDocs); // apply the limit after sorting return sorted.slice(0, limit); @@ -36,7 +36,7 @@ export class Database { // if the database doesn't exist, add the item // and return the inserted item as the result. if (!this.db.has(collection).value()) { - this.db.set(collection, [ item ]).value(); + this.db.set(collection, [item]).value(); return item; } // if the item already exists in the database, update it @@ -46,7 +46,7 @@ export class Database { } // If the item is not available in the database, insert it const coll = this.db.get(collection).value(); - this.db.set(collection, [ ...coll, item ]).value(); + this.db.set(collection, [...coll, item]).value(); return item; } } @@ -61,14 +61,14 @@ export default function (configDir) { router.post('/get', function (req, res) { const { collection, query, sort, limit } = req.body; const out = db.get(collection, query, sort, limit); - res.send({data: out}); + res.send({ data: out }); res.end(); }); router.post('/set', function (req, res) { const { collection, item } = req.body; const out = db.set(collection, item); - res.send({data: out}); + res.send({ data: out }); res.end(); }); From fb2e40447cae287c0260db74db7c6e3eced71752 Mon Sep 17 00:00:00 2001 From: Muhammed Thanish Date: Thu, 1 Sep 2016 13:20:11 +0530 Subject: [PATCH 5/6] Move the middleware to db repo --- dist/server/index.js | 13 +- .../storybook.js => middleware.js} | 10 +- dist/server/middleware/datastore.js | 128 ------------------ package.json | 4 +- src/server/index.js | 7 +- .../storybook.js => middleware.js} | 10 +- src/server/middleware/datastore.js | 76 ----------- 7 files changed, 22 insertions(+), 226 deletions(-) rename dist/server/{middleware/storybook.js => middleware.js} (90%) delete mode 100644 dist/server/middleware/datastore.js rename src/server/{middleware/storybook.js => middleware.js} (84%) delete mode 100644 src/server/middleware/datastore.js diff --git a/dist/server/index.js b/dist/server/index.js index 6e3eaf1d512e..ebc893dcc035 100755 --- a/dist/server/index.js +++ b/dist/server/index.js @@ -17,13 +17,13 @@ var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); -var _storybook = require('./middleware/storybook'); +var _middleware = require('./middleware'); -var _storybook2 = _interopRequireDefault(_storybook); +var _middleware2 = _interopRequireDefault(_middleware); -var _datastore = require('./middleware/datastore'); +var _middleware3 = require('@kadira/storybook-database-local/dist/server/middleware'); -var _datastore2 = _interopRequireDefault(_datastore); +var _middleware4 = _interopRequireDefault(_middleware3); var _package = require('../../package.json'); @@ -83,8 +83,9 @@ if (_commander2.default.staticDir) { // Build the webpack configuration using the `baseConfig` // custom `.babelrc` file and `webpack.config.js` files var configDir = _commander2.default.configDir || './.storybook'; -app.use((0, _storybook2.default)(configDir)); -app.use('/db', (0, _datastore2.default)(configDir)); +var dbPath = _path2.default.resolve(configDir, 'datastore.json'); +app.use((0, _middleware2.default)(configDir)); +app.use('/db', (0, _middleware4.default)(dbPath)); app.listen.apply(app, listenAddr.concat([function (error) { if (error) { diff --git a/dist/server/middleware/storybook.js b/dist/server/middleware.js similarity index 90% rename from dist/server/middleware/storybook.js rename to dist/server/middleware.js index ecbc4a7d7954..ed95ff72c862 100644 --- a/dist/server/middleware/storybook.js +++ b/dist/server/middleware.js @@ -52,22 +52,22 @@ var _webpackHotMiddleware = require('webpack-hot-middleware'); var _webpackHotMiddleware2 = _interopRequireDefault(_webpackHotMiddleware); -var _webpack3 = require('../config/webpack.config'); +var _webpack3 = require('./config/webpack.config'); var _webpack4 = _interopRequireDefault(_webpack3); -var _config = require('../config'); +var _config = require('./config'); var _config2 = _interopRequireDefault(_config); -var _index = require('../index.html'); +var _index = require('./index.html'); var _index2 = _interopRequireDefault(_index); -var _iframe = require('../iframe.html'); +var _iframe = require('./iframe.html'); var _iframe2 = _interopRequireDefault(_iframe); -var _utils = require('../utils'); +var _utils = require('./utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } \ No newline at end of file diff --git a/dist/server/middleware/datastore.js b/dist/server/middleware/datastore.js deleted file mode 100644 index aab8ee1212f3..000000000000 --- a/dist/server/middleware/datastore.js +++ /dev/null @@ -1,128 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.Database = undefined; - -var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); - -var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); - -var _keys = require('babel-runtime/core-js/object/keys'); - -var _keys2 = _interopRequireDefault(_keys); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -exports.default = function (configDir) { - var dbPath = _path2.default.resolve(configDir, 'datastore.json'); - var db = new Database(dbPath); - - var router = new _express.Router(); - router.use(_bodyParser2.default.json()); - - router.post('/get', function (req, res) { - var _req$body = req.body; - var collection = _req$body.collection; - var query = _req$body.query; - var sort = _req$body.sort; - var limit = _req$body.limit; - - var out = db.get(collection, query, sort, limit); - res.send({ data: out }); - res.end(); - }); - - router.post('/set', function (req, res) { - var _req$body2 = req.body; - var collection = _req$body2.collection; - var item = _req$body2.item; - - var out = db.set(collection, item); - res.send({ data: out }); - res.end(); - }); - - return router; -}; - -var _path = require('path'); - -var _path2 = _interopRequireDefault(_path); - -var _express = require('express'); - -var _lowdb = require('lowdb'); - -var _lowdb2 = _interopRequireDefault(_lowdb); - -var _fileAsync = require('lowdb/lib/file-async'); - -var _fileAsync2 = _interopRequireDefault(_fileAsync); - -var _bodyParser = require('body-parser'); - -var _bodyParser2 = _interopRequireDefault(_bodyParser); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var Database = exports.Database = function () { - function Database(dbPath) { - (0, _classCallCheck3.default)(this, Database); - - this.db = (0, _lowdb2.default)(dbPath, { storage: _fileAsync2.default }); - } - - (0, _createClass3.default)(Database, [{ - key: 'get', - value: function get(collection, query, sort, limit) { - // if the database doesn't exist, add the document - // and return the inserted document as the result. - if (!this.db.has(collection).value()) { - return []; - } - // If the sort param is not given, use the DB interface - if (!sort) { - return this.db.get(collection).filter(query).take(limit).value(); - } - // The db does not support sorting by multiple keys, get all data - // and sort it by each key (and its order) and then apply the limit - var allDocs = this.db.get(collection).filter(query).value(); - var sorted = (0, _keys2.default)(sort).reduce(function (unsorted, key) { - return unsorted.sort(function (x, y) { - var order = sort[key]; - return x[key] > y[key] ? order * 1 : order * -1; - }); - }, allDocs); - // apply the limit after sorting - return sorted.slice(0, limit); - } - }, { - key: 'set', - value: function set(collection, item) { - // if the database doesn't exist, add the item - // and return the inserted item as the result. - if (!this.db.has(collection).value()) { - this.db.set(collection, [item]).value(); - return item; - } - // if the item already exists in the database, update it - if (this.db.get(collection).find({ id: item.id }).value()) { - this.db.get(collection).find({ id: item.id }).assign(item).value(); - return item; - } - // If the item is not available in the database, insert it - var coll = this.db.get(collection).value(); - this.db.set(collection, [].concat((0, _toConsumableArray3.default)(coll), [item])).value(); - return item; - } - }]); - return Database; -}(); \ No newline at end of file diff --git a/package.json b/package.json index ba915556a662..98c9ea9f9ff6 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "@kadira/storybook-addon-links": "^1.0.0", "@kadira/storybook-addons": "^1.5.0", "@kadira/storybook-channel-pagebus": "^2.0.2", - "@kadira/storybook-database-local": "^1.0.3", + "@kadira/storybook-database-local": "^1.1.0", "@kadira/storybook-ui": "^3.3.2", "autoprefixer": "^6.3.7", "babel-core": "^6.11.4", @@ -43,7 +43,6 @@ "babel-preset-es2016": "^6.11.3", "babel-preset-react": "^6.11.1", "babel-runtime": "^6.9.2", - "body-parser": "^1.15.2", "case-sensitive-paths-webpack-plugin": "^1.1.2", "commander": "^2.9.0", "configstore": "^2.0.0", @@ -55,7 +54,6 @@ "json-stringify-safe": "^5.0.1", "json5": "^0.5.0", "lodash.pick": "^4.2.0", - "lowdb": "^0.13.1", "postcss-loader": "0.9.1", "qs": "^6.1.0", "react-modal": "^1.2.0", diff --git a/src/server/index.js b/src/server/index.js index 941309ed4391..bd6246915537 100755 --- a/src/server/index.js +++ b/src/server/index.js @@ -4,8 +4,8 @@ import express from 'express'; import program from 'commander'; import path from 'path'; import fs from 'fs'; -import storybook from './middleware/storybook'; -import datastore from './middleware/datastore'; +import storybook from './middleware'; +import datastore from '@kadira/storybook-database-local/dist/server/middleware'; import packageJson from '../../package.json'; import { parseList, getEnvConfig } from './utils'; import { track, dontTrack } from './track_usage'; @@ -65,8 +65,9 @@ if (program.staticDir) { // Build the webpack configuration using the `baseConfig` // custom `.babelrc` file and `webpack.config.js` files const configDir = program.configDir || './.storybook'; +const dbPath = path.resolve(configDir, 'datastore.json'); app.use(storybook(configDir)); -app.use('/db', datastore(configDir)); +app.use('/db', datastore(dbPath)); app.listen(...listenAddr, function (error) { if (error) { diff --git a/src/server/middleware/storybook.js b/src/server/middleware.js similarity index 84% rename from src/server/middleware/storybook.js rename to src/server/middleware.js index a1501f15d835..87cf843fa745 100644 --- a/src/server/middleware/storybook.js +++ b/src/server/middleware.js @@ -2,11 +2,11 @@ import { Router } from 'express'; import webpack from 'webpack'; import webpackDevMiddleware from 'webpack-dev-middleware'; import webpackHotMiddleware from 'webpack-hot-middleware'; -import baseConfig from '../config/webpack.config'; -import loadConfig from '../config'; -import getIndexHtml from '../index.html'; -import getIframeHtml from '../iframe.html'; -import { getHeadHtml } from '../utils'; +import baseConfig from './config/webpack.config'; +import loadConfig from './config'; +import getIndexHtml from './index.html'; +import getIframeHtml from './iframe.html'; +import { getHeadHtml } from './utils'; export default function (configDir) { // Build the webpack configuration using the `baseConfig` diff --git a/src/server/middleware/datastore.js b/src/server/middleware/datastore.js deleted file mode 100644 index a389de53dc5f..000000000000 --- a/src/server/middleware/datastore.js +++ /dev/null @@ -1,76 +0,0 @@ -import path from 'path'; -import { Router } from 'express'; -import lowdb from 'lowdb'; -import fileAsyncStorage from 'lowdb/lib/file-async'; -import bodyParser from 'body-parser'; - -export class Database { - constructor(dbPath) { - this.db = lowdb(dbPath, { storage: fileAsyncStorage }); - } - - get(collection, query, sort, limit) { - // if the database doesn't exist, add the document - // and return the inserted document as the result. - if (!this.db.has(collection).value()) { - return []; - } - // If the sort param is not given, use the DB interface - if (!sort) { - return this.db.get(collection).filter(query).take(limit).value(); - } - // The db does not support sorting by multiple keys, get all data - // and sort it by each key (and its order) and then apply the limit - const allDocs = this.db.get(collection).filter(query).value(); - const sorted = Object.keys(sort).reduce((unsorted, key) => { - return unsorted.sort(function (x, y) { - const order = sort[key]; - return (x[key] > y[key]) ? order * 1 : order * -1; - }); - }, allDocs); - // apply the limit after sorting - return sorted.slice(0, limit); - } - - set(collection, item) { - // if the database doesn't exist, add the item - // and return the inserted item as the result. - if (!this.db.has(collection).value()) { - this.db.set(collection, [item]).value(); - return item; - } - // if the item already exists in the database, update it - if (this.db.get(collection).find({ id: item.id }).value()) { - this.db.get(collection).find({ id: item.id }).assign(item).value(); - return item; - } - // If the item is not available in the database, insert it - const coll = this.db.get(collection).value(); - this.db.set(collection, [...coll, item]).value(); - return item; - } -} - -export default function (configDir) { - const dbPath = path.resolve(configDir, 'datastore.json'); - const db = new Database(dbPath); - - const router = new Router(); - router.use(bodyParser.json()); - - router.post('/get', function (req, res) { - const { collection, query, sort, limit } = req.body; - const out = db.get(collection, query, sort, limit); - res.send({ data: out }); - res.end(); - }); - - router.post('/set', function (req, res) { - const { collection, item } = req.body; - const out = db.set(collection, item); - res.send({ data: out }); - res.end(); - }); - - return router; -} From 014fc2777d6bea2c80d4d2cfbef684648df9f8e4 Mon Sep 17 00:00:00 2001 From: Muhammed Thanish Date: Thu, 1 Sep 2016 18:03:54 +0530 Subject: [PATCH 6/6] Enable database with a flag --- dist/server/index.js | 11 ++++++++--- src/server/index.js | 11 +++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/dist/server/index.js b/dist/server/index.js index ebc893dcc035..90cc81ac294d 100755 --- a/dist/server/index.js +++ b/dist/server/index.js @@ -37,7 +37,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de var logger = console; -_commander2.default.version(_package2.default.version).option('-p, --port [number]', 'Port to run Storybook (Required)', parseInt).option('-h, --host [string]', 'Host to run Storybook').option('-s, --static-dir ', 'Directory where to load static files from', _utils.parseList).option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from').option('--dont-track', 'Do not send anonymous usage stats.').parse(process.argv); +_commander2.default.version(_package2.default.version).option('-p, --port [number]', 'Port to run Storybook (Required)', parseInt).option('-h, --host [string]', 'Host to run Storybook').option('-s, --static-dir ', 'Directory where to load static files from', _utils.parseList).option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from').option('-d, --db-path [db-file]', 'File where to store addon database JSON file').option('--enable-db', 'Enable the (experimental) addon database service on dev-server').option('--dont-track', 'Do not send anonymous usage stats.').parse(process.argv); // The key is the field created in `program` variable for // each command line argument. Value is the env variable. @@ -83,9 +83,14 @@ if (_commander2.default.staticDir) { // Build the webpack configuration using the `baseConfig` // custom `.babelrc` file and `webpack.config.js` files var configDir = _commander2.default.configDir || './.storybook'; -var dbPath = _path2.default.resolve(configDir, 'datastore.json'); app.use((0, _middleware2.default)(configDir)); -app.use('/db', (0, _middleware4.default)(dbPath)); + +// The addon database service is disabled by default for now +// It should be enabled with the --enable-db for dev server +if (_commander2.default.enableDb) { + var dbPath = _commander2.default.dbPath || './.storybook/addon-db.json'; + app.use('/db', (0, _middleware4.default)(dbPath)); +} app.listen.apply(app, listenAddr.concat([function (error) { if (error) { diff --git a/src/server/index.js b/src/server/index.js index bd6246915537..0637bab6c003 100755 --- a/src/server/index.js +++ b/src/server/index.js @@ -18,6 +18,8 @@ program .option('-h, --host [string]', 'Host to run Storybook') .option('-s, --static-dir ', 'Directory where to load static files from', parseList) .option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from') + .option('-d, --db-path [db-file]', 'File where to store addon database JSON file') + .option('--enable-db', 'Enable the (experimental) addon database service on dev-server') .option('--dont-track', 'Do not send anonymous usage stats.') .parse(process.argv); @@ -65,9 +67,14 @@ if (program.staticDir) { // Build the webpack configuration using the `baseConfig` // custom `.babelrc` file and `webpack.config.js` files const configDir = program.configDir || './.storybook'; -const dbPath = path.resolve(configDir, 'datastore.json'); app.use(storybook(configDir)); -app.use('/db', datastore(dbPath)); + +// The addon database service is disabled by default for now +// It should be enabled with the --enable-db for dev server +if (program.enableDb) { + const dbPath = program.dbPath || './.storybook/addon-db.json'; + app.use('/db', datastore(dbPath)); +} app.listen(...listenAddr, function (error) { if (error) {