Code Monkey home page Code Monkey logo

polls-node's Introduction

Polls-Node Project Documentation

Introduction

Polls-Node is an application for creating and voting on polls in real-time, using Fastify, Node.js, Redis, TypeScript, Docker, PostgreSQL, and WebSockets. This documentation describes the components, functionalities, and how to set up and use the application.

Technologies Used

  • Fastify: Web framework for building efficient APIs.
  • Node.js: Execution environment for JavaScript.
  • Redis: In-memory database for caching and storing user information.
  • TypeScript: Typed superset of JavaScript.
  • Docker: Platform for developing, shipping, and running applications in containers.
  • PostgreSQL: Relational database management system.
  • Prisma: ORM (Object-Relational Mapping) for interacting with the database.
  • WebSocket: Bidirectional real-time communication protocol.

Environment Setup

Prerequisites

  • Node.js and npm installed
  • Docker and Docker Compose installed

Installation

  1. Clone the repository:

    git clone https://github.com/yan-pi/polls-node.git
  2. Install the dependencies:

    cd polls-node
    pnpm install
  3. Configure the environment variables:

    Create a .env file in the project root with the following variables:

    DATABASE_URL="postgresql://<user>:<password>@localhost:5432/polls?schema=public"

    Note: Replace <username> and <password> with your PostgreSQL credentials.

  4. Start the services with Docker Compose:

    docker-compose up -d
  5. Running migrations

    npx prisma migrate dev

    This will run all migrations creating the tables and other configs inside the database.

Project Structure

polls-node/
|-- src/
|   |-- http/
|   |   |-- poll.ts
|   |   |-- routes/
|   |   |    |-- create-poll.ts
|   |   |    |-- get-poll.ts
|   |   |    |-- vote-on-poll.ts
|   |-- ws/
|   |   |-- poll-results.ts
|   |-- index.ts
|-- .env
|-- docker-compose.yml
|-- tsconfig.json

Features

1. Create Poll

  • Endpoint: POST /poll
  • Description: Creates a new poll in the PostgreSQL database.

Example Usage

{
  "question": "What is your favorite animal?",
  "options": ["Dog", "Cat", "Fish"]
}

2. Get Poll

  • Endpoint: GET /polls/:pollId
  • Description: Retrieves the details of a specific poll.

3. Vote on Poll

  • Endpoint: POST /polls/:pollId/vote
  • Description: Records the user's vote on a poll and updates the results in real-time via WebSocket.

Example Usage

{
  "option": "Dog"
}

4. WebSockets

  • Endpoint: WS /polls/:pollId/results
  • Functionality: Real-time update of poll results.
  • Implementation: Utilizes WebSocket to send updated results to clients in real-time.

Redis Login Cache

User login is stored in the Redis cache for authentication and authorization.

ORM and Database

We use Prisma as the ORM for interacting with PostgreSQL. The database configuration is in the prisma/schema.prisma file.

Execution

After setting up the environment, run the following command to start the server:

pnpm start

The server will be running on port 3333.

Conclusion

Polls-Node is a robust application for creating and voting on polls in real-time, using modern and efficient technologies. With a well-defined and documented architecture, it is easy to understand and extend its functionalities.

polls-node's People

Contributors

yan-pi avatar

Watchers

 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.