Skip to content

Commit ba11b52

Browse files
committed
Started
0 parents  commit ba11b52

File tree

12 files changed

+585
-0
lines changed

12 files changed

+585
-0
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
node_modules/
2+
bower_components/
3+
4+
.idea/
5+
6+
*.iml
7+
8+
coverage/
9+
10+
rethinkdb/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014-2015 Jason Dobry
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<img src="https://raw.githubusercontent.com/js-data/js-data/master/js-data.png" alt="js-data logo" title="js-data" align="right" width="64" height="64" />
2+
3+
## js-data-examples
4+
5+
This repository contains both client-side and server-side example apps that use [js-data](http://www.js-data.io/).
6+
7+
The `client/` folder contains frontend examples of using js-data with various frameworks and frontend js-data adapters.
8+
9+
The `server/` folder contains backend examples of using js-data with various frameworks and backend js-data adapters.
10+
11+
The Grunt tasks in this repository allow you to start various combinations of frontend and backend examples.
12+
13+
## Grunt Tasks
14+
15+
TODO
16+
17+
## Frontend Examples
18+
19+
- [ ] - [js-data + js-data-angular + Angular.js]()
20+
- [ ] - [js-data + js-data-angular + js-data-localforage + Angular.js]()
21+
- [ ] - [js-data + js-data-angular + js-data-localstorage + Angular.js]()
22+
- [ ] - [js-data + js-data-angular + js-data-firebase + Angular.js]()
23+
- [ ] - [js-data + js-data-http + Ember.js]()
24+
- [ ] - [js-data + js-data-http + Ember.js]()
25+
- [ ] - [js-data + js-data-http + Backbone.js]()
26+
- [ ] - [js-data + js-data-http + React.js]()
27+
28+
## Backend Examples
29+
30+
- [ ] - [js-data + js-data-rethinkdb + Express.js]()
31+
- [ ] - [js-data + js-data-rethinkdb + js-data-redis + Express.js]()
32+
- [ ] - [js-data + js-data-mongodb + Express.js]()
33+
- [ ] - [js-data + js-data-sql + Express.js + MySQL]()
34+
- [ ] - [js-data + js-data-sql + Express.js + PostgreSQL]()
35+
36+
## Community
37+
- [Mailing List](https://groups.io/org/groupsio/jsdata) - Ask your questions!
38+
- [Issues](https://github.com/js-data/js-data-examples/issues) - Found a bug? Feature request? Submit an issue!
39+
- [GitHub](https://github.com/js-data/js-data-examples) - View the source code for js-data.
40+
- [Contributing Guide](https://github.com/js-data/js-data-examples/blob/master/CONTRIBUTING.md)
41+
42+
## Contributing
43+
44+
First, feel free to contact me with questions. [Mailing List](https://groups.io/org/groupsio/jsdata). [Issues](https://github.com/js-data/js-data-examples/issues).
45+
46+
1. Contribute to the issue that is the reason you'll be developing in the first place
47+
1. Fork js-data-examples
48+
1. `git clone https://github.com/<you>/js-data-examples.git`
49+
1. `cd js-data-examples; npm install; bower install;`
50+
1. Write your code
51+
1. Submit a PR and we'll review
52+
53+
## License
54+
55+
The MIT License (MIT)
56+
57+
Copyright (c) 2014-2015 Jason Dobry
58+
59+
Permission is hereby granted, free of charge, to any person obtaining a copy
60+
of this software and associated documentation files (the "Software"), to deal
61+
in the Software without restriction, including without limitation the rights
62+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
63+
copies of the Software, and to permit persons to whom the Software is
64+
furnished to do so, subject to the following conditions:
65+
66+
The above copyright notice and this permission notice shall be included in all
67+
copies or substantial portions of the Software.
68+
69+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
70+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
71+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
72+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
73+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
74+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
75+
SOFTWARE.

client/angular/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html data-ng-app="app">
3+
<head>
4+
<base href="/">
5+
<meta charset="utf-8">
6+
<title>js-data</title>
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
8+
<meta name="fragment" content="!"/>
9+
<meta name="Description" content="js-data example app"/>
10+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
11+
<link rel="stylesheet" type="text/css" href="css/codetrain.css">
12+
13+
<link rel="shortcut icon" href="/favicon.ico">
14+
</head>
15+
<body>
16+
It works!
17+
</body>
18+
</html>

server/rethinkdb/app/app.js

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
var express = require('express');
2+
var http = require('http');
3+
var bodyParser = require('body-parser');
4+
var cookieParser = require('cookie-parser');
5+
var session = require('express-session');
6+
var methodOverride = require('method-override');
7+
var GitHubStrategy = require('passport-github').Strategy;
8+
var path = require('path');
9+
var container = require('./container');
10+
11+
exports.createServer = function () {
12+
return container.resolve(function (safeCall, posts, comments, errorHandler, queryRewrite, passport, config) {
13+
var app = express();
14+
15+
// Simple route middleware to ensure user is authenticated.
16+
// Use this route middleware on any resource that needs to be protected. If
17+
// the request is authenticated (typically via a persistent login session),
18+
// the request will proceed. Otherwise, the user will be redirected to the
19+
// login page.
20+
function ensureAuthenticated(req, res, next) {
21+
if (req.isAuthenticated()) {
22+
return next();
23+
}
24+
res.redirect('/login');
25+
}
26+
27+
function renderIndex(req, res, next) {
28+
res.render('index.html', {}, function (err, html) {
29+
if (err) {
30+
next(err);
31+
} else {
32+
res.send(html);
33+
}
34+
});
35+
}
36+
37+
// Passport session setup.
38+
// To support persistent login sessions, Passport needs to be able to
39+
// serialize users into and deserialize users out of the session. Typically,
40+
// this will be as simple as storing the user ID when serializing, and finding
41+
// the user by ID when deserializing. However, since this example does not
42+
// have a database of user records, the complete GitHub profile is serialized
43+
// and deserialized.
44+
passport.serializeUser(function (user, done) {
45+
done(null, user);
46+
});
47+
48+
passport.deserializeUser(function (obj, done) {
49+
done(null, obj);
50+
});
51+
52+
/**
53+
* Use the GitHubStrategy within Passport.
54+
* Strategies in Passport require a `verify` function, which accept
55+
* credentials (in this case, an accessToken, refreshToken, and GitHub
56+
* profile), and invoke a callback with a user object.
57+
*/
58+
passport.use(new GitHubStrategy({
59+
clientID: config.GITHUB_CLIENT_ID,
60+
clientSecret: config.GITHUB_CLIENT_SECRET,
61+
callbackURL: config.GITHUB_CALLBACK_URL
62+
},
63+
function (accessToken, refreshToken, profile, done) {
64+
// asynchronous verification, for effect...
65+
process.nextTick(function () {
66+
67+
// To keep the example simple, the user's GitHub profile is returned to
68+
// represent the logged-in user. In a typical application, you would want
69+
// to associate the GitHub account with a user record in your database,
70+
// and return that user instead.
71+
return done(null, profile);
72+
});
73+
}
74+
));
75+
76+
// middleware
77+
app.use(queryRewrite);
78+
app.use(bodyParser.json());
79+
app.use(cookieParser());
80+
app.use(bodyParser.urlencoded({ extended: true }));
81+
app.use(methodOverride());
82+
console.log(path.join(__dirname, config.PUBLIC));
83+
app.set('views', path.join(__dirname, config.PUBLIC));
84+
app.set('view engine', 'ejs');
85+
app.engine('html', require('ejs').renderFile);
86+
app.use(session({ secret: 'keyboard cat' }));
87+
app.use(passport.initialize());
88+
app.use(passport.session());
89+
app.use(express.static(path.join(__dirname, config.PUBLIC)));
90+
91+
// app settings
92+
app.enable('trust proxy');
93+
94+
app.get('/', renderIndex);
95+
96+
/******************************/
97+
/********** comments **********/
98+
/******************************/
99+
app.route('/api/comments')
100+
// GET /comments
101+
.get(safeCall(comments.findAll))
102+
// POST /comments
103+
.post(ensureAuthenticated, safeCall(comments.createOne));
104+
105+
app.route('/api/comments/:id')
106+
.get(safeCall(comments.findOneById))
107+
.put(ensureAuthenticated, safeCall(comments.updateOneById))
108+
.delete(ensureAuthenticated, safeCall(comments.deleteOneById));
109+
110+
/*******************************/
111+
/********** posts **********/
112+
/*******************************/
113+
app.route('/api/posts')
114+
.get(safeCall(posts.findAll))
115+
.post(ensureAuthenticated, safeCall(posts.createOne));
116+
117+
app.route('/api/posts/:id')
118+
.get(safeCall(posts.findOneById))
119+
.put(ensureAuthenticated, safeCall(posts.updateOneById))
120+
.delete(ensureAuthenticated, safeCall(posts.deleteOneById));
121+
122+
app.get('/auth/github', passport.authenticate('github'));
123+
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), function (req, res) {
124+
res.redirect('/');
125+
});
126+
app.post('/api/logout', function (req, res) {
127+
req.logout();
128+
res.redirect('/');
129+
});
130+
131+
// redirect all others to the index (HTML5 history)
132+
app.get('*', renderIndex);
133+
134+
app.use(function (err, req, res, next) {
135+
errorHandler(err, req, res, next);
136+
});
137+
138+
return app;
139+
});
140+
};
141+
142+
if (module === require.main || process.NODE_ENV === 'prod') {
143+
var app = exports.createServer();
144+
var server = http.createServer(app);
145+
var config = container.get('config');
146+
147+
server.listen(config.PORT);
148+
149+
var io = require('socket.io').listen(server);
150+
151+
container.register('io', function () {
152+
return io;
153+
});
154+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module.exports = function (Comment, mout) {
2+
3+
return {
4+
/**
5+
* GET /comments
6+
*/
7+
findAll: function (req, res) {
8+
return Comment.findAll(req.query, { useClass: false }).then(function (comments) {
9+
return res.status(200).send(comments).end();
10+
});
11+
},
12+
13+
/**
14+
* GET /comments/:id
15+
*/
16+
findOneById: function (req, res) {
17+
return Comment.find(req.params.id).then(function (comment) {
18+
return res.status(200).send(comment).end();
19+
});
20+
},
21+
22+
/**
23+
* POST /comments
24+
*/
25+
createOne: function (req, res) {
26+
var comment = Comment.createInstance(req.body);
27+
comment.ownerId = req.user.id;
28+
return Comment.create(comment).then(function (comment) {
29+
return res.status(201).send(comment).end();
30+
});
31+
},
32+
33+
/**
34+
* PUT /comments/:id
35+
*/
36+
updateOneById: function (req, res, next) {
37+
return Comment.find(req.params.id).then(function (comment) {
38+
if (comment.ownerId !== req.user.id) {
39+
return next(404);
40+
} else {
41+
delete req.body.created;
42+
delete req.body.updated;
43+
mout.object.deepMixIn(comment, req.body);
44+
return Comment.update(comment.id, comment).then(function (comment) {
45+
return res.status(200).send(comment).end();
46+
});
47+
}
48+
});
49+
},
50+
51+
/**
52+
* DELETE /comments/:id
53+
*/
54+
deleteOneById: function (req, res, next) {
55+
return Comment.find(req.params.id).then(function (comment) {
56+
if (comment.ownerId !== req.user.id) {
57+
return next(404);
58+
} else {
59+
Comment.inject(comment);
60+
return Comment.destroy(comment.id).then(function () {
61+
return res.status(204).end();
62+
});
63+
}
64+
});
65+
}
66+
};
67+
};
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = function (container) {
2+
3+
function sendMessage(event, resource, instance) {
4+
var io = container.get('io');
5+
var message = {
6+
id: instance.id,
7+
ownerId: instance.ownerId,
8+
resource: resource,
9+
event: event
10+
};
11+
if (instance.parentType) {
12+
message.parentType = instance.parentType;
13+
}
14+
if (instance.parentId) {
15+
message.parentId = instance.parentId;
16+
}
17+
if (instance.lessonId) {
18+
message.lessonId = instance.lessonId;
19+
}
20+
io.sockets.emit(event, message);
21+
}
22+
23+
return {
24+
sendCreateMessage: function (resource, instance) {
25+
sendMessage('create', resource, instance);
26+
},
27+
28+
sendDestroyMessage: function (resource, instance) {
29+
sendMessage('destroy', resource, instance);
30+
},
31+
32+
sendUpdateMessage: function (resource, instance) {
33+
sendMessage('update', resource, instance);
34+
}
35+
};
36+
};

server/rethinkdb/app/lib/safeCall.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = function (Promise) {
2+
function safeCall(method) {
3+
return function (req, res, next) {
4+
return Promise.try(function () {
5+
return method(req, res, next);
6+
}).catch(next).error(next);
7+
};
8+
}
9+
10+
return safeCall;
11+
};

0 commit comments

Comments
 (0)