prepare seed script for other data types
This commit is contained in:
34
cli/lib/fetch_customers.js
Normal file
34
cli/lib/fetch_customers.js
Normal 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;
|
||||||
110
cli/seed_db.js
110
cli/seed_db.js
@@ -1,8 +1,12 @@
|
|||||||
/* eslint-disable no-console,import/no-extraneous-dependencies */
|
/* eslint-disable no-console,import/no-extraneous-dependencies */
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const https = require('https');
|
const minimist = require('minimist');
|
||||||
const { host, port } = require('../server/config');
|
const { host, port } = require('../server/config');
|
||||||
|
const fetchCustomers = require('./lib/fetch_customers');
|
||||||
|
|
||||||
|
const argv = minimist(process.argv.slice(2));
|
||||||
|
const modelName = argv._[0];
|
||||||
|
|
||||||
function testConnection(conn) {
|
function testConnection(conn) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@@ -14,68 +18,60 @@ function testConnection(conn) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchCustomers(count = 1) {
|
function populateDatabase(model) {
|
||||||
const url = `https://randomuser.me/api/?nat=us&results=${count}`;
|
console.log('Populating model:', model);
|
||||||
console.log('Seeding from https://randomuser.me/api/');
|
let task = false;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
switch (model.toLowerCase()) {
|
||||||
let userData = '';
|
case 'customers':
|
||||||
|
task = 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',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
https.get(url, (res) => {
|
const req = http.request(options, (res) => {
|
||||||
res.setEncoding('utf8');
|
res.setEncoding('utf8');
|
||||||
res.on('data', (chunk) => {
|
res.on('data', () => {});
|
||||||
userData = `${userData}${chunk}`;
|
res.on('end', resolve);
|
||||||
});
|
});
|
||||||
res.on('end', () => {
|
|
||||||
const { results } = JSON.parse(userData);
|
|
||||||
|
|
||||||
resolve(results.map(customer => ({
|
req.on('error', reject)
|
||||||
firstname: customer.name.first,
|
req.write(JSON.stringify(customer));
|
||||||
lastname: customer.name.last,
|
req.end();
|
||||||
email: customer.email,
|
});
|
||||||
address: customer.location.street,
|
});
|
||||||
city: customer.location.city,
|
|
||||||
state: customer.location.state,
|
return Promise.all(tasks);
|
||||||
zipcode: customer.location.postcode,
|
})
|
||||||
})));
|
.then(results => console.log('Customer(s) added', results.length));
|
||||||
});
|
break;
|
||||||
})
|
default:
|
||||||
.on('error', err => reject(err));
|
throw new Error(`Model not found or supported: ${model}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return task.catch((err) => {
|
||||||
|
console.log('FAILED to populate database', err);
|
||||||
|
process.exit(3);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function populateDatabase() {
|
if (!modelName) {
|
||||||
return fetchCustomers(100)
|
console.log('Please specify a model to populate');
|
||||||
.then((customers) => {
|
process.exit(1);
|
||||||
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 })
|
testConnection({ host, port })
|
||||||
.then(populateDatabase)
|
.then(populateDatabase(modelName))
|
||||||
.catch(() => console.log('Start the server to seed the database!'));
|
.catch(() => {
|
||||||
|
console.log('Start the server to seed the database!');
|
||||||
|
process.exit(2);
|
||||||
|
});
|
||||||
|
|||||||
@@ -35,6 +35,7 @@
|
|||||||
"file-loader": "^0.9.0",
|
"file-loader": "^0.9.0",
|
||||||
"html-webpack-plugin": "^2.26.0",
|
"html-webpack-plugin": "^2.26.0",
|
||||||
"json-loader": "^0.5.4",
|
"json-loader": "^0.5.4",
|
||||||
|
"minimist": "^1.2.0",
|
||||||
"nodemon": "^1.11.0",
|
"nodemon": "^1.11.0",
|
||||||
"nsp": "^2.1.0",
|
"nsp": "^2.1.0",
|
||||||
"rimraf": "^2.5.4",
|
"rimraf": "^2.5.4",
|
||||||
|
|||||||
Reference in New Issue
Block a user