add customer seed script

This commit is contained in:
2017-01-26 16:11:31 -07:00
parent c24686fc99
commit d058554cf1
2 changed files with 83 additions and 4 deletions

81
cli/seed_db.js Normal file
View File

@@ -0,0 +1,81 @@
/* eslint-disable no-console,import/no-extraneous-dependencies */
const net = require('net');
const http = require('http');
const https = require('https');
const { host, port } = require('../server/config');
function testConnection(conn) {
return new Promise((resolve, reject) => {
const client = net.connect(conn);
client.on('connect', () => client.end());
client.on('error', err => reject(err));
client.on('end', () => resolve(true));
});
}
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));
});
}
function populateDatabase() {
return fetchCustomers(100)
.then((customers) => {
const tasks = customers.map((customer) => {
return new Promise((resolve, reject) => {
const options = {
host,
port,
path: '/api/Customers',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
};
const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', () => {});
res.on('end', resolve);
});
req.on('error', reject)
req.write(JSON.stringify(customer));
req.end();
});
});
return Promise.all(tasks);
})
.catch(err => console.log('FAILED to populate database', err));
}
testConnection({ host, port })
.then(populateDatabase)
.catch(() => console.log('Start the server to seed the database!'));

View File

@@ -5,7 +5,7 @@
"ACL": 1, "ACL": 1,
"RoleMapping": 1, "RoleMapping": 1,
"Role": 1, "Role": 1,
"Customer": 2 "Customer": 1
}, },
"models": { "models": {
"User": {}, "User": {},
@@ -13,8 +13,6 @@
"ACL": {}, "ACL": {},
"RoleMapping": {}, "RoleMapping": {},
"Role": {}, "Role": {},
"Customer": { "Customer": {}
"1": "{\"firstname\":\"Demo\",\"lastname\":\"User\",\"email\":\"demo@user.com\",\"id\":1}"
}
} }
} }