Skip to content

Commit de1edf6

Browse files
committed
Merge pull request totaljs#14 from bigz94/master
nedb example
2 parents 3577d37 + ffd145c commit de1edf6

File tree

15 files changed

+296
-0
lines changed

15 files changed

+296
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var app = angular.module('app', ['ngResource']);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function UserCtrl($scope, User) {
2+
3+
$scope.users = User.query();
4+
$scope.isForm = false;
5+
6+
$scope.edit = function (_id) {
7+
$scope.user = User.get({ _id: _id });
8+
$scope.isForm = true;
9+
};
10+
11+
$scope.save = function() {
12+
$scope.user.$save({ _id: $scope.user._id }, function () {
13+
$scope.users = User.query();
14+
});
15+
$scope.isForm = false;
16+
};
17+
18+
$scope.cancel = function() {
19+
$scope.isForm = false;
20+
};
21+
22+
$scope.delete = function(_id) {
23+
24+
User.delete({ _id: _id }, function() {
25+
// Refresh users
26+
$scope.users = User.query();
27+
alert('User was removed.');
28+
});
29+
30+
$scope.isForm = false;
31+
};
32+
33+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*auto*/
2+
3+
body { padding:20px; margin:0; font:normal 12px Arial; }
4+
h2 { margin: 0 0 10px 0; padding: 0; font: normal bold 16px Arial; letter-spacing: -1px; }
5+
a { color: #3484EB; }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
app.factory('User', function ($resource) {
2+
return $resource('/user/:_id', { _id: '@_id' });
3+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
User-agent: *
2+
Allow: /
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<br />
2+
<hr />
3+
<br />
4+
<h2>Edit user</h2>
5+
<br />
6+
<div>Alias:</div>
7+
<div><input type="text" ng-model="user.alias" /></div>
8+
<br />
9+
<div>E-mail:</div>
10+
<div><input type="text" ng-model="user.email" /></div>
11+
<br />
12+
<button ng-click="save()">Save</button><button ng-click="cancel()">cancel</button>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exports.install = function(framework) {
2+
framework.route('/*', view_app);
3+
};
4+
5+
function view_app() {
6+
var self = this;
7+
self.view('app');
8+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
exports.install = function(framework) {
2+
framework.route('/user/', json_user_query);
3+
framework.route('/user/{id}/', json_user_get);
4+
framework.route('/user/{id}/', json_user_save, ['post', 'json']);
5+
framework.route('/user/{id}/', json_user_delete, ['delete']);
6+
};
7+
8+
/*
9+
Description: Get users
10+
Method: GET
11+
Output: JSON
12+
*/
13+
function json_user_query() {
14+
15+
var self = this;
16+
17+
var User = MODEL('user').db
18+
19+
User.find({}, function (err, docs) {
20+
self.json(docs);
21+
});
22+
}
23+
24+
/*
25+
Description: Get user
26+
Method: GET
27+
Output: JSON
28+
*/
29+
function json_user_get(id) {
30+
31+
var self = this;
32+
33+
// self.model('user').Schema;
34+
// framework.model('user').Schema;
35+
var User = MODEL('user').db;
36+
37+
User.findOne({ _id: id }, function (err, doc) {
38+
self.json(doc);
39+
});
40+
41+
}
42+
43+
/*
44+
Description: Save user
45+
Method: POST
46+
Output: JSON
47+
*/
48+
function json_user_save(id) {
49+
50+
var self = this;
51+
52+
// self.model('user').Schema;
53+
// framework.model('user').Schema;
54+
var User = MODEL('user').db;
55+
56+
console.log('save ->', id);
57+
58+
// What is it? https://github.com/totaljs/examples/tree/master/changes
59+
self.change('user: save, id: ' + id);
60+
61+
User.update({_id: id}, self.post, function (err, result) {
62+
if (err) {
63+
self.json({ 'error': 'An error has occurred' });
64+
} else {
65+
self.json(result);
66+
}
67+
});
68+
}
69+
70+
/*
71+
Description: Delete user
72+
Method: DELETE
73+
Output: JSON
74+
*/
75+
function json_user_delete(id) {
76+
77+
var self = this;
78+
79+
// self.model('user').Schema;
80+
// framework.model('user').Schema;
81+
var User = MODEL('user').db;
82+
83+
console.log('delete ->', id);
84+
85+
// What is it? https://github.com/totaljs/examples/tree/master/changes
86+
self.change('user: deleted, id: ' + id);
87+
88+
User.remove({ '_id': id }, function (err, result) {
89+
if (err) {
90+
self.json({ 'error': 'An error has occurred' });
91+
} else {
92+
self.json(result);
93+
}
94+
});
95+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{"alias":"Ashlee Headley","email":"mail@example.ch","_id":"2IWtaFtq1jZ4ZjPz"}
2+
{"alias":"Gemma Cusick","email":"mail@example.ch","_id":"4zgfI0LYvJ3Xky46"}
3+
{"alias":"Ilona Wetherington","email":"mail@example.ch","_id":"7dCYxsTf9r0xNWvQ"}
4+
{"alias":"Sandra Corker","email":"mail@example.ch","_id":"Gg6aDHRMRWb2SeUC"}
5+
{"alias":"Izola Sage","email":"mail@example.ch","_id":"HX9NZIJE7h0iO1jB"}
6+
{"alias":"Tory Windom","email":"mail@example.ch","_id":"Red9xmtrotl9tmpF"}
7+
{"alias":"Beth Ramsburg ","email":"mail@example.ch","_id":"a12QXm0Ls0o88TY2"}
8+
{"alias":"Kym Breese","email":"mail@example.ch","_id":"aGNo5bRwNOycerbt"}
9+
{"alias":"Gwyn Mack","email":"mail@example.ch","_id":"h6drirZrQBRBr897"}
10+
{"alias":"Jerrod Zendejas","email":"mail@example.ch","_id":"ny4Cl1fNmtsOZPqC"}
11+
{"alias":"Emery Luong","email":"mail@example.ch","_id":"rSvngpI8aadFRvRE"}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var Datastore = require('nedb');
2+
3+
db = {};
4+
db.users = new Datastore('database/users.db');
5+
6+
db.users.loadDatabase();
7+
8+
// insert test data
9+
/*
10+
db.users.insert([{
11+
alias: "Hien Schoenborn",
12+
email: "mail@example.ch"
13+
}, {
14+
alias: "Ashlee Headley",
15+
email: "mail@example.ch"
16+
}, {
17+
alias: "Emery Luong",
18+
email: "mail@example.ch"
19+
}, {
20+
alias: "Jerrod Zendejas",
21+
email: "mail@example.ch"
22+
}, {
23+
alias: "Gwyn Mack",
24+
email: "mail@example.ch"
25+
}, {
26+
alias: "Beth Ramsburg ",
27+
email: "mail@example.ch"
28+
}, {
29+
alias: "Izola Sage",
30+
email: "mail@example.ch"
31+
}, {
32+
alias: "Ilona Wetherington",
33+
email: "mail@example.ch"
34+
}, {
35+
alias: "Kym Breese",
36+
email: "mail@example.ch"
37+
}, {
38+
alias: "Sandra Corker",
39+
email: "mail@example.ch"
40+
}, {
41+
alias: "Gemma Cusick",
42+
email: "mail@example.ch"
43+
}, {
44+
alias: "Tory Windom",
45+
email: "mail@example.ch"
46+
}], function (err) { });
47+
*/
48+
49+
global.nedb = db;
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');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var userSchema = { alias: String, ip: String, email: String, password: String, created: Date };
2+
3+
exports.name = 'user';
4+
exports.db = nedb.users;
5+
6+
//mongoose.model('User', userSchema);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
![TOTALSTACK logo](http://www.totaljs.com/img/totalstack.png)
2+
# How to run it?
3+
4+
## First step
5+
6+
```
7+
npm install total.js
8+
npm install nedb
9+
```
10+
11+
## Second step
12+
13+
```
14+
node index
15+
```
16+
17+
Open browser: <http://127.0.0.1:8000>
18+
19+
## Documentation und Information about neDB
20+
https://github.com/louischatriot/nedb#compacting-the-database
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
@{ng('angular', 'resource')}
3+
4+
<!DOCTYPE html>
5+
<html ng-app="app">
6+
<head>
7+
@{meta}
8+
<meta charset="utf-8" />
9+
<meta http-equiv="X-UA-Compatible" content="IE=10" />
10+
<meta name="format-detection" content="telephone=no" />
11+
<meta name="viewport" content="width=1024, user-scalable=yes" />
12+
<meta name="robots" content="all,follow" />
13+
@{head}
14+
@{favicon('favicon.ico')}
15+
</head>
16+
<body>
17+
18+
@{body}
19+
20+
</body>
21+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@{meta('title', 'description', 'keywords')}
2+
3+
<!-- /app/css/ -->
4+
@{ngStyle('app')}
5+
6+
@{ngResource('user')}
7+
8+
<!-- cdnjs -->
9+
@{ngLocale('en-us')}
10+
11+
<!-- /app/controllers/ -->
12+
@{ngController('user')}
13+
14+
<div ng-controller="UserCtrl">
15+
16+
<h2>All users ({{ users.length }})</h2>
17+
<ul>
18+
<li ng-repeat="user in users | limitTo:12"><a href="javascript:void(0)" ng-click="edit(user._id)">edit</a> - <a href="javascript:void(0)" ng-click="delete(user._id)">delete</a> {{ user.alias }}</li>
19+
</ul>
20+
21+
<div ng-include src="'/templates/form.html'" ng-show="isForm"></div>
22+
23+
</div>

0 commit comments

Comments
 (0)