blistovmhz

Untitled

Oct 8th, 2020
1,429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Import required modules
  2. const csv = require('csvtojson')
  3. const path = require('path')
  4. const fs = require('fs')
  5. const {Client} = require('pg')
  6. const copyFrom = require('pg-copy-streams').from
  7. const config = require('./config.json')
  8.  
  9. // Setting file path
  10. const inputFile = path.join(__dirname, 'testfile.csv')
  11. const outputFile = path.join(__dirname, 'testfile.json')
  12.  
  13. // target table
  14. var table = 'drilldata'
  15.  
  16. // Getting connectin parameters from config.json
  17. const host = config.host
  18. const user = config.user
  19. const pw = config.pw
  20. const db = config.db
  21. const port = config.port
  22.  
  23. const conString = `postgres://${user}:${pw}@${host}/${db}`
  24. //const conString = `postgres://${user}:${pw}@${host}:${port}/${db}`
  25.  
  26. const executeQuery = (targetTable) => {
  27.     console.log('Starting executeQuery function')
  28.     // Connecting to Database
  29.     const client = new Client({
  30.         connectionString: conString,
  31.     })
  32.     client.connect()
  33.  
  34.     const execute = (target, callback) => {
  35.         client.query(`Truncate ${target}`, (err) => {
  36.                 if (err) {
  37.                 client.end()
  38.                 callback(err)
  39.                 // return console.log(err.stack)
  40.                 } else {
  41.                 console.log(`Truncated ${target}`)
  42.                 callback(null, target)
  43.                 }
  44.             })
  45.     }
  46.     execute(targetTable, (err) =>{
  47.         if (err) return console.log(`Error in Truncate Table: ${err}`)
  48.         var stream = client.query(copyFrom(`COPY ${targetTable} FROM STDIN`))
  49.         var fileStream = fs.createReadStream(outputFile)
  50.        
  51.         fileStream.on('error', (error) =>{
  52.             console.log(`Error in creating read stream ${error}`)
  53.         })
  54.         stream.on('error', (error) => {
  55.             console.log(`Error in creating stream ${error}`)
  56.         })
  57.         stream.on('end', () => {
  58.             console.log(`Completed loading data into ${targetTable}`)
  59.             client.end()
  60.         })
  61.         fileStream.pipe(stream);
  62.     })
  63. }
  64.  
  65. const main = (inputPath, outputPath) => {
  66.     console.log(`Converting ${inputPath} to JSON.`)
  67.     const convCsv = (inputP, outputP, callback) => {
  68.         buff = ''
  69.         csv()
  70.         .fromFile(inputP)
  71.         .on('data', (data) => {
  72.                 buff += data.toString('utf8')
  73.         })
  74.         .on('done', (error) => {
  75.             // if error happens, callback with error
  76.             if (error) return callback(error)
  77.             // if no error, callback with the converted data
  78.             console.log('Finished conversion.')
  79.             callback(null, buff)
  80.         })
  81.     }
  82.     convCsv(inputPath, outputPath, (error, data) =>{
  83.         // if error happens, gives error message and code stops here.
  84.         if (error) return console.error(`Error in csv conversion: ${error}`)
  85.         // if no error, write file
  86.         fs.writeFileSync(outputPath, data)
  87.         console.log(`File created as ${outputPath}`)
  88.         executeQuery(table)
  89.     })
  90. }
  91.  
  92. main(inputFile, outputFile)
Advertisement
Add Comment
Please, Sign In to add comment