-
-
Save estebanrfp/6aa5fde54b10edfa56acd8bfec2d3262 to your computer and use it in GitHub Desktop.
Playing with node-mongodb-native and ECMAScript 6 and Express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by k33g_org on 24/10/14. | |
*/ | |
import mongodb from 'mongodb'; | |
import uuid from 'node-uuid'; | |
/* | |
http://mongodb.github.io/node-mongodb-native/2.0/tutorials/crud_operations/ | |
*/ | |
export default class MongoDbHelper { | |
constructor (url) { | |
this.url = url; | |
this.mongoClient = mongodb.MongoClient; | |
this.db = null; | |
} | |
start (callback) { | |
this.mongoClient.connect(this.url, (err, db) => { | |
this.db = db; | |
callback(db) | |
}); | |
} | |
collection (collectionName) { | |
let mongoDbCollection = this.db.collection(collectionName) | |
let collection = { | |
insert : (model) => { //TODO: insert many | |
return new Promise((resolve, reject) => { | |
model._id = uuid.v1(); | |
mongoDbCollection.insertOne(model, (err, result) => { | |
if (err) { reject(err); } | |
resolve(model); | |
}); | |
}); | |
}, | |
update : (model) => { //TODO: update many | |
return new Promise((resolve, reject) => { | |
mongoDbCollection.updateOne({_id:model._id}, {$set:model}, (err, result) => { | |
if (err) { reject(err); } | |
resolve(model); | |
}); | |
}); | |
}, | |
findAll : () => { //TODO: search | |
return new Promise((resolve, reject) => { | |
mongoDbCollection.find({}).toArray( (err, docs) => { | |
if (err) { reject(err); } | |
resolve(docs); | |
}); | |
}); | |
}, | |
findById : (id) => { | |
return new Promise((resolve, reject) => { | |
mongoDbCollection.findOne({_id: id}, (err, doc) => { | |
if (err) { reject(err); } | |
resolve(doc); | |
}); | |
}); | |
}, | |
delete : (id) => { //TODO: delete many | |
return new Promise((resolve, reject) => { | |
mongoDbCollection.removeOne({_id: id}, (err, result) => { | |
if (err) { reject(err); } | |
resolve(result); | |
}); | |
}); | |
} | |
} | |
return collection; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Express application | |
import express from 'express'; | |
import http from 'http'; | |
import bodyParser from 'body-parser'; | |
import MongoDbHelper from './core/MongoDbHelper'; | |
// Connection URL | |
let url = 'mongodb://localhost:27017/spiele-project'; | |
let mongoDbHelper = new MongoDbHelper(url); | |
let app = express() | |
, http_port = 3008; | |
app.use(express.static(__dirname + '/public')); | |
app.use(bodyParser()); | |
app.get("/humans", (req, res) => { | |
mongoDbHelper.collection("humans").findAll().then((docs)=> { | |
res.send(docs); | |
}).catch((err) => { | |
console.log(err) | |
}) | |
}); | |
app.post("/humans", (req, res) => { | |
let human = req.body; | |
mongoDbHelper.collection("humans").insert(human).then((model) => { | |
res.statusCode = 301; | |
res.header("location", "/humans/"+model._id).end(); | |
}).catch((err) => { | |
console.log(err) | |
}) | |
}); | |
app.put("/humans/:id", (req, res) => { | |
let human = req.body; | |
mongoDbHelper.collection("humans").update(human).then((model) => { | |
res.statusCode = 200; | |
res.send(model); | |
}).catch((error) =>{ | |
console.log("error", error) | |
}); | |
}); | |
app.get("/humans/:id", (req, res) => { | |
mongoDbHelper.collection("humans").findById(req.params.id).then((doc) => { | |
res.send(doc); | |
}).catch((err) => { | |
console.log(err) | |
}) | |
}); | |
app.delete("/humans/:id", (req, res) => { | |
mongoDbHelper.collection("humans").delete(req.params.id).then((result) => { | |
res.statusCode = 200; | |
res.send(result); | |
}).catch((err) => { | |
console.log(err) | |
}); | |
}); | |
mongoDbHelper.start(() => { | |
app.listen(http_port); | |
console.log("Listening on " + http_port); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment