Last active
June 19, 2020 05:13
-
-
Save mickaelw/0a35d5b3a56c0f84c0a3beef97935615 to your computer and use it in GitHub Desktop.
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
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