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

Haidar Mongodb Crud (native driver) #13

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# mongodb-crud
CRUD with MongoDB
# mongoose-crud
CRUD with Mongoose ODM

## How To Use

```
npm install
//install all depedencies
npm start
// node app.js
npm run dev
//nodemon app.js
node seeds/books.js
// run books seeder

```
15 changes: 15 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const books = require('./routes/books');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.use('/books',books);

const port = process.env.PORT || 3000;

app.listen(port,() => {
console.log(`App listening on port ${port}`);
});
58 changes: 58 additions & 0 deletions controllers/BookController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const books = require('../models/books');

module.exports = {
index : (req,res) => {
books.findAll().then((books) => {
res.status(200).json({
message: 'Read All Books',
data: books
});
}).catch((err) => {
console.log(err);
res.status(500).json({
message: 'Something Went Wrong'
});
});
},
create: (req,res) => {
books.create(req.body).then((book) => {
res.status(200).json({
message: 'Success Create New Book',
data: book
});
}).catch((err) => {
console.log(err);
res.status(500).json({
message: 'Something Went Wrong'
});
});
},
destroy: (req,res) => {
const id = req.params.id;
books.destroy(id).then((book) => {
res.status(200).json({
message: 'Success Delete a Book',
data: book
});
}).catch((err) => {
console.log(err);
res.status(500).json({
message: 'Something Went Wrong'
});
});
},
update: (req,res) => {
const id = req.params.id;
books.update(id,req.body).then((book) => {
res.status(200).json({
message: 'Success Update a Book',
data: book
});
}).catch((err) => {
console.log(err);
res.status(500).json({
message: 'Something Went Wrong'
});
});
}
};
98 changes: 98 additions & 0 deletions models/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
const dbName = 'library';
const ObjectId = require('mongodb').ObjectID;
let client;

module.exports = {
findAll : () => {
return new Promise(function(resolve, reject) {
(async function() {
try {
client = await MongoClient.connect(url);
const db = client.db(dbName);
const docs = await db.collection('books').find({}).toArray();
resolve(docs) ;

} catch (err) {
console.log(err.stack);
reject(err);
}
if (client) {
client.close();
}
})();
});
},
create: (input) => {
return new Promise(function(resolve, reject) {
(async function() {
try {
client = await MongoClient.connect(url);
const db = client.db(dbName);
const insert = await db.collection('books').insertOne(input);
const docs = await db.collection('books').find(input).toArray();
resolve(docs) ;

} catch (err) {
console.log(err.stack);
reject(err);
}
if (client) {
client.close();
}
})();
});
},
destroy: (id) => {
return new Promise(function(resolve, reject) {
(async function() {
try {
client = await MongoClient.connect(url);
const db = client.db(dbName);
const docs = await db.collection('books').find({
_id: ObjectId(id)
}).toArray();
const destroy = await db.collection('books').remove({
_id: ObjectId(id)
});
resolve(docs) ;

} catch (err) {
console.log(err.stack);
reject(err);
}
if (client) {
client.close();
}
})();
});
},
update: (id,input) => {
return new Promise(function(resolve, reject) {
(async function() {
try {
client = await MongoClient.connect(url);
const db = client.db(dbName);
const update = await db.collection('books').findOneAndUpdate({
_id: ObjectId(id)
},
{ $set : input},
{
returnOriginal: false,
sort: [['_id',1]],
upsert: true
}
);
resolve(update.value);
} catch (err) {
console.log(err.stack);
reject(err);
}
if (client) {
client.close();
}
})();
});
}
};
Loading