Code Monkey home page Code Monkey logo

mongo-bears's Introduction

Mongo Bears

Topics:

  • Databases collection of data,organized,easy to get information out of it

  • MongoDB

  • ODMs

  • Mongoose

  • Performing CRUD operations. query: a way to store/retrieve data. asking questions about data. or executing commands to manage data.

DBMS(DB Managment System): software that provides a way to store/retrieve data .

client<>API<>DB server noSql(not only sql): a type of database key-value pair graph document<= mongoDB js const user ={ username:'admin', password:'secert' } mongoDB Server databases(lambda) collectiosa(users, roles,products) documents({_id: 'hfjhajhfhf', username:'admin'}) fields:_id, username why mongoDB popular mature js end to end dyamic schemas(shape of the data{[properties and data types]}) cons dynamic schemas clientAPI(driver)[DB Server]

Description

We will use Node.js and Express to write an API that can perform CRUD (Create, Read, Update and Destroy) operations on Bears stored in a MongoDB Database.

Software Requirements

For this project you need to have MongoDB Community Edition installed and running. Having a local instance of MongoDB running on your system is the preferred option.

Alternatively, you can sign up for an account from a Database As A Service (DBAAS) provider like MongoDB Atlas or mlab. Both DBAAS providers offer a free tier with 500MB size limit that can be used for development and testing.

Using a Local MongoDB Server

If you don't have MongoDB installed, please click on this link for instructions on how to install and run the Community Server and the mongo shell. Follow the instructions for your Operating System.

After MongoDB is installed, follow the instructions on the documentation to start the server. Then run the mongo shell from a separate terminal and execute the show dbs command. If all goes well you should see a list of available databases, similar to the sample below.

 > show dbs
 admin  0.000GB
 local  0.000GB

Getting the Starter Files

  1. Fork and Clone this repository.
  2. CD into the folder where you cloned the repository.
  3. Type yarn or npm install to download all dependencies listed inside package.json.
  4. After all dependencies finish downloading without errors, type yarn start or npm start to start the server.

Use Postman to Test the API.

  1. Make a GET Request to http://localhost:5000 using Postman. The response should be the following JSON object:
{
  "api": "running"
}

Connect API Server to MongoDB

Use yarn or npm to add mongoose to to the project.

Inside server.js, require mongoose and use it to connect your API to the beardb database in your MongoDB Server (local or remote). If the beardb database does not exist, it will be created automatically by MongoDB.

When the connection to MongoDB succeeds, log the following message to the console: "Successfully Connected to MongoDB".

If there is an error connecting to the database, log the following message to the console: "Database connection failed".

Create Bear Schema

In a separate file, create a Schema for the bears collection. Each Bear document should conform to the following object structure:

{
  species: "American Black Bear", // String, required
  latinName: "Ursus americanus",  // String, required
  createdOn: Mon Aug 14 2017 12:50:16 GMT-0700 (PDT) // Date, required, defaults to current date
}

Generate Bear Model

Use mongoose to generate a Bear model that can be used to perform operations on bear documents. Remember to export the model to make it available for importing into other parts of the application.

Create API Endpoints

Configure the API to respond to the following routes:

Method Endpoint Description
POST /api/bears Creates a bear using the information sent inside the request body.
GET /api/bears Returns an array of all the bear objects contained in the database.
GET /api/bears/:id Returns the bear object with the specified id.
DELETE /api/bears/:id Removes the bear with the specified id and returns the deleted bear.
PUT /api/bears/:id Updates the bear with the specified id using data from the request body. Returns the modified document, NOT the original.

Endpoint Specifications

When the client makes a POST request to /api/bears:

  • If the request body is missing the species or latinName property:

    • cancel the request.
    • respond with HTTP status code 400 (Bad Request).
    • return the following JSON response: { errorMessage: "Please provide both species and latinName for the bear." }.
  • If the information about the Bear is valid:

    • save the new Bear the the database.
    • return HTTP status code 201 (Created).
    • return the newly created Bear Document.
  • If there's an error while saving the Bear:

    • cancel the request.
    • respond with HTTP status code 500 (Server Error).
    • return the following JSON object: { errorMessage "There was an error while saving the bear to the database" }.

When the client makes a GET request to /api/bears:

  • If there's an error in retrieving the Bears from the database:
    • cancel the request.
    • respond with HTTP status code 500.
    • return the following JSON object: { errorMessage "The bear information could not be retrieved." }.

When the client makes a GET request to /api/bears/:id:

  • If the Bear with the specified id is not found:

    • return HTTP status code 404 (Not Found).
    • return the following JSON object: { message: "The bear with the specified ID does not exist." }.
  • If there's an error in retrieving the Bear from the database:

    • cancel the request.
    • respond with HTTP status code 500.
    • return the following JSON object: { errorMessage "The bear information could not be retrieved." }.

When the client makes a DELETE request to /api/bears/:id:

  • If the Bear with the specified id is not found:

    • return HTTP status code 404 (Not Found).
    • return the following JSON object: { errorMessage: "The bear with the specified ID does not exist." }.
  • If there's an error in removing the Bear from the database:

    • cancel the request.
    • respond with HTTP status code 500.
    • return the following JSON object: { errorMessage "The bear could not be removed" }.

When the client makes a PUT request to /api/bears/:id:

  • If the Bear with the specified id is not found:

    • return HTTP status code 404 (Not Found).
    • return the following JSON object: { message: "The bear with the specified ID does not exist." }.
  • If there's an error when updating the Bear:

    • cancel the request.
    • respond with HTTP status code 500.
    • return the following JSON object: { errorMessage "The bear information could not be modified." }.

Additional Notes

Stop the MongoDB database server when not in use to save computer resources.

mongo-bears's People

Contributors

luishrd avatar april7229 avatar karthikv avatar mixelpixel avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.