40 lines
632 B
JavaScript
40 lines
632 B
JavaScript
const db = require('../../data/dbConfig');
|
|
|
|
module.exports = {
|
|
addUser,
|
|
findUserBy,
|
|
findUserById,
|
|
updateUser,
|
|
removeUser
|
|
};
|
|
|
|
function findUserBy(filter) {
|
|
return db('users')
|
|
.where(filter);
|
|
}
|
|
|
|
async function addUser(user) {
|
|
const [id] = await db('users')
|
|
.insert(user);
|
|
return findUserById(id);
|
|
}
|
|
|
|
function findUserById(id) {
|
|
return db('users')
|
|
.where({ id })
|
|
.first()
|
|
.select('id', 'email', 'username');
|
|
}
|
|
|
|
async function updateUser(id, changes) {
|
|
await db('users')
|
|
.where({ id })
|
|
.update(changes, '*');
|
|
return findUserById(id);
|
|
}
|
|
|
|
function removeUser(id) {
|
|
return db('users')
|
|
.where({ id })
|
|
.del();
|
|
} |