Advertisement
Guest User

Untitled

a guest
Aug 9th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. 'use strict';
  2. var pg = require('pg');
  3. var async = require('async');
  4. var grab = require('ps-grab');
  5.  
  6. var faculdades = require('./faculdades.json');
  7. var cursos = require('./cursos.json');
  8.  
  9. console.log(grab('--user'));
  10.  
  11. console.log('Initializing connection');
  12. var config = {
  13. user: grab('--user'),
  14. database: grab('--db'),
  15. password: grab('--password'),
  16. port: grab('--port') || 5432,
  17. max: 10,
  18. idleTimeoutMillis: 30000,
  19. };
  20. var client = new pg.Pool(config);
  21. console.log('Connecting...');
  22. client.connect(function(err) {
  23. if (err) throw err;
  24. async.parallel([
  25. function(cb) {
  26. if (!process.env.skipUniversities) {
  27. console.log('Starting to write universities');
  28. faculdades.forEach(function(each, idx) {
  29. if (each['NO_IES'].indexOf('´') !== -1) {
  30. each['NO_IES'].replace('´', '');
  31. }
  32. client.query(`INSERT INTO universities_mec(name, short_name, state, city) VALUES('${each['NO_IES']}', '${each['SGL_IES']}', '${each['SGL_UF_IES']}', '${each['NO_MUNICIPIO_IES']}')`, function(err, result) {
  33. if (err) {
  34. console.log(`ERROR ON `);
  35. console.log(each);
  36. throw err;
  37. } else console.log(`${each['SGL_IES']} added to the database`);
  38. if (idx + 1 === faculdades.length) {
  39. client.query('INSERT INTO ')
  40. console.log(`Finished writing universities`);
  41. }
  42. });
  43. });
  44. }
  45. },
  46. function(cb) {
  47. console.log('Started writing courses');
  48. cursos.forEach(function(each, idx) {
  49. let name = each['NO_IES'];
  50. if (name.indexOf("'") !== -1) {
  51. name.replace("'", '');
  52. }
  53. client.query(`SELECT * FROM universities_mec WHERE name= '${name}'`, function(err, university) {
  54. if (err) throw err;
  55. if (university.rows.length === 0) return console.log(`${name} não é uma faculdade conhecida`);
  56. let university_id = university.rows[0].id;
  57. client.query(`INSERT INTO courses_mec(name, university_id) VALUES('${each['NO_CURSO']}', ${university_id})`, function(err, result) {
  58. if (err) {
  59. console.log('ERROR ON');
  60. console.log(each);
  61. throw err;
  62. }
  63. console.log(`${each['NO_CURSO']} da faculdade ${each['NO_IES']} criado.`);
  64. if (idx + 1 === cursos.length) {
  65. console.log('Finished writing.');
  66. }
  67. });
  68. })
  69. //client.query(`INSERT INTO cursos_mec(name, university)`)
  70. });
  71. }
  72. ])
  73. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement