Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrote the express example using the 2.0.0 driver #7

Merged
merged 1 commit into from
Apr 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
317 changes: 153 additions & 164 deletions todo-angular-express/app.js
Original file line number Diff line number Diff line change
@@ -1,211 +1,200 @@
// Import express
var async = require('async');
var express = require('express');
var bodyParser = require('body-parser');
var r = require('rethinkdb');

// Load config for RethinkDB and express
var config = require(__dirname+"/config.js");
var config = require(__dirname + '/config.js');

// Create the application
var app = express();


//For serving the index.html and all the other front-end assets.
app.use(express.static(__dirname + '/public'));

app.use(bodyParser());
app.use(bodyParser.json());

//The REST routes for "todos".
app.route('/todos')
.get(listTodoItems)
.post(createTodoItem);

app.route('/todos/:id')
.get(getTodoItem)
.put(updateTodoItem)
.delete(deleteTodoItem);

// Middleware that will create a connection to the database
app.use(createConnection);
//If we reach this middleware the route could not be handled and must be unknown.
app.use(handle404);

// Define main routes
app.route('/todo/get').get(get);
app.route('/todo/new').put(create);
app.route('/todo/update').post(update);
app.route('/todo/delete').post(del);
//Generic error handling middleware.
app.use(handleError);

// Middleware to close a connection to the database
app.use(closeConnection);

/*
* Create a RethinkDB connection, and save it in req._rdbConn
* Retrieve all todo items.
*/
function createConnection(req, res, next) {
r.connect(config.rethinkdb, function(error, conn) {
if (error) {
handleError(res, error);
}
else {
req._rdbConn = conn;
next();
}
function listTodoItems(req, res, next) {
r.table('todos').orderBy({index: 'createdAt'}).run(req.app._rdbConn, function(err, cursor) {
if(err) {
return next(err);
}

//Retrieve all the todos in an array.
cursor.toArray(function(err, result) {
if(err) {
return next(err);
}

res.json(result);
});
});
}

/*
* Send back a 500 error
* Insert a new todo item.
*/
function handleError(res, error) {
return res.send(500, {error: error.message});
}
function createTodoItem(req, res, next) {
var todoItem = req.body;
todoItem.createdAt = r.now();

console.dir(todoItem);

r.table('todos').insert(todoItem, {returnChanges: true}).run(req.app._rdbConn, function(err, result) {
if(err) {
return next(err);
}

res.json(result.changes[0].new_val);
});
}

/*
* Retrieve all todos
* Get a specific todo item.
*/
function get(req, res, next) {
r.table('todos').orderBy({index: "createdAt"}).run(req._rdbConn, function(error, cursor) {
if (error) {
handleError(res, error)
}
else {
// Retrieve all the todos in an array
cursor.toArray(function(error, result) {
if (error) {
handleError(res, error)
}
else {
res.send(JSON.stringify(result));
}
});
}
next();
});
function getTodoItem(req, res, next) {
var todoItemID = req.params.id;

r.table('todos').get(todoItemID).run(req.app._rdbConn, function(err, result) {
if(err) {
return next(err);
}

res.json(result);
});
}

/*
* Insert a todo
* Update a todo item.
*/
function create(req, res, next) {
var todo = req.body;
todo.createdAt = r.now(); // Set the field `createdAt` to the current time

r.table('todos').insert(todo, {returnVals: true}).run(req._rdbConn, function(error, result) {
if (error) {
handleError(res, error)
}
else if (result.inserted !== 1) {
handleError(res, new Error("Document was not inserted."))
}
else {
res.send(JSON.stringify(result.new_val));
}
next();
});
function updateTodoItem(req, res, next) {
var todoItem = req.body;
var todoItemID = req.params.id;

r.table('todos').get(todoItemID).update(todoItem, {returnChanges: true}).run(req.app._rdbConn, function(err, result) {
if(err) {
return next(err);
}

res.json(result.changes[0].new_val);
});
}

/*
* Update a todo
* Delete a todo item.
*/
function update(req, res, next) {
var todo = req.body;
if ((todo != null) && (todo.id != null)) {
r.table('todos').get(todo.id).update(todo, {returnVals: true}).run(req._rdbConn, function(error, result) {
if (error) {
handleError(res, error)
}
else {
res.send(JSON.stringify(result.new_val));
}
next();
});
}
else {
handleError(res, new Error("The todo must have a field `id`."))
next();
function deleteTodoItem(req, res, next) {
var todoItemID = req.params.id;

r.table('todos').get(todoItemID).delete().run(req.app._rdbConn, function(err, result) {
if(err) {
return next(err);
}

res.json({success: true});
});
}

/*
* Delete a todo
* Page-not-found middleware.
*/
function del(req, res, next) {
var todo = req.body;
if ((todo != null) && (todo.id != null)) {
r.table('todos').get(todo.id).delete().run(req._rdbConn, function(error, result) {
if (error) {
handleError(res, error)
}
else {
res.send(JSON.stringify(result));
}
next();
});
}
else {
handleError(res, new Error("The todo must have a field `id`."))
next();
}
function handle404(req, res, next) {
res.status(404).end('not found');
}

/*
* Close the RethinkDB connection
* Generic error handling middleware.
* Send back a 500 page and log the error to the console.
*/
function closeConnection(req, res, next) {
req._rdbConn.close();
function handleError(err, req, res, next) {
console.error(err.stack);
res.status(500).json({err: err.message});
}

/*
* Store the db connection and start listening on a port.
*/
function startExpress(connection) {
app._rdbConn = connection;
app.listen(config.express.port);
console.log('Listening on port ' + config.express.port);
}

/*
* Connect to rethinkdb, create the needed tables/indexes and then start express.
* Create tables/indexes then start express
*/
r.connect(config.rethinkdb, function(err, conn) {
if (err) {
console.log("Could not open a connection to initialize the database");
console.log(err.message);
process.exit(1);
}
r.table('todos').indexWait('createdAt').run(conn, function(err, result) {
if (err) {
// The database/table/index was not available, create them

r.dbCreate(config.rethinkdb.db).run(conn, function(err, result) {
if ((err) && (!err.message.match(/Database `.*` already exists/))) {
console.log("Could not create the database `"+config.db+"`");
console.log(err);
process.exit(1);
}
console.log('Database `'+config.rethinkdb.db+'` created.');

r.tableCreate('todos').run(conn, function(err, result) {
if ((err) && (!err.message.match(/Table `.*` already exists/))) {
console.log("Could not create the table `todos`");
console.log(err);
process.exit(1);
}
console.log('Table `todos` created.');

r.table('todos').indexCreate('createdAt').run(conn, function(err, result) {
if ((err) && (!err.message.match(/Index `.*` already exists/))) {
console.log("Could not create the index `todos`");
console.log(err);
process.exit(1);
}

console.log('Index `createdAt` created.');

r.table('todos').indexWait('createdAt').run(conn, function(err, result) {
if (err) {
console.log("Could not wait for the completion of the index `todos`");
console.log(err);
process.exit(1);
}
console.log('Index `createdAt` ready.');
console.log("Table and index are available, starting express...");

startExpress();
conn.close();
});
});
});
});
}
else {
console.log("Table and index are available, starting express...");
startExpress();
}
async.waterfall([
function connect(callback) {
r.connect(config.rethinkdb, callback);
},
function createDatabase(connection, callback) {
//Create the database if needed.
r.dbList().contains(config.rethinkdb.db).do(function(containsDb) {
return r.branch(
containsDb,
{created: 0},
r.dbCreate(config.rethinkdb.db)
);
}).run(connection, function(err) {
callback(err, connection);
});

});

function startExpress() {
app.listen(config.express.port);
console.log('Listening on port '+config.express.port);
}
},
function createTable(connection, callback) {
//Create the table if needed.
r.tableList().contains('todos').do(function(containsTable) {
return r.branch(
containsTable,
{created: 0},
r.tableCreate('todos')
);
}).run(connection, function(err) {
callback(err, connection);
});
},
function createIndex(connection, callback) {
//Create the index if needed.
r.table('todos').indexList().contains('createdAt').do(function(hasIndex) {
return r.branch(
hasIndex,
{created: 0},
r.table('todos').indexCreate('createdAt')
);
}).run(connection, function(err) {
callback(err, connection);
});
},
function waitForIndex(connection, callback) {
//Wait for the index to be ready.
r.table('todos').indexWait('createdAt').run(connection, function(err, result) {
callback(err, connection);
});
}
], function(err, connection) {
if(err) {
console.error(err);
process.exit(1);
return;
}

startExpress(connection);
});
20 changes: 10 additions & 10 deletions todo-angular-express/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module.exports = {
rethinkdb: {
host: "localhost",
port: 28015,
authKey: "",
db: "rethinkdb_ex"
},
express: {
port: 3000
}
}
rethinkdb: {
host: 'localhost',
port: 28015,
authKey: '',
db: 'rethinkdb_ex'
},
express: {
port: 3000
}
};
Loading