61 lines
1.4 KiB
JavaScript
61 lines
1.4 KiB
JavaScript
import PocketBase from 'pocketbase'
|
|
import { POCKETBASE_URL, PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD } from './pocketbase-config.js'
|
|
|
|
const COLLECTION = 'travellers'
|
|
|
|
let pb = null
|
|
let authPromise = null
|
|
|
|
function getClient() {
|
|
if (!pb) {
|
|
pb = new PocketBase(POCKETBASE_URL)
|
|
// Main process may receive overlapping IPC calls (e.g. React Strict Mode).
|
|
// Default auto-cancel would abort duplicate in-flight PocketBase requests.
|
|
pb.autoCancellation(false)
|
|
}
|
|
return pb
|
|
}
|
|
|
|
function parseEntry(r) {
|
|
return {
|
|
id: r.emr_id,
|
|
name: r.name ?? '',
|
|
location: r.location ?? '',
|
|
countryISO: r.country_iso ?? '',
|
|
countriesISO: r.countries_iso ?? [],
|
|
status: r.status ?? '',
|
|
departure: r.departure ?? '',
|
|
arrival: r.arrival ?? '',
|
|
created: r.created,
|
|
updated: r.updated,
|
|
}
|
|
}
|
|
|
|
export async function ensureAuth() {
|
|
const client = getClient()
|
|
if (client.authStore.isValid) return
|
|
|
|
if (authPromise) {
|
|
await authPromise
|
|
return
|
|
}
|
|
|
|
// PocketBase v0.23+ removed /api/admins/*; superusers authenticate as a collection.
|
|
authPromise = client
|
|
.collection('_superusers')
|
|
.authWithPassword(PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD)
|
|
try {
|
|
await authPromise
|
|
} catch (err) {
|
|
authPromise = null
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export async function fetchEntries() {
|
|
await ensureAuth()
|
|
const list = await getClient().collection(COLLECTION).getFullList({ sort: '-created' })
|
|
return list.map(parseEntry)
|
|
}
|
|
|