spinspire/pocketbase-sveltekit-starter

repository·master·Indexed 19 days ago

https://github.com/spinspire/pocketbase-sveltekit-starter

A high-performance JAMstack starter kit combining a static SvelteKit frontend with a PocketBase backend (Golang/SQLite). It supports two backend setup methods: a standard release for JavaScript extensions or a custom Go build for deeper customizations. Features include a configuration-driven hook system for triggering commands or webhooks, Docker-based development with modd for Go live-reloading, and automated TypeScript type regeneration to keep the frontend in sync with the database schema.

Tokens
8.3K
Snippets
35
Records
41
Agent score
68%

What's inside pocketbase-sveltekit-starter

  1. Extending PocketBase with Go or JavaScript

    master

    You can extend the backend functionality in two ways:

    • JavaScript Hooks: Easy development using JS. Example logic can be found in ./pb/pb_hooks/main.pb.ts. The server automatically hot-reloads when files in ./pb/pb_hooks change.
    • Golang Extensions: Full performance. You can implement custom API endpoints (e.g., /api/hello) and database event hooks (e.g., executing Go handlers when a row is created) by modifying main.go. If using modd, the server will hot-reload when Go code changes.
  2. Configure automated hooks via the hooks table

    master

    This project implements a configuration-driven hook system using a hooks table. This allows you to trigger external actions (commands or webhooks) in response to insert, update, or delete events on specific collections.

    The hooks table schema:

    • collection: The name of the collection that triggers the action.
    • event: The event type (insert, update, or delete).
    • action_type:
      • command: Runs a local program or script. The record is passed via STDIN as JSON.
      • post: Sends a POST request to a webhook URL. The record is sent as the JSON request body with Content-Type: application/json.
    • action: The path to the script/command or the webhook URL.
    • action_params: A string passed as an argument to the action.

    Example Use Cases:

    • Sending an email via a webhook when a new record is inserted.
    • Running a shell script to process data using jq when a record is updated.
  3. How the PocketBase SvelteKit architecture works

    master

    This project uses a JAMstack architecture where the frontend is a fully static SvelteKit application and the backend is a single compiled PocketBase (Golang) binary.

    Workflow:

    1. The SvelteKit app is compiled into static files using adapter-static (SSR is disabled).
    2. PocketBase serves the compiled SvelteKit app.
    3. The Browser (client) makes API calls directly to the PocketBase server.

    This combination provides high performance because the frontend requires no Node.js/Bun runtime at production, and the backend is a single, fast Go binary providing a database (SQLite), CRUD API, real-time subscriptions, Auth, and file storage.

  4. Manage Schema and TypeScript types

    master

    Schema Migrations

    PocketBase 0.9+ uses JavaScript auto-migrations. Changes made to the schema are automatically saved as JS files in the pb_migrations folder. Ensure you commit these files to version control.

    Regenerating TypeScript Types

    To keep your frontend in sync with your database collections, you must regenerate the generated-types.ts file whenever the schema changes. Run the typegen script located in the frontend's package.json.

  5. Run the PocketBase backend

    master

    You can start the backend using one of these methods:

    • Directly: Run ./pocketbase serve in the pb directory.
    • Via NPM: Run npm run backend from the sk directory.

    If you want the PocketBase backend to also serve your frontend assets, you must include the --publicDir flag pointing to your build folder:

    ./pocketbase serve --publicDir ../frontend/build
    ./pocketbase serve
  6. Use Docker for PocketBase development

    master

    Running PocketBase inside Docker is highly recommended. The repository includes:

    • A Dockerfile for production images.
    • A docker-compose.yml for development.
    • A docker-compose.override.yml for development-specific configurations.

    Using Docker also automates the setup of modd for Go live-reloading.

  7. Setup using custom Go compilation and modd

    master

    Use this method if you have Go tools installed and want to run the application directly on your OS without Docker.

    1. Ensure Go is installed (go version).
    2. In the /pb directory, run go mod tidy.
    3. Build the binary: go build.
    4. (Optional) For live development with hot-reloading of Go code, install modd:
      go install github.com/cortesi/modd/cmd/modd@latest
      Then run modd to start the backend development server.
    5. In a new terminal, run the frontend: cd sk && npm run develop.
    go mod tidy
    go build
    go install github.com/cortesi/modd/cmd/modd@latest
    cd sk && npm run develop
  8. Setup using a standard PocketBase binary

    master

    This method is suitable for simple use cases using only JavaScript-based customizations.

    1. Download the latest PocketBase binary for your OS from the official releases.
    2. Extract pocketbase.exe (or the relevant binary) into the /pb folder.
    3. Backend: In the /sk directory, run npm run backend.
      • Windows Note: You must edit ./sk/package.json and change the backend script to: cd .. && cd pb && pocketbase serve --publicDir=../sk/build.
    4. Frontend: In the /sk directory, install dependencies with npx pnpm install and run npm run dev.
    npm run backend
    npx pnpm install
    npm run dev
  9. Set up the PocketBase backend

    master

    You can set up the backend in two ways:

    1. Standard Release: Download the official binary from the PocketBase releases page, unzip it, and place the pocketbase binary in the pb directory. This version supports JavaScript extensions.
    2. Custom Build: If you want to use PocketBase as a framework with Go-language customizations, use the provided main.go. You can build it using go build or use modd for live reloading.

    Note: The entrypoint.sh script defaults to a custom build if a go compiler is detected; otherwise, it downloads the standard binary.

    go build
  10. Setup the project without Docker (Manual)

    master

    If you prefer not to use Docker, you can run the backend and frontend separately.

    1. Backend Setup (in /pb folder):

    • Initialize dependencies: go mod tidy
    • Run the server: go run main.go serve --dev (runs at http://localhost:8090)
    • Create admin user: Visit http://localhost:8090/_

    2. Frontend Setup (in /sk folder):

    • Install dependencies: bun install
    • Run dev server: bun run dev (runs at http://localhost:5173)
    go run main.go serve --dev
    bun install
    bun run dev
  11. Setup the project using Docker

    master

    Using Docker is the strongly recommended method, especially if you are customizing the backend with Go code.

    1. Copy .env.example to .env and configure your environment variables.
    2. (Optional) Copy docker-compose.override.yml.example to docker-compose.override.yml and edit as needed.
    3. Run the following command:
      docker compose up -d
    4. Access the frontend dev server at http://localhost:5173.
    5. Access the backend admin setup at http://localhost:5173/_ to create your first admin user.
    6. Verify the setup by visiting http://localhost:5173/hello to see if the 'Hello World!' API response appears.
    docker compose up -d