Example of mongodb native driver for node js
npm install
npm start
Helpers to perform a crud in selected collection using native driver for mongodb in nodejs (all methods return a promise).
-
get: method that receives a collection name.
-
post: method that receives a collection name and request data.
-
put: method that receives a collection name, document id, and data.
-
delete: method that receives a collection name and an document id.
-
getById: method that receives a collection name and an document id.
Example of get method
const fakeORM = require('./fakeORM');
fakeORM.get('collectionName').then(response => {
console.log(response)
}).catch(error => {
console.log(err)
})
The promise always return an object like this :
{
code: 200 // status code for api by example 200,
data: {...} // Object with data of operation
}
Example with express route
app.get('/users', (req, res) => {
fakeORM.get('users').then(
result => {res.status(result.status).send(result.data);}
).catch(err => {
res.status(err.status).send(err.data);
});
});