├── README.md ├── app.js ├── models ├── book.js └── genre.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # Simple-NodeJS-MongoDB-Example 2 | Simple Bookstore Example using NodeJS and MongoDB ( get , add , delete , update operations ) 3 | 4 | Project Setup : 5 | 6 | 1. Setup MongoDB on your computer. 7 | 2. Start MongoDB server ( For help : https://docs.mongodb.com/manual/tutorial/) 8 | 3. Create database named `bookstore` 9 | 4. Create collection genres example : `db.genres.insert( { "name" : 'Self Help' } )` 10 | 5. You can insert more datas. 11 | 6. Create collection books example query : `db.books.insert({name:'The Murder House', genre:'Suspene', description:'Test acıklaması', author:'Okan', publisher:'sirket',pages:'480', image_url:'http://i.dr.com.tr/cache/500x400-0/originals/0001700270001-1.jpg',buy_url:'ttp://www.dr.com.tr/Kitap/Kiz-Arkadasim-Dokuz-Kuyruklu-Bir-Tilki/Edebiyat/Roman/Romantik/urunno=0001700270001'})` 12 | 7. After install NodeJS on your computer. 13 | 8. Run `npm install` on command line. 14 | 9. After run `node app`. 15 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | const bodyParser = require('body-parser'); 4 | const mongoose = require('mongoose'); 5 | 6 | Genre = require('./models/genre'); 7 | Book = require('./models/book'); 8 | 9 | app.use(bodyParser.json()); 10 | // Connect to Mongoose 11 | mongoose.connect('mongodb://localhost/bookstore'); 12 | const db = mongoose.connection; 13 | 14 | app.get('/', (req, res) => { 15 | res.send('Please use /api/books or /api/genres'); 16 | }); 17 | 18 | app.get('/api/genres', (req, res) => { 19 | Genre.getGenres((err, genres) => { 20 | if (err) { 21 | throw err; 22 | } 23 | res.json(genres); 24 | }); 25 | }); 26 | app.post('/api/genres', (req, res) => { 27 | const genre = req.body; 28 | Genre.addGenre(genre, (err, genre) => { 29 | if (err) { 30 | throw err; 31 | } 32 | res.json(genre); 33 | }); 34 | }); 35 | app.put('/api/genres/:_id', (req, res) => { 36 | const id = req.params._id; 37 | const genre = req.body; 38 | Genre.updateGenre(id, genre, {}, (err, genre) => { 39 | if (err) { 40 | throw err; 41 | } 42 | res.json(genre); 43 | }); 44 | }); 45 | app.delete('/api/genres/:_id', (req, res) => { 46 | const id = req.params._id; 47 | const genre = req.body; 48 | Genre.removeGenre(id, (err, genre) => { 49 | if (err) { 50 | throw err; 51 | } 52 | res.json(genre); 53 | }); 54 | }); 55 | app.get('/api/books', (req, res) => { 56 | Book.getBooks((err, books) => { 57 | if (err) { 58 | throw err; 59 | } 60 | res.json(books); 61 | }); 62 | }); 63 | app.get('/api/books/:id', (req, res) => { 64 | Book.getBookById(req.params.id, (err, book) => { 65 | if (err) { 66 | throw err; 67 | } 68 | res.json(book); 69 | }); 70 | }); 71 | app.post('/api/books', (req, res) => { 72 | var book = req.body; 73 | Book.addBook(book, (err, book) => { 74 | if (err) { 75 | throw err; 76 | } 77 | res.json(book); 78 | }); 79 | }); 80 | app.put('/api/books/:_id', (req, res) => { 81 | const id = req.params._id; 82 | const book = req.body; 83 | Book.updateBook(id, book, {}, (err, book) => { 84 | if (err) { 85 | throw err; 86 | } 87 | res.json(book); 88 | }); 89 | }); 90 | app.delete('/api/books/:_id', (req, res) => { 91 | const id = req.params._id; 92 | const book = req.body; 93 | Book.removeBook(id, (err, book) => { 94 | if (err) { 95 | throw err; 96 | } 97 | res.json(book); 98 | }); 99 | }); 100 | app.listen(3000); 101 | console.log('Running on port 3000'); 102 | -------------------------------------------------------------------------------- /models/book.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | // Genre Schema 3 | 4 | const bookSchema = mongoose.Schema({ 5 | title: { 6 | type: String, 7 | required: true, 8 | }, 9 | genre: { 10 | type: String, 11 | required: true, 12 | }, 13 | description: { 14 | type: String, 15 | }, 16 | author: { 17 | type: String, 18 | required: true, 19 | }, 20 | publisher: { 21 | type: String, 22 | }, 23 | pages: { 24 | type: String, 25 | }, 26 | image_url: { 27 | type: String, 28 | }, 29 | buy_url: { 30 | type: String, 31 | }, 32 | create_date: { 33 | type: Date, 34 | default: Date.now, 35 | }, 36 | }); 37 | const Book = (module.exports = mongoose.model('Book', bookSchema)); 38 | // Get Genres 39 | module.exports.getBooks = function(callback, limit) { 40 | Book.find(callback).limit(limit); 41 | }; 42 | // Get BOOK by id 43 | module.exports.getBookById = function(id, callback) { 44 | Book.findById(id, callback); 45 | }; 46 | // Add Book 47 | module.exports.addBook = function(book, callback) { 48 | Book.create(book, callback); 49 | }; 50 | 51 | // Update Book 52 | module.exports.updateBook = function(id, book, options, callback) { 53 | var query = { _id: id }; 54 | var update = { 55 | title: book.title, 56 | genre: book.genre, 57 | description: book.description, 58 | author: book.author, 59 | publisher: book.publisher, 60 | page: book.pages, 61 | image_url: book.image_url, 62 | buy_url: book.buy_url, 63 | }; 64 | Book.findByIdAndUpdate(query, update, options, callback); 65 | }; 66 | 67 | // Remove Book 68 | module.exports.removeBook = function(id, callback) { 69 | var query = { _id: id }; 70 | Book.remove(query, callback); 71 | }; 72 | -------------------------------------------------------------------------------- /models/genre.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | // Genre Schema 3 | 4 | var genreSchema = mongoose.Schema({ 5 | name: { 6 | type: String, 7 | required: true, 8 | }, 9 | create_date: { 10 | type: Date, 11 | default: Date.now, 12 | }, 13 | }); 14 | const Genre = (module.exports = mongoose.model('Genre', genreSchema)); 15 | // Get Genres 16 | module.exports.getGenres = function(callback, limit) { 17 | Genre.find(callback).limit(limit); 18 | }; 19 | 20 | // add Genre 21 | module.exports.addGenre = function(genre, callback) { 22 | Genre.create(genre, callback); 23 | }; 24 | 25 | // Update Genre 26 | module.exports.updateGenre = function(id, genre, options, callback) { 27 | var query = { _id: id }; 28 | var update = { 29 | name: genre.name, 30 | }; 31 | Genre.findByIdAndUpdate(query, update, options, callback); 32 | }; 33 | // Remove Genre 34 | module.exports.removeGenre = function(id, callback) { 35 | var query = { _id: id }; 36 | Genre.remove(query, callback); 37 | }; 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bookstore", 3 | "version": "1.0.0", 4 | "description": "Simple bookstore app", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "devDependencies": { 10 | "express": "*", 11 | "body-parser": "*", 12 | "mongoose": "*", 13 | "eslint": "^3.19.0", 14 | "eslint-config-airbnb-base": "^11.1.3", 15 | "eslint-plugin-import": "^2.2.0" 16 | }, 17 | "dependencies": { 18 | "express": "*", 19 | "body-parser": "*", 20 | "mongoose": "*", 21 | "eslint": "^3.19.0", 22 | "eslint-plugin-prettier": "^2.0.1", 23 | "prettier": "^1.2.2", 24 | "stylelint": "^7.10.1", 25 | "stylelint-config-standard": "^16.0.0" 26 | }, 27 | "author": "Okan Davut", 28 | "license": "ISC" 29 | } --------------------------------------------------------------------------------