8000 Updated example. · JavaScriptExpert/examples@e2a50b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit e2a50b7

Browse files
committed
Updated example.
1 parent 9f06609 commit e2a50b7

File tree

5 files changed

+23
-40
lines changed

5 files changed

+23
-40
lines changed

postgresql/config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Connection string
2+
database : postgres://user:password@localhost:5432/mydatabasename

postgresql/controllers/default.js

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
11
exports.install = function() {
2-
F.route('/', view_homepage);
2+
F.route('/', view_homepage);
33
};
44

55
function view_homepage() {
66

7-
var self = this;
7+
var self = this;
88

9-
// definitions/postgresql.js
10-
// create a DB connection
11-
DATABASE('test', function(err, client, done) {
9+
// Documentation: https://github.com/petersirka/node-sqlagent
10+
var sql = DB();
1211

13-
if(err != null) {
14-
self.throw500(err);
15-
return;
16-
}
17-
18-
// Table schema = { Id: Number, Age: Number, Name: String };
19-
client.query('SELECT * FROM users', function(err, rows) {
20-
21-
// Close connection
22-
done();
23-
24-
if (err != null) {
25-
self.throw500(err);
26-
return;
27-
}
28-
29-
// Shows the result on a console window
30-
console.log(rows);
31-
32-
// Send rows as the model into the view
33-
self.view('index', rows);
34-
});
35-
36-
});
12+
sql.select('users', 'tbl_user').make(function(builder) {
13+
builder.sort('name');
14+
builder.where('isremoved', false);
15+
builder.take(10);
16+
});
3717

18+
sql.exec(function(err, response) {
19+
if (err)
20+
return self.throw500(err);
21+
self.view('index', response.users);
22+
});
3823
}

postgresql/definitions/postgresql.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
var pg = require('pg.js');
2-
3-
// override the framework prototype
4-
// use CONFIG files for connection string
5-
F.database = function(dbName, callback) {
6-
return new pg.connect('postgres://user:password@localhost:5432/' + dbName, callback);
7-
};
1+
// Documentation: https://github.com/petersirka/node-sqlagent#initialization-for-totaljs
2+
require('sqlagent/pg').init(CONFIG('database'), false);

postgresql/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
You must install module pg (https://github.com/brianc/node-postgres)
22

33
```
4-
$ npm install pg.js
4+
$ npm install pg
5+
$ npm install sqlagent
56
```

postgresql/views/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</colgroup>
88
@{foreach var m in model}
99
<tr>
10-
<td>@{m.Id}</td>
11-
<td>@{m.Age}</td>
12-
<td>@{m.Name}</td>
10+
<td>@{m.id}</td>
11+
<td>@{m.age}</td>
12+
<td>@{m.name}</td>
1313
</tr>
1414
@{end}
1515
</table>

0 commit comments

Comments
 (0)
0