@velastack/pocketbase

PocketBase bindings for SvelteKit: a hooks.server.ts middleware, a typed client and type-safe collection schemas. This is the package the generated code imports.

npm install @velastack/pocketbase

Installed for you by vela create and vela enable backend.

Entrypoints

  • @velastack/pocketbase - The middleware and the client
  • @velastack/pocketbase/form - Superforms helpers
  • @velastack/pocketbase/api-key - API key generation and verification
  • @velastack/pocketbase/testing - The TestContext type for server tests

handlePocketbase

Configures the PocketBase middleware in hooks.server.ts.

import { handlePocketbase } from '@velastack/pocketbase';
import {
    POCKETBASE_URL,
    POCKETBASE_SUPERUSER_EMAIL,
    POCKETBASE_SUPERUSER_PASSWORD
} from '$env/static/private';

export const handle = handlePocketbase({
	pocketbaseUrl: POCKETBASE_URL,
	superuserEmail: POCKETBASE_SUPERUSER_EMAIL,
	superuserPassword: POCKETBASE_SUPERUSER_PASSWORD
})

The auth, api and apiKeys blocks are added to this call by the enable commands. It puts two clients on locals: admin, which queries the database directly, and pb, which is scoped to the authenticated user.

setDefaultData

Sets the default data for a form from a PocketBase record. Exported from the /form entrypoint.

import { setDefaultData } from '@velastack/pocketbase/form';

export const actions = {
	default: async ({ locals, request }) => {
        const record = await locals.admin.collection('posts').getOne(1);
        setDefaultData(form, record);
        return { form };
    }
}

setPocketbaseErrors

Sets PocketBase errors as Superforms errors when a form submission fails.

import { setPocketbaseErrors } from '@velastack/pocketbase/form';

export const actions = {
	default: async ({ locals, request }) => {
        try {
            await locals.admin.collection('posts').create(form.data);
        } catch (error) {
            setPocketbaseErrors(form, error);
            return fail(400, { form });
        }
    }
}

API keys

The primitives behind vela enable api-keys. Keys are stored hashed, so the secret is only ever shown once, at generation.

import {
    generateApiKeySecret,
    hashApiKey,
    verifyApiKey
} from '@velastack/pocketbase/api-key';

Generated types

vela sync augments this package with Models, Collections and Schemas for your project’s collections, so locals.pb.collection('posts') is typed.