-
I am using sveltekit to try out pocketbase. This should be a newbie question, but how do you add a collection type to my code?
For example I have a todo collection with just a text field. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I'm pretty sure you should define this type yourself, in for example export type Todo = {
id: string;
text: string;
created: string;
updated: string;
}; Also, I don't think you should query pocketbase from the page component, instead you should probably use a page endpoint. You would end up with the page endpoint import type { RequestHandler } from '@sveltejs/kit';
import type { Todo } from '$lib/types';
import PocketBase from 'pocketbase';
const client = new PocketBase('http://127.0.0.1:8090');
export const GET: RequestHandler = async () => {
const result = await client.records.getList('todo', 1, 20);
const todos: Todo[] = result.items.map((record) => {
return {
id: record.id,
text: record.text,
created: record.created,
updated: record.updated
}
});
return {
body: {
todos: todos
}
};
}; and finally the page component itself <script lang='ts'>
import type { Todo } from '$lib/types';
export let todos: Todo[];
</script>
{#each todos as todo}
{todo.text}
{/each} |
Beta Was this translation helpful? Give feedback.
-
Thanks @ollema I was doing everything from the frontend, one side question, when accessing PocketBase from an endpoint, what is the proper way to pass authentication data? |
Beta Was this translation helpful? Give feedback.
I'm pretty sure you should define this type yourself, in for example
src/lib/types.ts
:Also, I don't think you should query pocketbase from the page component, instead you should probably use a page endpoint.
You would end up with the page endpoint
index.ts
: