Created
September 12, 2023 02:01
-
-
Save theetrain/0aaeedbd3e3425af7658ab198a0c403c to your computer and use it in GitHub Desktop.
PocketBase JavaScript SDK back-end connection (SvelteKit example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { connect } from '$lib/server/pbclient' | |
import { redirect, type Actions } from '@sveltejs/kit' | |
export const actions = { | |
create: async ({ request, cookies }) => { | |
const data = await request.formData() | |
const post = [...data.entries()] | |
const pb = await connect() | |
let res | |
try { | |
res = await pb.collection('items').create(Object.fromEntries(post)) | |
} catch (err) { | |
console.error({ err }) | |
// can use `return fail()` instead | |
throw redirect(303, '/new?fail=true') | |
} | |
// Handle happy path | |
} | |
} satisfies Actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/lib/server/pbclient.ts | |
import { env } from '$env/dynamic/private' | |
import PocketBase from 'pocketbase' | |
const connect = async () => { | |
const pb = new PocketBase(env.PB_HOST) | |
await pb.collection('users').authWithPassword(env.PB_USER, env.PB_PASSWORD) | |
return pb | |
} | |
export { connect } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment