Guest User

Untitled

a guest
Feb 7th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import { Client } from 'pg';
  2. const copyTo = require('pg-copy-streams').to;
  3. const copyFrom = require('pg-copy-streams').from;
  4.  
  5. const fromDb = new Client({
  6. host: 'oldDb.rds.amazonaws.com',
  7. user: 'username',
  8. password: 'password',
  9. database: 'api_logs'
  10. });
  11.  
  12. const toDb = new Client({
  13. host: 'newDb.rds.amazonaws.com',
  14. user: 'username',
  15. password: 'password',
  16. database: 'api_logs'
  17. });
  18. // query to select the api logs for the month of september
  19. let query1 = "COPY (SELECT id, api_key, statuscode, latency, method, time FROM logs WHERE time >= '2017-09-01T00:00:00' AND time < '2017-10-01T00:00:00') TO STDOUT";
  20.  
  21. // query to map the data retrieved above to specific columns
  22. let query2 = "COPY logs_201709 (id, api_key, statuscode, latency, method, time) FROM STDIN";
  23.  
  24. fromDb.connect().then(() => toDb.connect()).then(() => {
  25. let fromStream = fromDb.query(copyTo(query1));
  26. let toStream = toDb.query(copyFrom(query2));
  27. // pipe data from source to destination DB
  28. fromStream.pipe(toStream);
  29. fromStream.on('end', done);
  30. fromStream.on('error',done);
  31. });
  32.  
  33. let done = (err) => {
  34. if (err) console.log('err', err);
  35. console.log('Completed');
  36. }
Add Comment
Please, Sign In to add comment