vedranjukic
12/13/2017 - 9:17 PM

User repository Mongo db adapter factory

User repository Mongo db adapter factory

//
//  UserRepositoryMongo factory implementing UserRepository interface
//
export default db => {
  const users = db.get('users')
  
  const create = async (user) => {
    const { userId } = user 
    await users.insert(user)
  }
  
  const getById = async (userId) => {
    const user = await users.findOne({userId})
    if (!user) {
      throw new UserNotFoundException() 
    }
    return user
  }
  
  const save = async (user) => {
    const { userId } = user 
    await users.update({
      userId
    }, user)
  }
  
  return {
    create,
    getById,
    save
  }
}