@@ -40,28 +40,48 @@ fully TDD and with lots of love.
40
40
41
41
client.connect();
42
42
client.on('drain', client.end.bind(client));
43
-
44
- var printRow = function(row) {
45
- console.log(row);
46
- };
47
-
43
+
44
+ //queries are queued on a per-client basis and executed one at a time
45
+ client.query("create temp table user(heart varchar(10), birthday timestamptz);");
46
+
47
+ //parameters are always parsed & prepared inside of PostgreSQL server
48
+ //using unnamed prepared statement (no sql injection! yay!)
49
+ client.query({
50
+ text: 'INSERT INTO user(heart, birthday) VALUES ($1, $2)',
51
+ values: ['big', new Date(2031, 03, 03)]
52
+ });
53
+ client.query({
54
+ text: 'INSERT INTO user(heart, birthday) VALUES ($1, $2)',
55
+ values: ['filled with kisses', new Date(2010, 01, 01)]
56
+ });
57
+
48
58
var simpleQuery = client.query("select * from user where heart = 'big'");
49
- simpleQuery.on('row', printRow);
59
+ simpleQuery.on('row', function(row){
60
+ console.log(row.heart); //big
61
+ console.log(row.birthday.getYear()); //2031
62
+ });
50
63
51
64
var preparedStatement = client.query({
52
65
name: 'user by heart type',
53
66
text: 'select * from user where heart = $1',
54
67
values: ['big']
55
68
});
56
- preparedStatement.on('row', printRow);
69
+
70
+ preparedStatement.on('row', function(row){
71
+ console.log(row.heart); //big
72
+ console.log(row.birthday.getYear()); //2031
73
+ });
57
74
58
75
var cachedPreparedStatement = client.query({
59
76
name: 'user by heart type',
60
- //you can omit the text the 2nd time, but you don't have to
77
+ //you can omit the text the 2nd time if using a named query
61
78
values: ['filled with kisses']
62
79
});
63
- cachedPreparedStatement.on('row', printRow);
64
80
81
+ cachedPreparedStatement.on('row', function(row){
82
+ console.log(row.heart); //filled with kisses
83
+ console.log(row.birthday.getYear()); //2010
84
+ });
65
85
66
86
### Philosophy
67
87
0 commit comments