@@ -10,60 +10,64 @@ Non-blocking PostgreSQL client for node.js. Pure JavaScript and native libpq bi
10
10
11
11
### Simple, using built-in client pool
12
12
13
- var pg = require('pg');
14
- //or native libpq bindings
15
- //var pg = require('pg').native
16
-
17
- var conString = "tcp://postgres:1234@localhost/postgres";
18
-
19
- //error handling omitted
20
- pg.connect(conString, function(err, client) {
21
- client.query("SELECT NOW() as when", function(err, result) {
22
- console.log("Row count: %d",result.rows.length); // 1
23
- console.log("Current year: %d", result.rows[0].when.getYear());
24
- });
25
- });
13
+ ``` javascript
14
+ var pg = require (' pg' );
15
+ // or native libpq bindings
16
+ // var pg = require('pg').native
17
+
18
+ var conString = " tcp://postgres:1234@localhost/postgres" ;
19
+
20
+ // error handling omitted
21
+ pg .connect (conString, function (err , client ) {
22
+ client .query (" SELECT NOW() as when" , function (err , result ) {
23
+ console .log (" Row count: %d" ,result .rows .length ); // 1
24
+ console .log (" Current year: %d" , result .rows [0 ].when .getYear ());
25
+ });
26
+ });
27
+ ```
26
28
27
29
### Evented api
28
30
29
- var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`
30
- var conString = "tcp://postgres:1234@localhost/postgres";
31
-
32
- var client = new pg.Client(conString);
33
- client.connect();
34
-
35
- //queries are queued and executed one after another once the connection becomes available
36
- client.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)");
37
- client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['Ringo', 67, new Date(1945, 11, 2)]);
38
- client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]);
39
-
40
- //queries can be executed either via text/parameter values passed as individual arguments
41
- //or by passing an options object containing text, (optional) parameter values, and (optional) query name
42
- client.query({
43
- name: 'insert beatle',
44
- text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)",
45
- values: ['George', 70, new Date(1946, 02, 14)]
46
- });
47
-
48
- //subsequent queries with the same name will be executed without re-parsing the query plan by postgres
49
- client.query({
50
- name: 'insert beatle',
51
- values: ['Paul', 63, new Date(1945, 04, 03)]
52
- });
53
- var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);
54
-
55
- //can stream row results back 1 at a time
56
- query.on('row', function(row) {
57
- console.log(row);
58
- console.log("Beatle name: %s", row.name); //Beatle name: John
59
- console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates
60
- console.log("Beatle height: %d' %d\"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints
61
- });
62
-
63
- //fired after last row is emitted
64
- query.on('end', function() {
65
- client.end();
66
- });
31
+ ``` javascript
32
+ var pg = require (' pg' ); // native libpq bindings = `var pg = require('pg').native`
33
+ var conString = " tcp://postgres:1234@localhost/postgres" ;
34
+
35
+ var client = new pg.Client (conString);
36
+ client .connect ();
37
+
38
+ // queries are queued and executed one after another once the connection becomes available
39
+ client .query (" CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)" );
40
+ client .query (" INSERT INTO beatles(name, height, birthday) values($1, $2, $3)" , [' Ringo' , 67 , new Date (1945 , 11 , 2 )]);
41
+ client .query (" INSERT INTO beatles(name, height, birthday) values($1, $2, $3)" , [' John' , 68 , new Date (1944 , 10 , 13 )]);
42
+
43
+ // queries can be executed either via text/parameter values passed as individual arguments
44
+ // or by passing an options object containing text, (optional) parameter values, and (optional) query name
45
+ client .query ({
46
+ name: ' insert beatle' ,
47
+ text: " INSERT INTO beatles(name, height, birthday) values($1, $2, $3)" ,
48
+ values: [' George' , 70 , new Date (1946 , 02 , 14 )]
49
+ });
50
+
51
+ // subsequent queries with the same name will be executed without re-parsing the query plan by postgres
52
+ client .query ({
53
+ name: ' insert beatle' ,
54
+ values: [' Paul' , 63 , new Date (1945 , 04 , 03 )]
55
+ });
56
+ var query = client .query (" SELECT * FROM beatles WHERE name = $1" , [' John' ]);
57
+
58
+ // can stream row results back 1 at a time
59
+ query .on (' row' , function (row ) {
60
+ console .log (row);
61
+ console .log (" Beatle name: %s" , row .name ); // Beatle name: John
62
+ console .log (" Beatle birth year: %d" , row .birthday .getYear ()); // dates are returned as javascript dates
63
+ console .log (" Beatle height: %d' %d\" " , Math .floor (row .height / 12 ), row .height % 12 ); // integers are returned as javascript ints
64
+ });
65
+
66
+ // fired after last row is emitted
67
+ query .on (' end' , function () {
68
+ client .end ();
69
+ });
70
+ ```
67
71
68
72
### Example notes
69
73
164
168
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
165
169
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
166
170
THE SOFTWARE.
167
-
168
-
169
-
0 commit comments