From 40f11b7b65f789e79fcb705cb51ecde71a0380f4 Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Sat, 15 Feb 2020 19:34:19 -0700 Subject: [PATCH] Basic DB function structure --- .vscode/settings.json | 3 +++ api/index.js | 10 +++++++++- api/users.js | 11 +++++++++++ db/index.js | 3 +++ db/mappings/index.js | 3 +++ db/mappings/user.js | 40 ++++++++++++++++++++++++++++++++++++++++ db/models/user.js | 20 ++++++++++++++++++++ 7 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 api/users.js create mode 100644 db/index.js create mode 100644 db/mappings/index.js create mode 100644 db/mappings/user.js create mode 100644 db/models/user.js diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0a77011 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.tabSize": 2 +} \ No newline at end of file diff --git a/api/index.js b/api/index.js index e7a42b8..43be98d 100644 --- a/api/index.js +++ b/api/index.js @@ -1,9 +1,17 @@ const router = require('express-promise-router')() const pg = require('../db/pg') +router.use('/users/', require('./users')) + router.get('/', (req, res)=>{ - console.log(pg) res.json({test: true}) }) +router.use((req, res)=>{ + res.status(404) + res.json({ + error: 'Not found' + }) +}) + module.exports = router; diff --git a/api/users.js b/api/users.js new file mode 100644 index 0000000..5240fed --- /dev/null +++ b/api/users.js @@ -0,0 +1,11 @@ +const router = require('express-promise-router')() +const db = require('../db') + +router.get('/:uuid', async (req, res)=>{ + const user = await db.user.findById(req.params.uuid) + if(!user) return 'next' + + res.json(user) +}) + +module.exports = router; diff --git a/db/index.js b/db/index.js new file mode 100644 index 0000000..89ffc32 --- /dev/null +++ b/db/index.js @@ -0,0 +1,3 @@ +module.exports = { + user: require('./models/user') +} \ No newline at end of file diff --git a/db/mappings/index.js b/db/mappings/index.js new file mode 100644 index 0000000..b403e28 --- /dev/null +++ b/db/mappings/index.js @@ -0,0 +1,3 @@ +module.exports = [ + ...require('./user') +] \ No newline at end of file diff --git a/db/mappings/user.js b/db/mappings/user.js new file mode 100644 index 0000000..5090ce0 --- /dev/null +++ b/db/mappings/user.js @@ -0,0 +1,40 @@ +module.exports = [ + { + mapId: 'userMap', + idProperty: 'uuid', + properties: [ + 'email', + 'password', + 'email_confirmed', + 'time_registered', + 'time_email_confirmed' + ], + collections: [ + {name: 'login_links', mapId: 'loginLinkMap', columnPrefix: 'login_link_'}, + {name: 'sessions', mapId: 'sessionMap', columnPrefix: 'session_'} + ] + },{ + mapId: 'loginLinkMap', + idProperty: 'uuid', + properties: [ + 'time_created', + 'timeout_length', + 'login_hash' + ] + },{ + mapId: 'sessionMap', + idProperty: 'uuid', + properties: [ + 'time_created', + 'time_last_active', + 'timeout_length', + 'ip_address', + 'user_agent', + 'referer' + ], + associations: [ + {name: 'originating_link', mapId: 'loginLinkMap', columnPrefix: 'login_link_'}, + // {name: 'cart', mapId: 'cartMap', columnPrefix: 'cart_'} + ] + } +] \ No newline at end of file diff --git a/db/models/user.js b/db/models/user.js new file mode 100644 index 0000000..186980b --- /dev/null +++ b/db/models/user.js @@ -0,0 +1,20 @@ +const pg = require('../pg') +const joinjs = require('join-js').default; +const debug = require('debug')('sos:db:user') +const mappings = require('../mappings') + +const user = module.exports = {} + +user.findById = async (user_uuid)=>{ + const query = { + text: 'select * from "user" where user_uuid = $1', + values: [ + user_uuid + ] + } + + debug(query); + + const result = await pg.query(query) + return joinjs.map(result.rows, mappings, 'userMap', 'user_')[0]; +} \ No newline at end of file