database.build Documentation

repository·main·Indexed 25 days ago

https://github.com/supabase-community/database-build

An in-browser Postgres sandbox powered by PGlite (WASM) that enables instant creation of Postgres databases stored in IndexedDB. Features include AI-driven table generation from CSVs, report and chart generation, and database diagramming. The project consists of a Next.js web application, a browser proxy for TCP connections via Web Sockets, and a deploy worker for exporting databases to platforms like Supabase.

Tokens
15.2K
Snippets
27
Records
118
Agent score
84%

What's inside database.build

  1. Overview of database.build

    main

    database.build is an in-browser Postgres sandbox that provides AI assistance. It allows users to instantly spin up unlimited Postgres databases that run directly in the browser using PGlite, a WASM version of Postgres. Data is persisted in IndexedDB, ensuring changes remain after a page refresh.

    Key features include:

    • Drag-and-drop CSV import with automatic table generation.
    • AI-powered report generation and export.
    • AI-powered chart generation.
    • Database diagram building.
  2. Understand @database.build/web architecture

    main

    The application provides an in-browser Postgres sandbox using PGlite. The architecture relies on two distinct database instances stored locally in the browser via IndexedDB:

    1. Meta DB: Tracks user databases and message history.
    2. User DB: A dedicated instance for each database created by the user, containing their specific tables and data.

    To prevent main thread blocking, every PGlite instance runs within a Web Worker. Note that because data is stored in IndexedDB, databases are not persisted to the cloud and are device-specific.

  3. Monorepo Project Structure

    main

    The database.build repository is organized as a monorepo containing the following core projects:

    • Web (./apps/web/): The primary Next.js web application.
    • Browser proxy (./apps/browser-proxy/): Proxies Postgres TCP connections back to the browser using pg-gateway and Web Sockets.
    • Deploy worker (./apps/deploy-worker/): Handles deployment of in-browser databases to external platforms (currently supporting Supabase).
  4. Configure Supabase Vault secrets for the Certificate function

    main

    The Certificate function's cron job requires two specific secrets to be stored in Supabase Vault:

    1. supabase_url: The Supabase API URL.
    2. supabase_functions_certificate_secret: A shared secret used to authorize the trigger of the 'certificate' Supabase Edge Function.
    select vault.create_secret('<supabase_url>', 'supabase_url', 'Supabase API URL');
    select vault.create_secret(encode(gen_random_bytes(24), 'base64'), 'supabase_functions_certificate_secret', 'Shared secret to trigger the "certificate" Supabase Edge Function');
  5. Run database.build in development mode

    main

    To start the development environment, run the following command from the monorepo root. This uses turbo to manage dependencies and automatically build required packages in ./packages/* before building the applications in ./apps/*.

    Note: If you bypass turbo, you must manually build each package in ./packages/* before the applications in ./apps/* can function.

    npm run dev
  6. Set up the Browser Proxy for development

    main

    To run the Browser Proxy locally, configure your environment variables by copying .env.example to .env. You must set the correct environment variables as specified in the example file.

    Once configured, run the dev server from the monorepo root. When running, the proxy will listen on the following ports:

    • 5432 (Postgres TCP)
    • 443 (Web Sockets)

    This setup enables communication between a PGlite instance in the browser and a standard PostgreSQL client.

  7. Set up the database.build development environment

    main

    Follow these steps from the monorepo root to set up your local development environment:

    1. Install dependencies:

      npm i
    2. Start local Supabase stack:

      npx supabase start
    3. Configure Web environment variables: Extract your local Supabase URL and anon key into ./apps/web/.env.local:

      npx supabase status -o env \
        --override-name api.url=NEXT_PUBLIC_SUPABASE_URL \
        --override-name auth.anon_key=NEXT_PUBLIC_SUPABASE_ANON_KEY | \
          grep NEXT_PUBLIC >> ./apps/web/.env.local
    4. Configure OpenAI API key: Create an OpenAI API key and add it to ./apps/web/.env.local:

      echo 'OPENAI_API_KEY="<openai-api-key>"' >> ./apps/web/.env.local
    5. Configure local KV (Redis) variables: Add these exact values to ./apps/web/.env.local:

      echo 'KV_REST_API_URL="http://localhost:8080"' >> ./apps/web/.env.local
      echo 'KV_REST_API_TOKEN="local_token"' >> ./apps/web/.env.local
    6. Start local Redis containers (for rate limiting):

      docker compose -f ./apps/web/docker-compose.yml up -d
    7. Complete remaining variables: Check the .env.example files in the following directories to fill in any remaining required variables:

      • ./apps/web/.env.example
      • ./apps/browser-proxy/.env.example
      • ./apps/deploy-worker/.env.example
    npm i
    npx supabase start
    npx supabase status -o env --override-name api.url=NEXT_PUBLIC_SUPABASE_URL --override-name auth.anon_key=NEXT_PUBLIC_SUPABASE_ANON_KEY | grep NEXT_PUBLIC >> ./apps/web/.env.local
    echo 'OPENAI_API_KEY="<openai-api-key>"' >> ./apps/web/.env.local
    echo 'KV_REST_API_URL="http://localhost:8080"' >> ./apps/web/.env.local
    echo 'KV_REST_API_TOKEN="local_token"' >> ./apps/web/.env.local
    docker compose -f ./apps/web/docker-compose.yml up -d
  8. Schedule weekly certificate renewal with pg_cron

    main

    Use pg_cron to schedule a weekly task (e.g., every Sunday at 00:00) that calls the Certificate Edge Function via net.http_post. The request must include the domainName in the JSON body and use the supabase_functions_certificate_secret for Bearer authentication.

    select cron.schedule (
      'certificates',
      -- every Sunday at 00:00
      '0 0 * * 0',
      $$
      -- certificate for the browser proxy
      select net.http_post(
        url:=(select supabase_url() || '/functions/v1/certificate'),
        headers:=('{"Content-Type": "application/json", "Authorization": "Bearer ' || (select supabase_functions_certificate_secret()) || '"}')::jsonb,
        body:='{"domainName": "browser.db.build"}'::jsonb
      ) as request_id
      $$
    );