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
interface AbstractionLayer { | |
createUser(); | |
} | |
class MongoDbAdapter implements AbstractionLayer { | |
createUser() { | |
// Mongodb specific code | |
} | |
} |
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
const ClientWrapper = require('./mongo-client-wrapper.js'); | |
ClientWrapper("url/to/mongodb").then((db) => { | |
db.insert(); | |
}); |
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
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); |
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
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)); | |
}); | |
}); | |
}); | |
}); |
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
app.get('/users', (req, res) => { | |
const users = db.getUsersAsync((err, data) => { | |
console.log(data); | |
return data; | |
}); | |
res.send(users); | |
}); |
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
function resolveKeyString(obj, keyString) { | |
return keyString.split('.').reduce(function(p, c) { | |
return p[c]; | |
}, t); | |
} | |
// Usage: | |
var obj = { | |
a: { | |
b: { |