Skip to content
Open
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
137 changes: 6 additions & 131 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,138 +1,13 @@
# DEV Bootcamp Backend Starter
HSA Final Project - BACKEND Description

### Getting Started
1. artists database: contains artist ID and name (we can add more information after API implementation)

To start your project built off of this repository, click the **Fork** button in the top right of the github GUI. This will create a copy of this repository under your account. Share this repository with your team members so that everyone may work off of it.
2. matches: contains user IDs matched to one another (currently, there is only 1 match per user, and most matches are not injective yet)

### Running the project
3. relations: contains a user ID and the song ID that the user likes (currently, there are some redundancies due to the random generator)

Make sure necessary environment variables have been set, which includes the DB credentials. To create an ElephantSQL database and access the required credentials, see the section on [setting up the development environment](#Set-up-a-development-environment).
4. songs: contains song ID and song information and refers to the artist object

```bash
npm i
npm run dev
```
5. users: contains user ID and some basic user information

## Project Layout

### Project Structure

The repo was built by the general frontend web file structure conventions we use at DEV. It has react-router, apollo-client, and ThemeProvider from styled-components set up in src/App.js.


```
project-repo-name
|
└───node_modules
|
└───data
└───src
│ │ cleanup.js
| | index.js
│ │
│ └───config
| | └───index.js
│ |
│ └───db
│ | └───migrations
│ | └───seeds
│ │
│ └───graphql
│ | └───resolvers.js
│ | └───router.js
│ | └───typeDefs.js
│ | └───Mutation
│ | └───Query
│ |
│ └───lib
│ | └───auth
│ | └───context
│ | └───cronjob
│ | └───formatError
│ | └───knex
│ | └───permissions
│ |
│ └───models
│ └───BaseModel.js
|
└───test
|
| .env
| .eslintrc.json
| .gitignore
| knexfile.js
| nodemon.json
| package-lock.json
| package.json
| README.md
```

### Important Directories and Files

- **src**

- The main directory. Used to store all application code.

- **src/index.js**

- This is where the server itself is set up using Express. Cron jobs can be added here.

- **config/index.js**

- Used to store constants that are important to the functionality of the app (like API urls, API keys, database credentials, etc.)

- **.env**

- Here is where all the sensitive environment variables that are referenced by the config live.

- **migrations**

- This is where all migrations are kept. Make sure to only add migrations with the command `npx knex migrate:make <name>`

- **graphql/typeDefs.js**

- Here is the GraphQL TypeDefs.

- **graphql/Mutation & graphql/Query**

- Under the Mutation and Query folders are all the resolvers. What is here corresponds directly to what is in the TypeDefs.

- **graphql/router.js**

- Here is where the GraphQL router is set up. It handles all the network requests. If you need to apply any middleware. it should be done here.

- **lib**

- This directory contains library code that is used across the app. The only things that are really important to understand are the password and token helpers.

- **models**

- This is where all the Objection models are kept. Make sure to keep this in sync with any changes to the migrations.

## Set up a development environment

- To make development easier, we will use ElephantSQL's free plan to host a different database in the cloud for each engineer. It is always on and never fails so your computer stays free.

- Sign up for a free personal account [here](https://customer.elephantsql.com/login). There is also a log in with Github option.
- Once you have signed in, click the **Create New Instance** button on the top right.
- Add a name, probably something like `app-name-db` and keep it on the free `Tiny Turtle` plan. You shouldn't need to add any payment information! Press **Select Region**.
- Select the `US-East-1` as the region and press **Review**. and then **Create Instance**.
- This should take you back to your list of your created instances (databases). Click on the one you just made.
- Under the Details section you should see **Server**, **User & Default database**, and **Password**. These are the credentials you need to connect to the database.

* Connect to the database

- Run `cp sample.env .env` in your terminal to create the .env file.
- Add all the credentials from ElephantSQL to the corresponding fields in the .env file. It should probably end up looking something like this:
<br />
<br />

```
DB_HOST=salt.db.elephantsql.com
DB_DATABASE=abcdefgh
DB_USER=abcdefgh
DB_PASSWORD=abcdefghabcdefghabcdefghabcdefgh
DB_PORT=5432
NODE_ENV=development
```
18 changes: 18 additions & 0 deletions data/artist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

const password = '$2a$10$rQEY9CNl4OC.UtiyRgKnZeW0KaWnEANMKAxfIpNDQCgiCybm3G1fy'

const casual = require('casual')

casual.define('artist', () => ({
id: casual.uuid,
name: casual.name,
}))

const artistsData = []

for (let i = 0; i < 10; ++i) {
artistsData.push(casual.artist)
}

// console.log(artistsData)
module.exports = artistsData
24 changes: 24 additions & 0 deletions data/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const casual = require('casual')
const userData = require('./users')

casual.define('match', ({ user1Id, user2Id }) => ({
id: casual.uuid,
user1Id,
user2Id,
}))

const matches = []

for (let i = 0; i < 20; ++i) {
const user1Id = casual.random_element(userData).id
let user2Id = null

do {
user2Id = casual.random_element(userData).id
} while (user1Id === user2Id)

matches.push(casual.match({ user1Id, user2Id }))
}

// console.log(matches)
module.exports = matches
23 changes: 23 additions & 0 deletions data/relation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const casual = require('casual')
const userData = require('./users')
const songData = require('./song')

casual.define('relation', ({ personId, songId }) => ({
id: casual.uuid,
personId,
songId,
}))

const relations = []

// problem: there will be redundant pairs
for (let i = 0; i < 50; ++i) {
const personId = casual.random_element(userData).id
const songId = casual.random_element(songData).id


relations.push(casual.relation({ personId, songId }))
}

// console.log(relations)
module.exports = relations
19 changes: 19 additions & 0 deletions data/song.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const casual = require('casual')
const artistsData = require('./artist')

casual.define('song', artistId => ({
id: casual.uuid,
artistId,
title: casual.word,
}))

const songsData = []

for (let i = 0; i < 20; ++i) {
const artistId = casual.random_element(artistsData).id
// console.log(artistId)
songsData.push(casual.song(artistId))
}

// console.log(songsData)
module.exports = songsData
15 changes: 10 additions & 5 deletions data/user.js → data/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ const casual = require('casual')
// 'password' hashed with bcrypt scheme
const password = '$2a$10$rQEY9CNl4OC.UtiyRgKnZeW0KaWnEANMKAxfIpNDQCgiCybm3G1fy'

casual.define('user', () => ({
casual.define('users', () => ({
id: casual.uuid,
email: casual.email,
password,
createdAt: casual.moment,
updatedAt: casual.moment,
password: password,
firstName: casual.first_name,
lastName: casual.last_name,
// birth: casual.date(),
phoneNumber: casual.phone,
age: casual.integer(1, 100),

}))


const userData = []

for (let i = 0; i < 20; ++i) {
userData.push(casual.user)
userData.push(casual.users)
}

// console.log(userData)
module.exports = userData
Loading