prepare seed script for other data types

This commit is contained in:
2017-01-26 16:23:39 -07:00
parent d058554cf1
commit 6116ee37b3
3 changed files with 88 additions and 57 deletions

View File

@@ -0,0 +1,34 @@
/* eslint-disable no-console */
const https = require('https');
function fetchCustomers(count = 1) {
const url = `https://randomuser.me/api/?nat=us&results=${count}`;
console.log('Seeding from https://randomuser.me/api/');
return new Promise((resolve, reject) => {
let userData = '';
https.get(url, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
userData = `${userData}${chunk}`;
});
res.on('end', () => {
const { results } = JSON.parse(userData);
resolve(results.map(customer => ({
firstname: customer.name.first,
lastname: customer.name.last,
email: customer.email,
address: customer.location.street,
city: customer.location.city,
state: customer.location.state,
zipcode: customer.location.postcode,
})));
});
})
.on('error', err => reject(err));
});
}
module.exports = fetchCustomers;