-
Notifications
You must be signed in to change notification settings - Fork 23
/
express-example.js
172 lines (126 loc) · 4.06 KB
/
express-example.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* A simple example of how to use Waterline v0.10 with Express
*/
//////////////////////////////////////////////////////////////////
// Install dependencies:
// npm install waterline
// npm install sails-orientdb
// npm install express
// npm install body-parser
// npm install method-override
//////////////////////////////////////////////////////////////////
var express = require('express'),
app = express(),
Waterline = require('waterline'),
bodyParser = require('body-parser'),
methodOverride = require('method-override');
// Instantiate a new instance of the ORM
var orm = new Waterline();
//////////////////////////////////////////////////////////////////
// WATERLINE CONFIG
//////////////////////////////////////////////////////////////////
// Require any waterline compatible adapters here
var orientAdapter = require('sails-orientdb');
// Build A Config Object
var config = {
// Setup Adapters
// Creates named adapters that have have been required
adapters: {
'default': orientAdapter,
orient: orientAdapter,
},
// Build Connections Config
// Setup connections using the named adapter configs
connections: {
myLocalOrient: {
adapter: 'orient',
host: 'localhost',
port: 2424,
user: 'root',
password: 'root',
database: 'waterline-express'
}
},
defaults: {
migrate: 'alter'
}
};
//////////////////////////////////////////////////////////////////
// WATERLINE MODELS
//////////////////////////////////////////////////////////////////
var User = Waterline.Collection.extend({
identity: 'user',
connection: 'myLocalOrient',
attributes: {
first_name: 'string',
last_name: 'string'
}
});
var Pet = Waterline.Collection.extend({
identity: 'pet',
connection: 'myLocalOrient',
attributes: {
name: 'string',
breed: 'string'
}
});
// Load the Models into the ORM
orm.loadCollection(User);
orm.loadCollection(Pet);
//////////////////////////////////////////////////////////////////
// EXPRESS SETUP
//////////////////////////////////////////////////////////////////
// Setup Express Application
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
// Build Express Routes (CRUD routes for /users)
app.get('/users', function(req, res) {
app.models.user.find().exec(function(err, models) {
if(err) return res.json({ err: err }, 500);
res.json(models);
});
});
app.post('/users', function(req, res) {
app.models.user.create(req.body, function(err, model) {
if(err) return res.json({ err: err }, 500);
res.json(model);
});
});
app.get('/users/:id', function(req, res) {
app.models.user.findOne({ id: req.params.id }, function(err, model) {
if(err) return res.json({ err: err }, 500);
res.json(model);
});
});
app.delete('/users/:id', function(req, res) {
app.models.user.destroy({ id: req.params.id }, function(err) {
if(err) return res.json({ err: err }, 500);
res.json({ status: 'ok' });
});
});
app.put('/users/:id', function(req, res) {
// Don't pass ID to update
delete req.body.id;
app.models.user.update({ id: req.params.id }, req.body, function(err, model) {
if(err) return res.json({ err: err }, 500);
res.json(model);
});
});
//////////////////////////////////////////////////////////////////
// START WATERLINE
//////////////////////////////////////////////////////////////////
// Start Waterline passing adapters in
orm.initialize(config, function(err, models) {
if(err) throw err;
app.models = models.collections;
app.connections = models.connections;
// Start Server
app.listen(3000);
console.log();
console.log('To list saved users, visit http://localhost:3000/users');
console.log();
console.log('To retrieve a specific user, visit http://localhost:3000/users/<id>');
console.log('Note: OrientDB ids need to be URI encoded, so #11:0 would be accessed at http://localhost:3000/users/%2311%3A0');
console.log('You can add users with curl, e.g.: curl --data "first_name=john&last_name=doe" http://localhost:3000/users');
});