Coder Social home page Coder Social logo

mini-birds-finished's Introduction

mini-birds-mongoose (Part II)

Expand on yesterday's project to support Birds and Users.

Objectives

  • Practice embedded relationships.
  • Practice reference relationships.

We will:

  • Add a User model.
  • Add a Bird schema.
  • Refactor our Sighting model to support User via reference, and Bird via an embedded document.
  • Add a POST endpoint for User.
  • Adjust our GET endpoint for the Sighting model.

Prep Step: Change db name

To avoid data collisions, let's change the db name to birds-mongoose-2.

Step 1: Understanding our Data Structure

Identify the type of relationships we will be creating.

Sightings

[{
  "user": "aUserID",
  "birds": [{
    "name": "red breasted merganser",
    "order": "Anseriformes",
    "status": "least concern"
  },
  {
    "name": "cedar waxwing",
    "order": "Passeriformes",
    "status": "least concern"
  }],
  "confirmed": true,
  "numberSeen": 2
},
{
  "user": "aUserID",
  "birds": [{
    "name": "osprey",
    "order": "Accipitriformes",
    "status": "least concern"
  }],
  "confirmed": false,
  "numberSeen": 1
}]

Users

[{
  "email": "[email protected]",
  "username": "homersimpson2000",
  "level": 98,
  "location": "Springfield",
  "member": true
},
{
  "email": "[email protected]",
  "username": "ridnick1",
  "level": 2,
  "location": "Louisiana",
  "member": false
}]

Step 2: Create the Bird schema to be embedded into the Sighting model

In a new file, Bird.js, create a Bird schema using properties from the existing Sighting model. Name, order, and status will be the properties moved to our Bird object.

Step 3: Create the User Model

In a new file, User.js, create a User model with the schema properties email, username, level, location, and member.

Declare a var referencing your User model in server.js.

Step 4: Refactor our Sighting Model

Add a property to the Sighting schema called user that will create a relationship between a User and and Sighting. Each Sighting should be required to be related to only one User. A user may have multiple sightings.

Add another property called bird that will store embedded data related to a specific bird when a new sighting is created.

Step 4: Add POST for User

Add a POST endpoint for creating new users: /api/users. Test it with real data.

Step 5: Refactor Sighting Endpoints

  • POST a new /api/sighting, this time using the new Sighting data and a User id.
  • When GET /api/sighting is requested make sure to populate it with User data before returning it to the client.
  • Make it possible for the client to request sightings for a specific user by through sending the user id as a part of the request query in addition to status.

Copyright

© DevMountain LLC, 2016. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.

mini-birds-finished's People

Contributors

zmckimmins avatar

Watchers

James Cloos avatar  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.