sleep-tracker-backend/services/users/usersModel.js

40 lines
632 B
JavaScript
Raw Normal View History

const db = require('../../data/dbConfig');
module.exports = {
2019-08-01 14:25:44 +00:00
addUser,
findUserBy,
findUserById,
updateUser,
removeUser
};
2019-08-01 14:25:44 +00:00
function findUserBy(filter) {
return db('users')
.where(filter);
}
2019-08-01 14:25:44 +00:00
async function addUser(user) {
const [id] = await db('users')
.insert(user);
2019-08-01 14:25:44 +00:00
return findUserById(id);
}
2019-08-01 14:25:44 +00:00
function findUserById(id) {
return db('users')
.where({ id })
.first()
.select('id', 'email', 'username');
}
2019-08-01 07:42:25 +00:00
2019-08-01 14:25:44 +00:00
async function updateUser(id, changes) {
2019-08-01 07:42:25 +00:00
await db('users')
.where({ id })
.update(changes, '*');
2019-08-01 14:25:44 +00:00
return findUserById(id);
2019-08-01 07:42:25 +00:00
}
2019-08-01 14:25:44 +00:00
function removeUser(id) {
2019-08-01 07:42:25 +00:00
return db('users')
.where({ id })
.del();
}