Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript to Postgress

var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`
var conString = "tcp://postgres:1234@localhost/postgres";

var client = new pg.Client(conString);
client.connect();

//queries are queued and executed one after another once the connection becomes available
client.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)");
client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['Ringo', 67, new Date(1945, 11, 2)]);
client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]);

//queries can be executed either via text/parameter values passed as individual arguments
//or by passing an options object containing text, (optional) parameter values, and (optional) query name
client.query({
  name: 'insert beatle',
  text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)",
  values: ['George', 70, new Date(1946, 02, 14)]
});

//subsequent queries with the same name will be executed without re-parsing the query plan by postgres
client.query({
  name: 'insert beatle',
  values: ['Paul', 63, new Date(1945, 04, 03)]
});
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);

//can stream row results back 1 at a time
query.on('row', function(row) {
  console.log(row);
  console.log("Beatle name: %s", row.name); //Beatle name: John
  console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates
  console.log("Beatle height: %d' %d"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints
});

//fired after last row is emitted
query.on('end', function() { 
  client.end();
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to assign empty function in react component props 
Javascript :: js two array combining with id neasted 
Javascript :: reactjs svg SyntaxError: unknown: Namespace tags are not supported by default 
Javascript :: remove the last character from a string in JavaScript, 
Javascript :: textarea events react testing library 
Javascript :: setTimeout() nodejs 
Javascript :: history.back() and refresh in js 
Javascript :: json validator 
Javascript :: uncaught type error event listener error 
Javascript :: gsap keyframes 
Javascript :: key value pair array in javascript 
Javascript :: lowest common ancestor leetcode 
Javascript :: koa access request body 
Javascript :: How to make a JSON call to an URL 
Javascript :: {"statusCode":400,"error":"Bad Request","message":"Unexpected token o in JSON at position 1"} 
Javascript :: dates in javascript 
Javascript :: python restapi use post json 
Javascript :: how to set random dice image with js 
Javascript :: react event listener 
Javascript :: [Object] node js output 
Javascript :: react native refresh control color 
Javascript :: get search value from reacr route1 
Javascript :: opposite of includes javascript 
Javascript :: javascript got device ip 
Javascript :: Laravel react 404 routes 
Javascript :: javascript this Inside Function with Strict Mode 
Javascript :: convert c# to javascript online 
Javascript :: material ui change icon size on xs screen 
Javascript :: remove last tag in dom javascript 
Javascript :: JavaScript Checking in switch Statement 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =