Skip to content

Repository files navigation

Prisma ERD Backend Setup Guide

This backend service integrates PostgreSQL (via Prisma ORM) and Stripe payments for managing user subscriptions and premium content access.


1. Prerequisites

Make sure you have the following installed on your local machine:

  • Node.js (v18 or higher recommended)
  • PostgreSQL Database (or a remote PostgreSQL connection URL)
  • Stripe CLI (for local webhook forwarding and testing)

2. Getting Started

Step 1: Install Dependencies

Navigate to the project root directory and install the package dependencies:

npm install

Step 2: Environment Configuration

Create a .env file in the root directory (you can copy .env.example) and configure your values:

PORT=5000
APP_URL="http://localhost:5000"

# Database Connection URL (PostgreSQL)
DATABASE_URL="postgres://username:password@hostname:port/database?sslmode=require"

# Password Hashing
BCRYPT_SALT_ROUNDS=10

# JWT Auth Configuration
JWT_ACCESS_SECRET="your-jwt-access-secret"
JWT_ACCESS_EXPIRY="1d"
JWT_REFRESH_SECRET="your-jwt-refresh-secret"
JWT_REFRESH_EXPIRY="7d"

# Stripe API Keys (Retrieve from Stripe Dashboard in Test Mode)
STRIPE_PRODUCT_PRICE_ID="price_1Tp..."
STRIPE_SECRET_KEY="sk_test_..."
STRIPE_WEBHOOK_SECRET="whsec_..."

3. Database Schema Setup

Prisma in this project is configured to use the multi-file schema feature (prisma.config.ts), with models split inside the prisma/model directory.

Step 1: Apply Database Migrations

Run the following command to apply all existing migrations to your database:

npx prisma migrate deploy

(Note: If you have modified the schemas in prisma/model and want to create a new migration, run npx prisma migrate dev --name <migration_name> instead.)

Step 2: Generate Prisma Client

Generate the type-safe Prisma client:

npx prisma generate

4. Running the Project

Start the Development Server

Runs the application in watch mode using tsx:

npm run dev

Start Prisma Studio

Launches a browser interface to view and edit database tables at http://localhost:5555:

npm run prisma:studio

5. Stripe Webhook Testing Setup

To test checkout payments and subscription updates locally:

  1. Download and Log In to Stripe CLI: Follow instructions to install Stripe CLI, then authorize it:

    stripe login
  2. Start the Webhook Forwarder: Start forwarding Stripe events to your local server:

    npm run stripe:webhook

    This will output a line like: > Ready! Your webhook signing secret is: whsec_xxxxxxxxxx

  3. Update Webhook Secret: Copy the whsec_... secret and paste it as the STRIPE_WEBHOOK_SECRET inside your .env file.

  4. Trigger Webhook Events: Open a separate terminal to trigger events manually or test checkout through the API:

    • To trigger checkout completion: stripe trigger checkout.session.completed
    • To cancel a subscription: stripe subscriptions cancel <sub_id>

6. Helper & Testing Utility Scripts

We've added testing scripts under the scratch/ directory to help you quickly mock data:

  • Check Database Records: Verify what users and subscriptions currently exist:

    npx tsx scratch/check_db.ts
  • Mock Active Premium Subscriptions: Instantly make all database users premium so they can create premium posts:

    npx tsx scratch/make_premium.ts
  • Clear Subscriptions: Wipe out subscription logs so you can run a clean checkout test from scratch:

    npx tsx scratch/clear_subscriptions.ts

About

A robust backend API built with TypeScript, Express, Prisma, and PostgreSQL, featuring user management, social interactions (posts, comments), and secure payment processing via Stripe.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages