Giter VIP home page Giter VIP logo

massive-demo's Introduction

SETUP

Copy the contents of ./schema.sql, paste it into a script (using SQL Tabs), and execute it.

Mini Project

Step 1

Clone the repo (do not fork it).

Step 2: Install the NPM modules

npm install

Step 3: Test it

Start your application by running:

node server.js

Step 5: Start Postgres

Start your Postgres server.

Step 6: Include the massive-js Dependency

In server.js, add to your list of dependencies:

var massive = require('massive')

Step 7: Connect to Postgres via massive-js

In server.js add code to connect to your database:

massive({
  host: 'ec2-23-21-197-231.compute-1.amazonaws.com',
  port: 5432,
  database: 'd3end9n7irlc8n',
  user: 'ldmvvsyrzvqvax',
  password: 'd3b46f0d5dfbc5d98e53ed6eda29d1076096facb6c5e9ded5ec7b815faed7b64',
  ssl: true
}).then(function(db) {
  app.set('db', db);
});

Step 8: Create a SQL Repository

Massive JS Function Docs

massive-js works by converting your SQL queries, held in files, into JS functions.

For example, the following file, held in the ./db directory of your project:

db/getAllInjuries.sql

SELECT * FROM injuries;

Yields the following function:

app.get("db").getAllInjuries().then(function(injuries) {
  console.log(injuries) // injuries will contain an array of injuries
});

Create the ./db directory, and add a file, getAllIncidents.sql (incidents, not injuries).

Step 9: Query Incidents

Now that you have a repository for SQL queries, add a query to your new file that shows you retrieves the following pieces of information for every incident in your database:

  • incidents.id
  • incidents.us_state
  • injuries.name
  • affected_areas.name
  • causes.name

Step 10: Upgrade the GET Endpoint

Now that you have a way to return basic information about incidents of injuries, upgrade the GET endpoint such that an HTTP request can return the information to a client (like Angular) in your response:

Hint:

app.get("db").getAllInjuries().then(function(injuries) {
  console.log(injuries) // injuries will contain an array of injuries
});

Step 11: Up the Ante

If you've made it this far, great work. Now, upgrade your endpoint again, this time accepting a new query parameter, cause=Sneezing (e.g. any cause). When cause=Sneezing is submitted as part of the same GET request, return the results of a different query, db/getIncidentsByCause.sql.

Make sure the original query works as before.

Hint:

massive-js accepts arguments as part of your SQL using $1, $2, ...

select * from products
where in_stock = $1 and price < $2;

Your arguments can be submitted as an array as the first argument in the function, before the callback.

app.get("db").productsInStock([true, 1000]).then(function(products) {
  // products is a results array
});

Step 12 (Optional): Up the Ante (Again)

Upgrade your GET request to accept by=cause and by=affected_area, with a value=Sneezing or value=Neck.

Step 13: Create a New Incident

Upgrade the POST request to give yourself the ability to create a new incident. Here's a sample request body for Postman:

{
  "us_state": "WV",
  "injury_id": 1,
  "cause_id": 5
}

massive-demo's People

Contributors

kendagriff 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.