Skip to content

Instantly share code, notes, and snippets.

@mickaelw
Last active June 19, 2020 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mickaelw/0a35d5b3a56c0f84c0a3beef97935615 to your computer and use it in GitHub Desktop.
Save mickaelw/0a35d5b3a56c0f84c0a3beef97935615 to your computer and use it in GitHub Desktop.
export class SequelizeCustomerLoader implements CustomerLoader {
async get(id: string): Promise<Customer> {
const customers = await SequelizeCustomerModel
.findAll({
where: { id: id },
limit: 1
})
.then(this.mapToCustomers)
return customers[0] || Promise.reject(new CustomerNotFoundError())
}
private mapToCustomers(customers: SequelizeCustomerModel[]): Customer[] {
return customers.map(customer => new Customer(
customer.id,
customer.name,
customer.email,
customer.phone
))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment