Coder Social home page Coder Social logo

sql-massive-node's Introduction

sql-massive-node

Today we're going to make our first full CRUD back-end.

I'll be referring to this mini project through these instructions : https://github.com/DevMountain/mini-sql-node-massive

Setup your server

Get your basic server working. Follow the steps from the mini-project to setup a server :

  • Require and setup express and get it listening on a port
  • Require MassiveJS and Connect to a database (make a new one, or use one you've already made).
  • Add db to our express app, put the massive instance on there and export our app (see the mini-project)

Understand your data

We are going to be working with a single table, products. It's schema will look something like this:

  • ID : Primary key number
  • Name : string
  • Description: string
  • Price: number
  • Imageurl : string

Create your .sql files

  • Create a folder called db and place 5 files in there:

    • create_product
    • read_products
    • read_product
    • update_product
    • delete_product
  • Create a working SQL query for each one. You can use pgAdmin to test your queries against the database.

create_product

This query will need to take the 4 parameters defined in the schema and insert a record into the database.

read_products

This query will get all products in the table and return only the name, price, and image url

read_product

This query will take in an id and return all data for that product

update_product

This query will take in an id and a new description. Find the product with the id and update it's description with the new description.

delete_product

This query will take in an id. Find and delete the product with the id.

Run your queries in your controller

  • Create a productsCtrl.js
    • Export an object with 5 functions
      • Create, GetOne, GetAll, Update, Delete
    • At the top of the controller (outside the object), get the db object off of our express app (see mini-project)
    • Inside of Create use the create_product query
    • Inside of GetAll use the read_products query
    • Inside of GetOne, use the read_product query
    • Inside of Update, use the update_product query
    • Inside of Delete, use the delete_product query

Create endpoints

  • Create some endpoints on express. Create one for each query type.

Sample Urls

GET /api/products GET /api/product/:productId PUT /api/product/:productId?desc=.... POST /api/product DELETE /api/product/:productId

Wire your endpoints up to your controller

  • Get any information you need off of the query parameters, query, or body and pass them into your controller
  • You will need to add parameters in your controller functions to receive these values.

Test

  • You should have a working crud app. Use postman to insert a few records, modify them, query them, and delete them to make sure it's all working.

Black Diamond

  • Create an angular front end to interact with your app.
  • Use express static to serve up your angular files from a public folder
  • Create a single view that can insert, read, update, and delete products.
  • Create a 2nd page that just reads the products and displays them in a more pretty way (like Jane.com or amazon).

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.

sql-massive-node's People

Contributors

jrobber avatar qais11 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.