-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
85 lines (70 loc) · 2.19 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var formful = require('../lib/formful'),
resourceful = require('resourceful'),
Creature = resourceful.define('creature');
Creature.property('awesome', Boolean, { default: false });
Creature.property('type', String, { default: "dragon", enum: ["Dragon", "Unicorn", "Pony"] });
Creature.property('email', String, { format: "email" });
Creature.property('life', Number, { default: 10, min: 0, max: 20 });
Creature.feed = function (_id, options, callback) {
var self = this;
self.get(_id, function(err, creature){
if(err) {
return callback(err);
}
var life = creature.life + 1;
self.update(_id, { life: life }, function(err, result){
callback(null, creature.id + ' has been fed. life is: ' + result.life);
});
});
}
Creature.feed.remote = true;
Creature.hit = function (_id, options, callback) {
var self = this;
self.get(_id, function(err, creature){
if(err) {
return callback(err);
}
var life = creature.life - 1;
self.update(_id, { life: life }, function(err, result){
callback(null, creature.id + ' has been hit. life is: ' + result.life);
});
});
}
Creature.hit.remote = true;
var Database = resourceful.define('database', function(){
this.string('type', {
description: "The type of the database",
enum: ["couch", "mongo", "redis", "mysql"],
required: true,
message: "Database type should be valid"
});
this.number('port', {
description: "The port your node app should listen on",
default: 8080,
minimum: 1,
maximum: 65535,
message: "Port should be valid",
editable: false
});
this.string('host', {
description: "The host your node app should listen on",
format: "host-name",
minLenght: 1,
default: "localhost",
editable: false
});
this.string('username', {
description: "The username used to connect to the database",
editable: false
});
this.string('password', {
description: "The password used to connect to the database",
default: "",
format: "password",
editable: false
});
//this.object('metadata', { private: true });
});
formful.createServer([Creature, Database]).listen(8000, function () {
console.log(' > formful server started on port 8000');
});