Home

karankurani edited this page Jan 13, 2012 · 6 revisions
Clone this wiki locally

Introduction

This is a pure javascript library to connect to a Vertica database.

Installation

To install use npm

npm install vertica

Example of a simple query

Vertica = require 'vertica'

connection = Vertica.connect user: "username", password: 'password', database: "database", host: 'localhost', (err) ->
  throw err if err
  
  # unbuffered
  query = connection.query "SELECT * FROM table"
  query.on 'fields', (fields) ->
    console.log("Fields:", fields)

  query.on 'row', (row) ->
    console.log(row)

  query.on 'end', (status) ->
    console.log("Finished!", status)

  # buffered
  connection.query "SELECT * FROM table", (err, resultset) ->
    console.log err, resultset.fields, resultset.rows, resultset.status

If you call connection.query multiple times on the same connection object, the queries are queued up internally and executed sequentially.

Closing a connection

Close the connection by calling disconnect on the connection object.

  # buffered
  connection.query "SELECT * FROM table", (err, resultset) ->
    console.log err, resultset.fields, resultset.rows, resultset.status
    connection.disconnect #Disconnect after you are done querying

Batch Inserts using COPY/LCOPY queries

Note: It does not currently support LCOPY queries but you can use the custom handler method (as detailed below) instead to do batch inserts from any machine.

There are three ways to copy data:

q = "COPY table FROM STDIN ..."

Using a file

Note that the file has to exist locally on the vertica machine that you are connecting to.

connection.copy q, "/path/to/file", -> console.log('done')

Using STDIN

connection.copy q, process.stdin, -> console.log('done')

Using a custom handler

copier = (transfer, success, fail) ->
try 
transfer('some data')
transfer('some more data')
success()
catch e
fail(e.message)

connection.copy q, copier, -> console.log('done')