Skip to content
This repository was archived by the owner on Jul 16, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 45 additions & 28 deletions api/database.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const firebase = require('firebase');
const jsonpatch = require('fast-json-patch');

function guessType(value) {
const number = /^-?\d*\.?\d*$/;
Expand Down Expand Up @@ -37,39 +38,43 @@ if (!process.env.FIREBASE_CONFIG) {
const config = JSON.parse(process.env.FIREBASE_CONFIG);
firebase.initializeApp(config);

module.exports = {
get: (key, orderKey, filterValue, valueType) => {
let ref = firebase.database().ref(key);
function get(key, orderKey, filterValue, valueType) {
let ref = firebase.database().ref(key);

if (orderKey) {
ref = ref.orderByChild(orderKey);
if (filterValue !== undefined) {
ref = ref.equalTo(convert(filterValue, valueType));
}
if (orderKey) {
ref = ref.orderByChild(orderKey);
if (filterValue !== undefined) {
ref = ref.equalTo(convert(filterValue, valueType));
}
return ref
.once('value')
.then(snapshot => {
if (!orderKey) {
return snapshot.val();
}
return ref
.once('value')
.then(snapshot => {
if (!orderKey) {
return snapshot.val();
}

const data = [];
// snapshot.val() forgets any ordering info
snapshot.forEach(item => {
if (item.val()) {
data.push(item.val());
}

const data = [];
// snapshot.val() forgets any ordering info
snapshot.forEach(item => {
if (item.val()) {
data.push(item.val());
}
});
return data;
});
},
return data;
});
}

post: (key, data) =>
firebase
.database()
.ref(key)
.set(data),
function post(key, data) {
return firebase
.database()
.ref(key)
.set(data);
}

module.exports = {
get,
post,

put: (key, data) =>
firebase
Expand All @@ -84,5 +89,17 @@ if (!process.env.FIREBASE_CONFIG) {
.database()
.ref(key)
.remove(),

patch: (key, data, type) => {
return get(key).then(stored => {
let patched = null;
if (type === 'application/json') {
patched = Object.assign(stored, data);
} else if (type === 'application/json-patch+json') {
patched = jsonpatch.applyPatch(stored, data).newDocument;
}
return post(key, patched);
});
},
}
}
13 changes: 12 additions & 1 deletion api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ router.post(/^\/[0-9a-z]{64}/, checkContentType, (req, res) =>
.post(req.path, req.body)
.then(() => res.status(201).send({ ok: true }))
.catch(() => res.status(500).send({ ok: false }))
)
);

router.put(/^\/[0-9a-z]{64}/, checkContentType, (req, res) =>
database
Expand All @@ -49,4 +49,15 @@ router.delete(/^\/[0-9a-z]{64}/, (req, res) =>
.catch(() => res.status(500).send({ ok: false }))
);

router.patch(/^\/[0-9a-z]{64}/, (req, res) =>
database
.patch(req.path, req.body, req.headers['content-type'])
.then(() => res.status(200).send({ ok: true }))
.catch((err) => {
console.log(err);
res.status(500).send({ ok: false });
}),
);

module.exports = router;

Loading