Skip to content

Commit 21229e5

Browse files
committed
added new example
1 parent 01f286c commit 21229e5

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed

mongodb/config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// PLEASE:
2+
// !!! Database is readonly !!!
3+
// This database uses also: https://github.com/totaljs/demo
4+
5+
database : mongodb://test:test@ds029979.mongolab.com:29979/totaldemo

mongodb/controllers/default.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
exports.install = function(framework) {
2+
framework.route('/', view_homepage);
3+
};
4+
5+
function view_homepage() {
6+
var self = this;
7+
8+
var users = DATABASE('users');
9+
10+
// or
11+
// var users = framework.database('user');
12+
// or
13+
// var users = self.database('user');
14+
15+
users.find({}).limit(10).toArray(function(err, docs) {
16+
self.view('homepage', docs);
17+
});
18+
19+
// or
20+
// users.find().limit(10).toArray(self.callback('homepage'));
21+
}

mongodb/definitions/database.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var MC = require('mongodb').MongoClient;
2+
var DB = null;
3+
4+
MC.connect(CONFIG('database'), function(err, db) {
5+
if (err)
6+
throw err;
7+
DB = db;
8+
});
9+
10+
framework.database = function(collection) {
11+
if (collection)
12+
return DB.collection(collection);
13+
return DB;
14+
};

mongodb/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ===================================================
2+
// IMPORTANT: only for development
3+
// total.js - web application framework for node.js
4+
// http://www.totaljs.com
5+
// ===================================================
6+
7+
require('total.js').http('debug');

mongodb/readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# How to run it?
2+
3+
## First step
4+
5+
```
6+
npm install total.js
7+
npm install mongodb
8+
```
9+
10+
## Second step
11+
12+
```
13+
node index
14+
```
15+
16+
Open browser: <http://127.0.0.1:8000>

mongodb/views/_layout.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html ng-app="app">
3+
<head>
4+
@{meta}
5+
<meta charset="utf-8" />
6+
<meta http-equiv="X-UA-Compatible" content="IE=10" />
7+
<meta name="format-detection" content="telephone=no" />
8+
<meta name="viewport" content="width=1024, user-scalable=yes" />
9+
<meta name="robots" content="all,follow" />
10+
@{head}
11+
</head>
12+
<body>
13+
@{body}
14+
</body>
15+
</html>

mongodb/views/homepage.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{meta('Title')}
2+
3+
<div style="padding:20px;font-family:Arial">
4+
<h1>Users</h1>
5+
@{foreach m in model}
6+
<div>@{m.alias}</div>
7+
@{end}
8+
</div>

0 commit comments

Comments
 (0)