|
1 | 1 | var sys = require('sys');
|
2 |
| -var config = require(__dirname + '/../test/cli'); |
| 2 | +var args = require(__dirname + '/../test/cli'); |
3 | 3 | var pg = require(__dirname + '/../lib');
|
4 |
| -var con = new pg.Connection(); |
5 |
| -var people |
6 |
| -con.connect(config.port, config.host); |
7 |
| -con.on('connect', function() { |
8 |
| - console.log('connected'); |
9 |
| - con.startup({ |
10 |
| - user: config.user, |
11 |
| - database: config.database |
12 |
| - }); |
13 |
| - con.once('readyForQuery', function() { |
14 |
| - config.down===true ? dropTable(con) : createTable(con); |
15 |
| - }); |
16 |
| -}); |
17 | 4 |
|
18 | 5 | var people = [
|
19 | 6 | {name: 'Aaron', age: 10},
|
@@ -44,43 +31,29 @@ var people = [
|
44 | 31 | {name: 'Zanzabar', age: 260}
|
45 | 32 | ]
|
46 | 33 |
|
47 |
| -var makeInsert = function(person) { |
48 |
| - return "insert into person(name, age) values('"+person.name + "', '" + person.age + "')"; |
49 |
| -}; |
50 |
| -var personIndex = 0; |
51 |
| -var createTable = function(con) { |
52 |
| - console.log("creating table 'person'"); |
53 |
| - con.query('create table person (id serial, name varchar(30), age integer)'); |
54 |
| - con.once('readyForQuery', function() { |
55 |
| - console.log('created person table'); |
56 |
| - insertPerson(con); |
| 34 | +var con = new pg.Client({ |
| 35 | + user: args.user, |
| 36 | + password: args.password, |
| 37 | + database: args.database |
| 38 | +}); |
| 39 | +con.connect(); |
| 40 | +if(args.down) { |
| 41 | + console.log("Dropping table 'person'") |
| 42 | + var query = con.query("drop table person"); |
| 43 | + query.on('end', function() { |
| 44 | + console.log("Dropped!"); |
| 45 | + con.end(); |
57 | 46 | });
|
58 |
| -}; |
59 |
| - |
60 |
| -var insertPerson = function(con) { |
61 |
| - if(personIndex < people.length) { |
62 |
| - var query = makeInsert(people[personIndex++]); |
63 |
| - con.query(query); |
64 |
| - con.once('readyForQuery', function() { |
65 |
| - insertPerson(con); |
66 |
| - }); |
67 |
| - } |
68 |
| - else { |
69 |
| - con.query("select * from person"); |
70 |
| - con.on('dataRow', function(row) { |
71 |
| - console.log(row.fields); |
72 |
| - }); |
73 |
| - con.once('readyForQuery', function() { |
74 |
| - con.end(); |
75 |
| - }); |
76 |
| - } |
77 |
| -}; |
78 |
| - |
79 |
| -var dropTable = function(con){ |
80 |
| - console.log("dropping table 'person'"); |
81 |
| - con.query('drop table person'); |
82 |
| - con.once('readyForQuery', function() { |
83 |
| - console.log("dropped table 'person'"); |
| 47 | +} else { |
| 48 | + console.log("Creating table 'person'"); |
| 49 | + con.query("create table person(id serial, name varchar(10), age integer)").on('end', function(){ |
| 50 | + console.log("Created!"); |
| 51 | + console.log("Filling it with people"); |
| 52 | + });; |
| 53 | + people.map(function(person) { |
| 54 | + return con.query("insert into person(name, age) values('"+person.name + "', '" + person.age + "')"); |
| 55 | + }).pop().on('end', function(){ |
| 56 | + console.log("Inserted 26 people"); |
84 | 57 | con.end();
|
85 | 58 | });
|
86 | 59 | }
|
0 commit comments