Skip to content

Commit

Permalink
very early code for querying
Browse files Browse the repository at this point in the history
  • Loading branch information
akshendra committed Jun 7, 2018
1 parent 8a82b83 commit 4740f68
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
9 changes: 9 additions & 0 deletions api/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ if (!process.env.FIREBASE_CONFIG) {
.once('value')
.then(snapshot => snapshot.val()),

query: (key, condition) =>
firebase
.database()
.ref(key)
.orderByChild(condition.key)
.equalTo(condition.value)
.once('value')
.then(snapshot => snapshot.val()),

post: (key, data) =>
firebase
.database()
Expand Down
26 changes: 22 additions & 4 deletions api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,30 @@ router.get('/get-token', (req, res) => {
return res.send({ token: hash });
});

router.get(/^\/[0-9a-z]{64}/, (req, res) =>
database
.get(req.path)
function convert(value, type) {
const typemap = {
'number': Number,
'boolean': Boolean,
'string': String,
};
const converter = typemap[type] || String;
return converter(value);
}

router.get(/^\/[0-9a-z]{64}/, (req, res) => {
const { key, value, type } = req.query;
let reference = null;

if (key && value) {
reference = database.query(req.path, { key, value: convert(value, type) });
} else {
reference = database.get(req.path, { key, value })
}

reference
.then(result => res.status(200).send({ result, ok: true }))
.catch(() => res.status(500).send({ ok: false }))
);
});

router.post(/^\/[0-9a-z]{64}/, checkContentType, (req, res) =>
database
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"start": "run-s client:build server:start",
"prod": "PORT=80 npm start",
"server:start": "node ./server",
"server:start": "nodemon ./server",
"client:build": "webpack",
"heroku-postbuild": "npm run client:build"
},
Expand Down

0 comments on commit 4740f68

Please sign in to comment.