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

feat: Backend setup #1

Draft
wants to merge 14 commits into
base: main
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ The project will be organized in a single GitHub repository with the following s
### **Weekend 1: Backend Development**

- **Setup**: Initialize Node.js with TypeScript, set up project structure.
- **Database**: Design PostgreSQL schema for Users, Habits, and Trackers.
- **Database**: Design SQLite schema for Users, Habits, and Trackers.
- **GraphQL**: Define and implement GraphQL schema and resolvers.
- **Authentication**: Implement JWT-based authentication.
- **Testing**: Write unit tests for API endpoints.
Expand Down
68 changes: 68 additions & 0 deletions docs/DatabaseRelationship.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Database Relationships

This document outlines the relationships between the `User`, `Habit`, `Tracker`, and `Reminder` entities in the habit-tracking application.

## 1. User and Habit

- **Relationship**: **One-to-Many**
- **Explanation**: A single user can have multiple habits, but each habit is associated with only one user.

- **User** entity has a one-to-many relationship with the **Habit** entity.
- **Habit** entity has a many-to-one relationship with the **User** entity.

## 2. Habit and Tracker

- **Relationship**: **One-to-Many**
- **Explanation**: Each habit can have multiple trackers, where each tracker records an instance of the habit being tracked (e.g., daily, weekly).

- **Habit** entity has a one-to-many relationship with the **Tracker** entity.
- **Tracker** entity has a many-to-one relationship with the **Habit** entity.

## 3. Habit and Reminder

- **Relationship**: **One-to-Many**
- **Explanation**: A habit can have multiple reminders to notify the user to perform the habit at specific times.

- **Habit** entity has a one-to-many relationship with the **Reminder** entity.
- **Reminder** entity has a many-to-one relationship with the **Habit** entity.

## 4. User and Tracker

- **Relationship**: **Indirect Relationship Through Habit**
- **Explanation**: A **User** has multiple **Habits**, and each **Habit** has multiple **Trackers**. Thus, there is an indirect one-to-many relationship between **User** and **Tracker**.

## 5. User and Reminder

- **Relationship**: **Indirect Relationship Through Habit**
- **Explanation**: Similar to the **User-Tracker** relationship, a **User** has multiple **Habits**, each of which can have multiple **Reminders**. Thus, the relationship between **User** and **Reminder** is also indirectly one-to-many.

## Visualization of Relationships

Here is a simplified diagram of how these entities relate to each other:

```plaintext
┌───────────┐ ┌────────────┐ ┌────────────┐
│ User │-1─────────────m>│ Habit │-1────────────m->│ Tracker │
└───────────┘ └────────────┘ └────────────┘
1
m
|
v
┌────────────┐
│ Reminder │
└────────────┘
```

**1 User can have many Habits**
**1 Habit can have many Trackers**
**1 Habit can have many Reminders**

### How to Use This Document

1. **Refer** to this document whenever you need a clear understanding of the entity relationships.
2. **Update** the document if there are changes in the schema design in the future.

This version provides a clearer view of the relationships between entities, including a text-based diagram to visualize these connections.
4 changes: 4 additions & 0 deletions habit-tracker-backend/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NODE_ENV=development
PORT=3000
DB_PATH=./data/db.sqlite
JWT_SECRET=my_jwt_super_secret
18 changes: 18 additions & 0 deletions habit-tracker-backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Node modules
node_modules/

# Build artifacts
dist/

# Environment files
.env

# Logs
*.log

# TypeScript artifacts
*.tsbuildinfo

# OS-specific files
.DS_Store
Thumbs.db
1 change: 1 addition & 0 deletions habit-tracker-backend/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.19.1
6 changes: 6 additions & 0 deletions habit-tracker-backend/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 80
}
89 changes: 88 additions & 1 deletion habit-tracker-backend/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,90 @@
# Daily Habit Tracker Backend

TODO
## Node.js Version

This project uses Node.js version specified in the `.nvmrc` file. To ensure consistency, please use [nvm](https://github.com/nvm-sh/nvm) to manage your Node.js versions.

### Using nvm

1. **Install nvm**: Follow the installation instructions from the [nvm repository](https://github.com/nvm-sh/nvm#installing-and-updating).

2. **Use the correct Node.js version**: Run the following command in the project directory to switch to the specified Node.js version:

```bash
nvm use
```

3. **Check Node.js version**: Ensure you are using the correct Node.js version:

```bash
npm run check-node-version
```

4. **Install dependencies**: After switching to the correct Node.js version, install the project dependencies:

```bash
npm install
```

## Project Setup Guide

1. **Clone the Repository**:

```bash
git clone https://github.com/yourusername/daily-habit-tracker.git
cd daily-habit-tracker/habit-tracker-backend
```

2. **Set Up Environment Variables**:
Copy the `.env.sample` file to `.env` and update the values as needed:

```bash
cp .env.sample .env
```

3. **Run Database Migrations**:
Run the following command to set up the database schema:
```bash
npm run migration:run
```

## How to Run the Application

1. **Start the Application**:
Run the following command to start the application:

```bash
npm start
```

This will execute the `startApplication` function in `src/index.ts`, which initializes the database and starts the server.

2. **Verify the Application**:
Open your browser and navigate to `http://localhost:3000` (or the port specified in your `.env` file) to verify that the server is running correctly.

## How to Run the Application in Development Mode

1. **Start the Application in Development Mode**:
Run the following command to start the application in development mode:

```bash
npm run start:dev
```

This will run the backend server and watch for file changes to automatically restart the server when changes are detected.

## Available Scripts

- `npm run build`: Compiles the TypeScript code to JavaScript.
- `npm run start`: Starts the compiled JavaScript application.
- `npm run start:dev`: Runs the application in development mode with file watching.
- `npm run lint`: Runs ESLint to check for code quality issues.
- `npm run format`: Formats the code using Prettier.
- `npm run migration:create`: Creates an empty migration file where you can write your SQL in both UP and Down functions to either schema drop or create the schema.
- `npm run migration:generate`: Generates SQL from your entities and you can find them inside the migrations folder as per `src/config/typeorm.config.ts`.
- `npm run migration:run`: Populates your database with the new database schema as per the migration file generated.
- `npm run migration:revert`: Reverts the last executed migration.
- `npm run schema:sync`: Synchronizes the database schema with the entities.
- `npm run schema:drop`: Drops the database schema.
- `npm run typeorm:cache`: Clears the TypeORM query cache.
- `npm run check-node-version`: Checks if the correct Node.js version is being used.
Binary file added habit-tracker-backend/data/db.sqlite
Binary file not shown.
Empty file added habit-tracker-backend/db.sqlite
Empty file.
10 changes: 10 additions & 0 deletions habit-tracker-backend/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';

export default [
{ files: ['src/**/*.{js,mjs,cjs,ts}'] },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
Loading