RJBULK-APP/lib/pb.ts

31 lines
808 B
TypeScript
Raw Normal View History

import PocketBase from "pocketbase";
import { PB_URL } from "./pocketbase";
/**
* PB record helpers — auth-aware clients for server actions and route handlers.
*/
export function pbAdmin(): PocketBase {
return new PocketBase(PB_URL);
}
export async function pbAsUser(token: string): Promise<PocketBase> {
const pb = new PocketBase(PB_URL);
pb.authStore.save(token, null);
return pb;
}
/**
* Generate a PB-compatible id (15 chars, lowercase a-z + 0-9).
* This PB version's autogenerate pattern doesn't fire on create, so we
* generate IDs explicitly in the app layer.
*/
export function pbId(): string {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
let out = "";
for (let i = 0; i < 15; i++) {
out += chars[Math.floor(Math.random() * chars.length)];
}
return out;
}