Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New MongoDB Project #101

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions NoSQL/README.md
Original file line number Diff line number Diff line change
@@ -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 (?)
3 changes: 3 additions & 0 deletions NoSQL/homework/solutions/week2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`npm install mongodb`

https://www.npmjs.com/package/mongodb
84 changes: 84 additions & 0 deletions NoSQL/homework/solutions/week2/lambda_functions/form.js
Original file line number Diff line number Diff line change
@@ -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 };
}
};
3 changes: 3 additions & 0 deletions NoSQL/homework/solutions/week2/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
# Directory with the serverless Lambda functions
functions = "lambda_functions"