Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
#node-pg-native
High performance, native bindings between node.js and PostgreSQL via libpq with a clean and modern interface.
$ npm i pg-native
Please note the following code uses no async flow control, promise, or generator modules to make the callback hell go away. I figure this is more straight-forward as an example but in general you probably want to use one of the aforementioned approaches in production.
var Client = require('pg-native')
var client = new Client();
client.connect(function(err) {
if(err) throw err
//text queries
client.query('SELECT NOW() AS the_date', function(err, rows) {
if(err) throw err
console.log(rows[0].the_date) //Tue Sep 16 2014 23:42:39 GMT-0400 (EDT)
//parameterized statements
client.query('SELECT $1::text as twitter_handle', ['@briancarlson'], function(err, rows) {
if(err) throw err
console.log(rows[0].twitter_handle) //@briancarlson
})
//prepared statements
client.prepare('get_twitter', 'SELECT $1::text as twitter_handle', 1, function(err) {
if(err) throw err
//execute the prepared, named statement
client.execute('get_twitter', ['@briancarlson'], function(err, rows) {
if(err) throw err
console.log(rows[0].twitter_handle) //@briancarlson
//execute the prepared, named statement again
client.execute('get_twitter', ['@realcarrotfacts'], function(err, rows) {
if(err) throw err
console.log(rows[0].twitter_handle) //@realcarrotfacts
client.end(function() {
console.log('ended');
})
})
})
})
})
})
Because pg-native
is bound to libpq it is able to provide sync operations for both connection and queries. This is exteremly convienent sometimes.
var Client = require('pg-native')
var client = new Client();
client.connectSync();
//text queries
var rows = client.query('SELECT NOW() AS the_date')
console.log(rows[0].the_date) //Tue Sep 16 2014 23:42:39 GMT-0400 (EDT)
//parameterized queries
var rows = client.query('SELECT $1::text as twitter_handle', ['@briancarlson'])
console.log(rows[0].twitter_handle) //@briancarlson
//prepared statements
client.prepare('get_twitter', 'SELECT $1::text as twitter_handle', 1)
var rows = client.execute('get_twitter', ['@briancarlson'])
console.log(rows[0].twitter_handle) //@briancarlson
var rows = client.execute('get_twitter', ['@realcarrotfacts'])
console.log(rows[0].twitter_handle) //@realcarrotfacts
constructor Client()
Constructs and returns a new Client
instance
client.connect(<params:string>, callback:function(err:Error))
Connect to a PostgreSQL backend server. params is optional and is in any format accepted by libpq. Returns an Error
to the callback
if the connection was unsuccessful. callback
is required.
var client = new Client()
client.connect(function(err) {
if(err) throw err
console.log('connected!')
})
var client2 = new Client()
client2.connect('postgresql://user:password@host:5432/database?param=value', function(err) {
if(err) throw err
console.log('connected with connection string!')
})
client.query(queryText:string, <values:string[]>, callback:Function(err:Error, rows:Object[]))
Execute a query with the text of queryText
and optional parameters specified in the values
array. All values are passed to the PostgreSQL backend server and executed as a parameterized statement. The callback is required and is called with an Error
object in the event of a query error, otherwise it is passed an array of result objects. Each element in this array is a dictionary of results with keys for column names and their values as the values for those columns.
var client = new Client()
client.connect(function(err) {
if (err) throw err
client.query('SELECT NOW()', function(err, rows) {
if (err) throw err
console.log(rows) // [{ "now": "Tue Sep 16 2014 23:42:39 GMT-0400 (EDT)" }]
client.query('SELECT $1::text as name', ['Brian'], function(err, rows) {
if (err) throw err
console.log(rows) // [{ "name": "Brian" }]
client.end()
})
})
})
client.prepare(statementName:string, queryText:string, nParams:int, callback:Function(err:Error))
Prepares a named statement for later execution. You must supply the name of the statement via statementName
, the command to prepare via queryText
and the number of parameters in queryText
via nParams
. Calls the callback with an Error
if there was an error.
var client = new Client()
client.connection(function(err) {
if(err) throw err
client.prepare('saved_statement', 'SELECT $1::text as name`, 1, function(err) {
if(err) throw err
console.log('statement prepared')
client.end()
})
})
client.execute(statementName:string, <values:string[]>, callback:Function(err:err, rows:Object[]))
Executes a previously prepared statement on this client with the name of statementName
, passing it the optional array of query parameters as a values
array. The callback
is mandatory and is called with and Error
if the execution failed, or with the same array of results as would be passed to the callback of a client.query
result.
var client = new Client()
client.connection(function(err) {
if(err) throw err
client.prepare('i_like_beans', 'SELECT $1::text as beans', 1, function(err) {
if(err) throw err
client.execute('i_like_beans', ['Brak'], function(err, rows) {
if(err) throw err
console.log(rows) // [{ "i_like_beans": "Brak" }]
client.end()
})
})
})
client.end(<callback:Function()>
Ends the connection. Calls the optional callback when the connection is terminated.
var client = new Client()
client.connection(function(err) {
if(err) throw err
client.end(function() {
console.log('client ended') // client ended
})
})
client.connectSync(params:string)
Connect to a PostgreSQL backend server. Params is in any format accepted by libpq. Throws an Error
if the connection was unsuccessful.
client.querySync(queryText:string, <values:string[]>) -> results:Object[]
Executes a query with a text of queryText
and optional parameters as values
. Uses a parameterized query if values
are supplied. Throws an Error
if the query fails, otherwise returns an array of results.
client.prepareSync(statementName:string, queryText:string, nParams:int)
Prepares a name statement with name of statementName
and a query text of queryText
. You must specify the number of params in the query with the nParams
argument. Throws an Error
if the statement is un-preparable, otherwise returns an array of results.
client.executeSync(statementName:string, <values:string[]>) -> results:Object[]
Executes a previously prepared statement on this client with the name of statementName
, passing it the optional array of query paramters as a values
array. Throws an Error
if the execution fails, otherwas returns an array of results.
The MIT License (MIT)
Copyright (c) 2014 Brian M. Carlson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A slightly nicer interface to Postgres over node-libpq
The npm package pg-native receives a total of 78,155 weekly downloads. As such, pg-native popularity was classified as popular.
We found that pg-native demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.