2019-08-01 06:55:37 +00:00
|
|
|
const db = require('../../data/dbConfig');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
add,
|
|
|
|
findBy,
|
|
|
|
findById,
|
2019-08-01 07:42:25 +00:00
|
|
|
update,
|
|
|
|
remove
|
2019-08-01 06:55:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function findBy(filter) {
|
|
|
|
return db('users')
|
|
|
|
.where(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function add(user) {
|
|
|
|
const [id] = await db('users')
|
|
|
|
.insert(user);
|
|
|
|
return findById(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findById(id) {
|
|
|
|
return db('users')
|
|
|
|
.where({ id })
|
|
|
|
.first()
|
|
|
|
.select('id', 'email', 'username');
|
|
|
|
}
|
2019-08-01 07:42:25 +00:00
|
|
|
|
|
|
|
async function update(id, changes) {
|
|
|
|
await db('users')
|
|
|
|
.where({ id })
|
|
|
|
.update(changes, '*');
|
|
|
|
return findById(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove(id) {
|
|
|
|
return db('users')
|
|
|
|
.where({ id })
|
|
|
|
.del();
|
|
|
|
}
|