Skip to content

Instantly share code, notes, and snippets.

@MrLeebo
Created January 29, 2015 00:01
Show Gist options
  • Save MrLeebo/447344bb8068b69c60cc to your computer and use it in GitHub Desktop.
Save MrLeebo/447344bb8068b69c60cc to your computer and use it in GitHub Desktop.
assert = require 'assert'
fs = require 'fs'
{ from } = require 'pg-copy-streams'
knex = require './knex' # knex instance with dbConfig
module.exports = (done) ->
runner = new knex.client.Runner(knex.client)
runner.ensureConnection().then (conn) ->
sql = "copy import_data from stdin with csv header null '' delimiter '|' encoding 'windows-1251';"
sqlStream = conn.query(from(sql)).on 'end', ->
knex.raw('select count(*) from import_data').then (results) ->
runner.cleanupConnection()
done(results.rows[0].count)
fs.createReadStream('import_data.txt').pipe(sqlStream)
assert = require 'assert'
fs = require 'fs'
{ from } = require 'pg-copy-streams'
knex = require './knex' # knex instance with dbConfig
module.exports = (done) ->
runner = new knex.client.Runner(knex.client)
runner.ensureConnection().then (conn) ->
sql = "copy import_data from stdin with csv header null '' delimiter '|' encoding 'windows-1251';"
sqlStream = conn.query(from(sql))
fs.createReadStream('import_data.txt').pipe(sqlStream).on 'finish', ->
knex.raw('select count(*) from import_data').then (results) ->
runner.cleanupConnection()
done(results.rows[0].count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment