Last active
December 16, 2017 12:58
-
-
Save vedranjukic/0dbcb0e06a4c3c4cb45c654c99fa12ce to your computer and use it in GitHub Desktop.
User repository Mongo db adapter factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment