Skip to content

Commit 6ddfce3

Browse files
committed
updated script to use client api instead of connection api
1 parent 8c6bc80 commit 6ddfce3

File tree

1 file changed

+23
-50
lines changed

1 file changed

+23
-50
lines changed

script/create-test-tables.js

+23-50
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
var sys = require('sys');
2-
var config = require(__dirname + '/../test/cli');
2+
var args = require(__dirname + '/../test/cli');
33
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-
});
174

185
var people = [
196
{name: 'Aaron', age: 10},
@@ -44,43 +31,29 @@ var people = [
4431
{name: 'Zanzabar', age: 260}
4532
]
4633

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();
5746
});
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");
8457
con.end();
8558
});
8659
}

0 commit comments

Comments
 (0)