NextFaster Documentation

repository·main·Indexed 26 days ago

https://github.com/ethanniser/nextfaster

A high-performance e-commerce template built with Next.js 15, Server Actions, Partial Prerendering, Drizzle ORM, and Neon Postgres. It features a hierarchical product structure (collections, categories, subcollections, subcategories), hybrid product search using PostgreSQL full-text and trigram indexes, and a JWT-based session authentication system.

Tokens
1.7K
Snippets
5
Records
19
Agent score
89%

What's inside NextFaster

  1. Seed Vercel Postgres with large dataset

    main

    The data/data.zip file contains a data.sql file with the full schema and over 1,000,000 products. Because this data exceeds the free tier limits for Neon on Vercel, you must seed it manually using psql:

    1. Unzip data.zip to obtain data.sql.
    2. Run the following command using your connection string:
    psql "YOUR_CONNECTION_STRING" -f data/data.sql
  2. Set up NextFaster local development environment

    main

    Follow these steps to configure your local environment:

    1. Link to Vercel: Run vc link to connect your local project to your Vercel account.
    2. Pull Environment Variables: Run vc env pull to generate a .env.local file containing your database credentials.
    3. Install Dependencies: Run pnpm install.
    4. Start Development Server: Run pnpm dev.

    Note for DB Migrations: When using drizzle-kit, ensure ?sslmode=required is appended to your POSTGRES_URL environment variable.

    vc link
    vc env pull
    pnpm install
    pnpm dev
  3. Configure Drizzle ORM with PostgreSQL

    main

    The project uses drizzle-kit for database migrations and schema management. The configuration requires a PostgreSQL dialect and points to the schema definition located at ./src/db/schema.ts. Database connection details are provided via the POSTGRES_URL environment variable.

    import { defineConfig } from "drizzle-kit";
    
    export default defineConfig({
      schema: "./src/db/schema.ts",
      dialect: "postgresql",
      dbCredentials: {
        url: process.env.POSTGRES_URL!,
      },
      verbose: true,
      strict: true,
    });
  4. Get product counts for categories and subcategories

    main

    Use these functions to retrieve the number of products in specific scopes:

    • getProductCount(): Returns the total count of all products in the database.
    • getCategoryProductCount(categorySlug): Returns the count of products within a specific category (including nested subcollections/subcategories).
    • getSubcategoryProductCount(subcategorySlug): Returns the count of products within a specific subcategory.

    All count queries are cached for 2 hours.

  5. Sign and verify JWT session tokens

    main

    The session utility provides functions to manually sign and verify JSON Web Tokens (JWT) using the AUTH_SECRET environment variable. Tokens are signed using the HS256 algorithm and are set to expire 1 day after issuance.

    signToken accepts a SessionData object:

    • user: An object containing the user { id: number }.
    • expires: An ISO string representing the expiration time.

    verifyToken returns the decoded SessionData payload.

  6. Initialize the Drizzle database client

    main
    The db instance is a pre-configured Drizzle ORM client designed to interact with a Neon Postgres database via HTTP. It uses the @neondatabase/serverless driver and includes the project's schema for type-safe queries. To use it, ensure the DATABASE_URL environment variable is set in your environment.
  7. Manage user sessions via cookies

    main

    The session utility provides high-level helpers to manage authentication state using Next.js cookies.

    • setSession(user: NewUser): Creates a session token containing the user's ID, signs it, and stores it in a cookie named session. The cookie is configured as httpOnly, secure, and sameSite: 'lax', with a 24-hour expiration.
    • getSession(): Retrieves the session cookie from the request headers and verifies it. Returns the SessionData payload if valid, or null if the cookie is missing or invalid.
  8. Database schema and types for NextFaster

    main

    The database schema is defined using drizzle-orm for PostgreSQL. It includes tables for organizing products through a hierarchical structure: collections -> categories -> subcollections -> subcategories -> products. Additionally, a users table is provided for authentication.

    Key entities and their TypeScript types include:

    • Collection: Represents top-level groups.
    • Category: Belongs to a Collection via collection_id.
    • Subcollection: Belongs to a Category via category_slug.
    • Subcategory: Belongs to a Subcollection via subcollection_id.
    • Product: Belongs to a Subcategory via subcategory_slug. Includes full-text search and trigram indexes on the name field.
    • User: Contains username, passwordHash, and timestamps.