Add the PocketBase backend to a SvelteKit project that has none — one scaffolded from the static template, or one the backend was removed from. Installs @velastack/pocketbase, pocketbase-sveltekit and @sveltejs/adapter-node, composes handlePocketbase into src/hooks.server.ts alongside the handles already there, scaffolds the data directory the database runs from with fixtures, hooks and seeds inside, and writes the test/setup.ts that gives server tests their PocketBase clients. Run through the CLI it also asks for an admin email and password, creates that superuser and records it in .env, so the database is usable the moment vela dev starts it. Everything else that needs a database — auth, the API, scaffolds, seeds and fixtures — builds on this.
Sample data for development
# Hooks
Add \*.pb.js hooks here to extend PocketBase: https://pocketbase.io/docs/js-overview/
Essential system-level data (e.g., roles, locales)
import { env } from "$env/dynamic/private";
import { handlePocketbase } from "@velastack/pocketbase";
export const handle = handlePocketbase({
pocketbaseUrl: env.POCKETBASE_URL,
superuserEmail: env.POCKETBASE_SUPERUSER_EMAIL,
superuserPassword: env.POCKETBASE_SUPERUSER_PASSWORD,
});
import adapter from "@sveltejs/adapter-node";
/** @type {import('@sveltejs/kit').Config} */
const config = {
compilerOptions: {
runes: ({ filename }) =>
filename.split(/[/\\]/).includes("node_modules") ? undefined : true,
},
kit: {
adapter: adapter(),
},
};
export default config;
import { beforeEach, afterEach, beforeAll } from "vitest";
import supertest, { type Agent } from "supertest";
import PocketBase from "pocketbase-sveltekit";
import type { TestContext } from "@velastack/pocketbase/testing";
const admin = new PocketBase(
process.env.POCKETBASE_URL!,
) as App.Locals["admin"];
const pb = new PocketBase(process.env.POCKETBASE_URL!) as App.Locals["pb"];
const testUserPassword = "password";
async function authenticateUser(agent: Agent, user: { email: string }) {
await agent.post("/login").type("form").send({
type: "password",
email: user.email,
password: testUserPassword,
});
}
beforeAll(async () => {
await admin
// @ts-ignore
.collection("_superusers")
.authWithPassword(
process.env.POCKETBASE_SUPERUSER_EMAIL!,
process.env.POCKETBASE_SUPERUSER_PASSWORD!,
);
});
beforeEach(async (context: TestContext) => {
context.request = supertest(process.env.VITE_TEST_URL!);
context.agent = supertest.agent(
process.env.VITE_TEST_URL!,
) as TestContext["agent"];
context.admin = admin;
context.pb = pb;
context.user = await context.admin.collection("users").create({
email: `test-${Math.random().toString(36).slice(2)}@example.com`,
password: testUserPassword,
passwordConfirm: testUserPassword,
});
context.agent.authenticateUser = () =>
authenticateUser(context.agent, context.user);
await context.pb
.collection("users")
.authWithPassword(context.user.email, testUserPassword);
});
afterEach(async (context: TestContext) => {
await context.pb.authStore.clear();
await context.admin.collection("users").delete(context.user.id);
});