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
// | |
// Isolated update user logic from express route | |
// Still coupled with mongo db client | |
// | |
async function updateUser ({db, updateParams}) { | |
const { userId, userName, userEmail } = updateParams | |
// validate request params | |
if (!userId || !userName || !userEmail) { | |
throw new InvalidParamsException('Missing required params') |
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
// | |
// Express controller with isolated | |
// update user logic | |
// | |
router.put('/user', async (req, res) => { | |
// db object is already added to req object when express is initialised | |
const db = req.db; | |
const { userId, userName, userEmail } = req.body | |
await updateUser({ |
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
// | |
// Express controller route example of | |
// coupled infrastructure with core logic | |
// | |
router.put('/user', async (req, res) => { | |
// db object is already added to req object when express is initialised | |
const db = req.db | |
const { userId, userName, userEmail } = req.body |
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
// | |
// UserRepositoryMongo factory implementing UserRepository interface | |
// | |
export default db => { | |
const users = db.get('users') | |
const create = async (user) => { | |
const { userId } = user | |
await users.insert(user) | |
} |
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
// | |
// UserRepository interface implemented instead of | |
// directly interfacing mongo db client | |
// | |
async function updateUser ({userRepository, updateParams}) { | |
const { userId, userName, userEmail } = updateParams | |
// validate params | |
if (!userId || !userName || !userEmail) { | |
throw new InvalidParamsException('Missing required params') |
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
// | |
// Express controller with UserRepositoryMongo instantiated | |
// and passed to updateUser | |
// | |
router.put('/user', async (req, res) => { | |
// db object is already added to req object when express is initialised | |
const db = req.db; | |
const { userId, userName, userEmail } = req.body | |
// get userRepository instance from factory |
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
// | |
// UserRepositoryInMemory factory implementing UserRepository interface | |
// used for mocking the infrastructure for running tests | |
// | |
export default () => { | |
const users = new Map() | |
const create = async (user) => { | |
const { userId } = user | |
users.set(userId, user) |
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
// get userRepository instance from factory | |
const userRepository = UserRepositoryInMemory() | |
const USER_ID = 1 | |
const USER_NAME_BEFORE = 'user1' | |
const USER_EMAIL_BEFORE = 'email1@test.com' | |
const USER_NAME_AFTER = 'user1_updated' | |
const USER_EMAIL_AFRTER = 'email1.updated@test.com' |
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
// | |
// UserRepositoryMySQL factory implementing UserRepository interface | |
// | |
export default conn => { | |
const create = user => { | |
const { userId, userName, userEmail } = user | |
return new Promise((resolve, reject) => { | |
conn.query(`INSERT INTO users (userId, userName, userEmail) VALUES (${userId}, '${userName}', '${userEmail}')`, function (error) { | |
if (error) { | |
return reject(error) |
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
// | |
// Express controller | |
// User repository using mysql database instead of mongo | |
// | |
router.put('/user', async (req, res) => { | |
// mysql conn object is already added to req object when express is initialised | |
const conn = req.conn; | |
const { userId, userName, userEmail } = req.body | |
// get userRepository instance from factory |
OlderNewer