Basic DB function structure

main
Ashelyn Dawn 5 years ago
parent aa35a1f4a6
commit 40f11b7b65

@ -0,0 +1,3 @@
{
"editor.tabSize": 2
}

@ -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;

@ -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;

@ -0,0 +1,3 @@
module.exports = {
user: require('./models/user')
}

@ -0,0 +1,3 @@
module.exports = [
...require('./user')
]

@ -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_'}
]
}
]

@ -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];
}
Loading…
Cancel
Save