diff --git a/NoSQL/README.md b/NoSQL/README.md index 569eb953..7b71bac8 100644 --- a/NoSQL/README.md +++ b/NoSQL/README.md @@ -1,54 +1,61 @@ # Bitcamp NoSQL :leaves: -Data is all around us and knowing how to use and manipulate databases is an increasingly important skill in today's technological world. NoSQL platforms are ideal to search, create, and analyze data and make applications with databases. +Data is all around us and knowing how to use and manipulate databases is an increasingly important skill in today's technological world. Think about the amount of data stored and retrieved everyday: what can you do with this information, and how can you accomplish that? NoSQL platforms are ideal to search, create, and analyze data and make applications with databases. -## The Goal 🥅 -**Who?** Built with the audience of college and high school students that have minimal to advanced coding experience in mind. Most do not have experience with NoSQL databases and deploying web applications. +Throughout the course, students will build a complete web app with a frontend (HTML, CSS, JS) and backend (MongoDB Database, Netlify). The web app consists of endpoints that allow users to create and edit surveys and submit and analyze responses. -**What?** Concise and interactive Github Learning Lab that introduces and develops skills related to NoSQL databases with [MongoDB](https://azure.microsoft.com/en-us/services/functions/) +**Prerequisites** -**How?** Students gain experience through building a complete web app with a frontend (HTML, CSS, JS) and backend (MongoDB, JS). [...] -**When?** -* *Starting out:* ... -* *The project:* ... +Basic understanding of a programming language (preferrably JavaScript) -**Where?** Tools used include Github, Git Bash, Atlas Realms, and MongoDB Databases. +**Agenda** -### Project Description -4-week course on using MongoDB, Atlas Realms, and Deploying a Webapp. +* Week 1 gets students familiar with the tools needed to finish this project and introduces them to NoSQL Databases. +* Weeks 2 - 4 is when students start on their project for this course: a web application similar to Google Forms. + +**Tools** + +- Netlify +- MongoDB Atlas +- Postman +- Github +- VSCode ### **Week 1** *** - -📚 **Summary** ... **Learning Objectives** -... +- How to Use GitHub (commits, cloning, branches etc.) +- NoSQL database structure (databases, collections, documents) +- The basics of JSON +- Installing an IDE that supports JS +- Deploy a MongoDB Atlas cluster ### **Week 2** *** - -📚 **Summary** ... **Learning Objectives** -... +- Create abstractions in programming through functions with parameters +- Create endpoints that each has its own function (edit, view, submit) +- How to update MongoDB clusters ### **Week 3** *** - -:books: **Summary** ... **Learning Objectives** -... +- Create HTML/CSS files to style website +- Create UI that integrates the endpoints +- Deploy Web App on Netlify +- Material UI ### **Week 4** *** - -:books: **Summary** ... **Learning Objectives** -... +- Work with Atlas Charts to visualize data on frontend +- Perform statistical analytics on received data +- Create another Function that updates/calculates (?) diff --git a/NoSQL/homework/solutions/week2/README.md b/NoSQL/homework/solutions/week2/README.md new file mode 100644 index 00000000..4d651a0e --- /dev/null +++ b/NoSQL/homework/solutions/week2/README.md @@ -0,0 +1,3 @@ +`npm install mongodb` + +https://www.npmjs.com/package/mongodb diff --git a/NoSQL/homework/solutions/week2/lambda_functions/form.js b/NoSQL/homework/solutions/week2/lambda_functions/form.js new file mode 100644 index 00000000..2b39370e --- /dev/null +++ b/NoSQL/homework/solutions/week2/lambda_functions/form.js @@ -0,0 +1,84 @@ +// ./lambda_functions/pokemon.js + +const MongoClient = require("mongodb").MongoClient; + +const MONGODB_URI = process.env.MONGODB_URI; +const DB_NAME = 'formboiz'; + +let cachedDb = null; + +const connectToDatabase = async (uri) => { + // we can cache the access to our database to speed things up a bit + // (this is the only thing that is safe to cache here) + if (cachedDb) return cachedDb; + + const client = await MongoClient.connect(uri, { + useUnifiedTopology: true, + }); + + cachedDb = client.db(DB_NAME); + + return cachedDb; +}; + +const queryDatabase = async (db) => { + const surveys = await db.collection("surveys").find({}).toArray(); + + return { + statusCode: 200, + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(surveys), + }; + }; + + module.exports.handler = async (event, context) => { + // otherwise the connection will never complete, since + // we keep the DB connection alive + context.callbackWaitsForEmptyEventLoop = false; + + const db = await connectToDatabase(MONGODB_URI); + return queryDatabase(db); + }; + + const pushToDatabase = async (db, data, collection) => { + const collect; + if (collection == "surveys") { + collect = { + content: data.questions, + hash: data.hash, + }; + } + else if (collection == "responses") { + collect = { + content: data.responses, + hash: data.hash, + }; + } + + + if (collect.content && collect.hash) { + await db.collection(collection).insertMany([data]); + return { statusCode: 201 }; + } else { + return { statusCode: 422 }; + } + }; + + module.exports.handler = async (event, context) => { + // otherwise the connection will never complete, since + // we keep the DB connection alive + context.callbackWaitsForEmptyEventLoop = false; + + const db = await connectToDatabase(MONGODB_URI); + + switch (event.httpMethod) { + case "GET": + return queryDatabase(db); + case "POST": + return pushToDatabase(db, JSON.parse(event.body), "surveys"); + default: + return { statusCode: 400 }; + } + }; diff --git a/NoSQL/homework/solutions/week2/netlify.toml b/NoSQL/homework/solutions/week2/netlify.toml new file mode 100644 index 00000000..ac088a0d --- /dev/null +++ b/NoSQL/homework/solutions/week2/netlify.toml @@ -0,0 +1,3 @@ +[build] + # Directory with the serverless Lambda functions + functions = "lambda_functions"