Skip to content

Instantly share code, notes, and snippets.

@spinolacastro
Created October 11, 2013 03:31
Show Gist options
  • Select an option

  • Save spinolacastro/6929191 to your computer and use it in GitHub Desktop.

Select an option

Save spinolacastro/6929191 to your computer and use it in GitHub Desktop.
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
mongourl = process.env.OPENSHIFT_MONGODB_DB_URL;
database = process.env.OPENSHIFT_APP_NAME;
var server = new Server(mongourl, {auto_reconnect: true});
db = new Db(database, server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'bikeapi' database");
db.collection('users', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'users' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
exports.findById = function(req, res) {
var id = req.params.id;
console.log('Retrieving user: ' + id);
db.collection('users', function(err, collection) {
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
res.send(item);
});
});
};
exports.findAll = function(req, res) {
db.collection('users', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
};
exports.addUser = function(req, res) {
var user = req.body;
console.log('Adding user: ' + JSON.stringify(user));
db.collection('users', function(err, collection) {
collection.insert(user, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred'});
} else {
console.log('Success: ' + JSON.stringify(result[0]));
res.send(result[0]);
}
});
});
}
exports.updateUser = function(req, res) {
var id = req.params.id;
var user = req.body;
console.log('Updating user: ' + id);
console.log(JSON.stringify(user));
db.collection('users', function(err, collection) {
collection.update({'_id':new BSON.ObjectID(id)}, user, {safe:true}, function(err, result) {
if (err) {
console.log('Error updating user: ' + err);
res.send({'error':'An error has occurred'});
} else {
console.log('' + result + ' document(s) updated');
res.send(user);
}
});
});
}
exports.deleteUser = function(req, res) {
var id = req.params.id;
console.log('Deleting user: ' + id);
db.collection('users', function(err, collection) {
collection.remove({'_id':new BSON.ObjectID(id)}, {safe:true}, function(err, result) {
if (err) {
res.send({'error':'An error has occurred - ' + err});
} else {
console.log('' + result + ' document(s) deleted');
res.send(req.body);
}
});
});
}
/*--------------------------------------------------------------------------------------------------------------------*/
// Populate database with sample data -- Only used once: the first time the application is started.
// You'd typically not find this code in a real-life app, since the database would already exist.
var populateDB = function() {
var users = [
{
name: "Joao da Silva",
email: "[email protected]",
password: "Grenache / Syrah",
},
{
name: "Pedro Bernados",
email: "[email protected]",
password: "GXX234@#$2",
}];
db.collection('users', function(err, collection) {
collection.insert(users, {safe:true}, function(err, result) {});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment