Skip to content

Instantly share code, notes, and snippets.

@k33g
Last active July 9, 2018 14:28
Show Gist options
  • Save k33g/9960d96ec5f7d78a67e4 to your computer and use it in GitHub Desktop.
Save k33g/9960d96ec5f7d78a67e4 to your computer and use it in GitHub Desktop.
Playing with node-mongodb-native and ECMAScript 6 and Express
/**
* 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;
}
}
//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