-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add database support for addons #415
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
17daa45
Update storybook-addons API
thani-sh 00db207
Implement database server
thani-sh cc4d678
Add default database client
thani-sh a2e6a1c
Fix lint errors
thani-sh fb2e404
Move the middleware to db repo
thani-sh 014fc27
Enable database with a flag
thani-sh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
'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; | ||
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to move this to another repo? If there's a reason we can move the server code too. (I mean the middleware)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this on RN storybook also. I'll move the middle ware tomorrow. Please hold this PR until then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move it to the same module. We can import the middleware like this: