{"size": 914, "ext": "js", "max_stars_repo_path": "users/users-model.js", "max_stars_repo_name": "Build-Week-Essentialism-2-Bayron/back-end", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "users/users-model.js", "max_issues_repo_name": "Build-Week-Essentialism-2-Bayron/back-end", "max_issues_repo_issues_event_min_datetime": "2021-05-11T14:54:32.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T11:36:39.000Z", "max_forks_repo_path": "users/users-model.js", "max_forks_repo_name": "Build-Week-Essentialism-2-Bayron/back-end", "max_forks_repo_forks_event_min_datetime": "2020-05-20T23:00:37.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-20T23:00:37.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 15.7586206897, "max_line_length": 54, "alphanum_fraction": 0.6039387309}
const db = require('../data/dbConfig.js');
const bcrypt = require('bcryptjs');

function insert(user) {
  return db('users')
    .insert(user, 'id')
    .then(([id]) => id);
};

function findBy(where) {
  return db('users').where(where);
};

function findByUsername(username) {
  return findBy({ username }).first();
};

function find() {
  return db('users');
};


function add(user) {
  user.password = bcrypt.hash(user.password, 14)
  const [id] = db('users').insert(user)

  return findById(id)
};

function findById(id) {
  return db('users').where({ id }).first('id')
};


function update(id, changes) {
  return db('users')
    .where({ id })
    .update(changes)
    .then(count => (count > 0 ? this.get(id) : null));
};

function remove(id) {
  return db('users')
    .where({ id })
    .del()
};

module.exports = {
  insert,
  findBy,
  findByUsername,
  find,
  findById,
  add,
  update,
  remove 
};
