Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
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
}
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
}
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