import PocketBase from "pocketbase"; import { createPlayersService } from "./services/players"; import { createTournamentsService } from "./services/tournaments"; import { createTeamsService } from "./services/teams"; class PocketBaseAdminClient { private pb: PocketBase; public authPromise: Promise; constructor() { this.pb = new PocketBase(import.meta.env.VITE_POCKETBASE_URL); this.pb.beforeSend = (url, options) => { options.cache = "no-store"; options.headers = { ...options.headers, "Cache-Control": "no-cache, no-store, must-revalidate", Pragma: "no-cache", Expires: "0", }; return { url, options }; }; this.pb.autoCancellation(false); this.authPromise = this.authenticate(); this.authPromise.then(() => { Object.assign(this, createPlayersService(this.pb)); Object.assign(this, createTeamsService(this.pb)); Object.assign(this, createTournamentsService(this.pb)); }); } private async authenticate() { await this.pb .collection("_superusers") .authWithPassword( import.meta.env.VITE_POCKETBASE_ADMIN_EMAIL!, import.meta.env.VITE_POCKETBASE_ADMIN_PASSWORD! ); } } interface AdminClient extends PocketBaseAdminClient, ReturnType, ReturnType, ReturnType { authPromise: Promise; } export const pbAdmin = new PocketBaseAdminClient() as AdminClient;