Skip to content

Instantly share code, notes, and snippets.

View Amberlamps's full-sized avatar

Alexander Behrens Amberlamps

View GitHub Profile
interface AbstractionLayer {
createUser();
}
class MongoDbAdapter implements AbstractionLayer {
createUser() {
// Mongodb specific code
}
}
const ClientWrapper = require('./mongo-client-wrapper.js');
ClientWrapper("url/to/mongodb").then((db) => {
db.insert();
});
@Amberlamps
Amberlamps / app.js
Last active July 8, 2017 11:25
going full circle 0
app.post('/comments', (req, res, next) => {
try {
const { userId, postId } = req.body;
const comment = db.createCommentSync(req.body);
const post = db.getPostSync(postId);
db.updatePostSync(postId, { comments: post.comments + 1 });
const user = db.getUserSync(userId);
@Amberlamps
Amberlamps / app.js
Created July 8, 2017 09:31
going full circle 2
app.get('/users/:userId', (req, res) => {
db.getUserAsync(req.params.userId, (err, user) => {
db.getPostsByUserAsync(user.id, (err, posts) => {
db.getCommentsByPostsAsync(posts, (err, comments) => {
res.json(mergeData(user, posts, comments));
});
});
});
});
@Amberlamps
Amberlamps / app.js
Created July 8, 2017 09:20
going full circle 1
app.get('/users', (req, res) => {
const users = db.getUsersAsync((err, data) => {
console.log(data);
return data;
});
res.send(users);
});
@Amberlamps
Amberlamps / navigation.css
Created November 8, 2014 14:30
How to get rid of if/else statements and additional classes when it comes to highlighting the selected item in a menu/navigation
.navigation li[data-selected] {
font-weight: bold;
}
@Amberlamps
Amberlamps / resolveKeyString.js
Last active August 29, 2015 14:07
Resolving dot separated key fields in nested Javascript objects.
function resolveKeyString(obj, keyString) {
return keyString.split('.').reduce(function(p, c) {
return p[c];
}, t);
}
// Usage:
var obj = {
a: {
b: {