Last active
June 20, 2018 20:18
-
-
Save kimmobrunfeldt/d5bb8ff249c8c8f922f6de1b5a88a00a to your computer and use it in GitHub Desktop.
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 { query } = require('../util/pg') | |
/* | |
Usage: | |
function handler(req) { | |
const id = req.params.id | |
const core = new UserCore(req.logger) | |
return core.getUser(id) | |
} | |
*/ | |
class UserCore { | |
constructor(logger) { | |
this.logger = logger | |
} | |
getUser(id) { | |
this.logger.log(`Getting user with id ${id} ..`) | |
return query('SELECT * FROM users WHERE id = :id', { id }) | |
.then((rows) => { | |
if (rows.length < 1) { | |
return null | |
} | |
return this._userRowToObject(rows[0]) | |
}) | |
} | |
_userRowToObject(row) { | |
this.logger.log('Transforming database row to object ..') | |
return { | |
id: row.id, | |
firstName: row.first_name, | |
lastName: row.last_name, | |
} | |
} | |
} | |
module.exports = UserCore |
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 { query } = require('../util/pg') | |
/* | |
Usage: | |
function handler(req) { | |
const id = req.params.id | |
return userCore.getUser(id).bind(req.logger) | |
} | |
*/ | |
function getUser(id) { | |
this.logger.log(`Getting user with id ${id} ..`) | |
return query('SELECT * FROM users WHERE id = :id', { id }) | |
.then((rows) => { | |
if (rows.length < 1) { | |
return null | |
} | |
return _userRowToObject.apply(this, rows[0]) | |
}) | |
} | |
function _userRowToObject(row) { | |
this.logger.log('Transforming database row to object ..') | |
return { | |
id: row.id, | |
firstName: row.first_name, | |
lastName: row.last_name, | |
} | |
} | |
module.exports = { | |
getUser | |
} |
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 { query } = require('../util/pg') | |
/* | |
Usage: | |
function handler(req) { | |
const id = req.params.id | |
return userCore.getUser(logger, id) | |
} | |
*/ | |
function getUser(logger, id) { | |
logger.log(`Getting user with id ${id} ..`) | |
return query('SELECT * FROM users WHERE id = :id', { id }) | |
.then((rows) => { | |
if (rows.length < 1) { | |
return null | |
} | |
return _userRowToObject(logger, rows[0]) | |
}) | |
} | |
function _userRowToObject(logger, row) { | |
this.logger.log('Transforming database row to object ..') | |
return { | |
id: row.id, | |
firstName: row.first_name, | |
lastName: row.last_name, | |
} | |
} | |
module.exports = { | |
getUser | |
} |
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 { query } = require('../util/pg') | |
/* | |
Usage: | |
function handler(req) { | |
const id = req.params.id | |
const core = createCore(req.logger) | |
return core.getUser(id) | |
} | |
*/ | |
function createCore(logger) { | |
function getUser(id) { | |
logger.log(`Getting user with id ${id} ..`) | |
return query('SELECT * FROM users WHERE id = :id', { id }) | |
.then((rows) => { | |
if (rows.length < 1) { | |
return null | |
} | |
return _userRowToObject(rows[0]) | |
}) | |
} | |
function _userRowToObject(row) { | |
logger.log('Transforming database row to object ..') | |
return { | |
id: row.id, | |
firstName: row.first_name, | |
lastName: row.last_name, | |
} | |
} | |
return { | |
getUser, | |
} | |
} | |
module.exports = createCore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment