Merge pull request #144 from rpflorence/master
Syntax highlighting for the README
This commit is contained in:
commit
fe10d8544a
65
README.md
65
README.md
@ -10,60 +10,64 @@ Non-blocking PostgreSQL client for node.js. Pure JavaScript and native libpq bi
|
|||||||
|
|
||||||
### Simple, using built-in client pool
|
### Simple, using built-in client pool
|
||||||
|
|
||||||
var pg = require('pg');
|
```javascript
|
||||||
//or native libpq bindings
|
var pg = require('pg');
|
||||||
//var pg = require('pg').native
|
//or native libpq bindings
|
||||||
|
//var pg = require('pg').native
|
||||||
|
|
||||||
var conString = "tcp://postgres:1234@localhost/postgres";
|
var conString = "tcp://postgres:1234@localhost/postgres";
|
||||||
|
|
||||||
//error handling omitted
|
//error handling omitted
|
||||||
pg.connect(conString, function(err, client) {
|
pg.connect(conString, function(err, client) {
|
||||||
client.query("SELECT NOW() as when", function(err, result) {
|
client.query("SELECT NOW() as when", function(err, result) {
|
||||||
console.log("Row count: %d",result.rows.length); // 1
|
console.log("Row count: %d",result.rows.length); // 1
|
||||||
console.log("Current year: %d", result.rows[0].when.getYear());
|
console.log("Current year: %d", result.rows[0].when.getYear());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
```
|
||||||
|
|
||||||
### Evented api
|
### Evented api
|
||||||
|
|
||||||
var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`
|
```javascript
|
||||||
var conString = "tcp://postgres:1234@localhost/postgres";
|
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);
|
var client = new pg.Client(conString);
|
||||||
client.connect();
|
client.connect();
|
||||||
|
|
||||||
//queries are queued and executed one after another once the connection becomes available
|
//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("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)", ['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)]);
|
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
|
//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
|
//or by passing an options object containing text, (optional) parameter values, and (optional) query name
|
||||||
client.query({
|
client.query({
|
||||||
name: 'insert beatle',
|
name: 'insert beatle',
|
||||||
text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)",
|
text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)",
|
||||||
values: ['George', 70, new Date(1946, 02, 14)]
|
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
|
//subsequent queries with the same name will be executed without re-parsing the query plan by postgres
|
||||||
client.query({
|
client.query({
|
||||||
name: 'insert beatle',
|
name: 'insert beatle',
|
||||||
values: ['Paul', 63, new Date(1945, 04, 03)]
|
values: ['Paul', 63, new Date(1945, 04, 03)]
|
||||||
});
|
});
|
||||||
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);
|
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);
|
||||||
|
|
||||||
//can stream row results back 1 at a time
|
//can stream row results back 1 at a time
|
||||||
query.on('row', function(row) {
|
query.on('row', function(row) {
|
||||||
console.log(row);
|
console.log(row);
|
||||||
console.log("Beatle name: %s", row.name); //Beatle name: John
|
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 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
|
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
|
//fired after last row is emitted
|
||||||
query.on('end', function() {
|
query.on('end', function() {
|
||||||
client.end();
|
client.end();
|
||||||
});
|
});
|
||||||
|
```
|
||||||
|
|
||||||
### Example notes
|
### Example notes
|
||||||
|
|
||||||
@ -164,6 +168,3 @@ Copyright (c) 2010 Brian Carlson (brian.m.carlson@gmail.com)
|
|||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
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
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user