This commit is contained in:
2025-10-05 13:52:22 +02:00
commit 63929c2dce
65 changed files with 10729 additions and 0 deletions

21
doc/.gitignore vendored Normal file
View File

@@ -0,0 +1,21 @@
# build output
dist/
# generated types
.astro/
# dependencies
node_modules/
# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env
.env.production
# macOS-specific files
.DS_Store

4
doc/.vscode/extensions.json vendored Normal file
View File

@@ -0,0 +1,4 @@
{
"recommendations": ["astro-build.astro-vscode"],
"unwantedRecommendations": []
}

11
doc/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "./node_modules/.bin/astro dev",
"name": "Development server",
"request": "launch",
"type": "node-terminal"
}
]
}

38
doc/CHANGELOG.md Normal file
View File

@@ -0,0 +1,38 @@
# doc
## 0.4.0
### Minor Changes
- middleware is externalized in src/lib/pocketbase/middleware.ts
## 0.3.1
### Patch Changes
- 0c3e99c: change default naming from Model to Record as pocketbase calls it
## 0.3.0
### Minor Changes
- 23c9e7f: remove eleventy fetch in favoi of pocketbase sdk and refine the way content is updated
## 0.2.0
### Minor Changes
- c1027d0: add cacheDir option for fetching
## 0.1.0
### Minor Changes
- 8901c7e: add enum options
- 99172d0: add ignore option
## 0.0.2
### Patch Changes
- 21cb879: add eleventy-fetch peer dependency mention

55
doc/README.md Normal file
View File

@@ -0,0 +1,55 @@
# Starlight Starter Kit: Basics
[![Built with Starlight](https://astro.badg.es/v2/built-with-starlight/tiny.svg)](https://starlight.astro.build)
```
npm create astro@latest -- --template starlight
```
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/withastro/starlight/tree/main/examples/basics)
[![Open with CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/withastro/starlight/tree/main/examples/basics)
[![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/withastro/starlight&create_from_path=examples/basics)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fwithastro%2Fstarlight%2Ftree%2Fmain%2Fexamples%2Fbasics&project-name=my-starlight-docs&repository-name=my-starlight-docs)
> 🧑‍🚀 **Seasoned astronaut?** Delete this file. Have fun!
## 🚀 Project Structure
Inside of your Astro + Starlight project, you'll see the following folders and files:
```
.
├── public/
├── src/
│ ├── assets/
│ ├── content/
│ │ ├── docs/
│ │ └── config.ts
│ └── env.d.ts
├── astro.config.mjs
├── package.json
└── tsconfig.json
```
Starlight looks for `.md` or `.mdx` files in the `src/content/docs/` directory. Each file is exposed as a route based on its file name.
Images can be added to `src/assets/` and embedded in Markdown with a relative link.
Static assets, like favicons, can be placed in the `public/` directory.
## 🧞 Commands
All commands are run from the root of the project, from a terminal:
| Command | Action |
| :------------------------ | :----------------------------------------------- |
| `npm install` | Installs dependencies |
| `npm run dev` | Starts local dev server at `localhost:4321` |
| `npm run build` | Build your production site to `./dist/` |
| `npm run preview` | Preview your build locally, before deploying |
| `npm run astro ...` | Run CLI commands like `astro add`, `astro check` |
| `npm run astro -- --help` | Get help using the Astro CLI |
## 👀 Want to learn more?
Check out [Starlights docs](https://starlight.astro.build/), read [the Astro documentation](https://docs.astro.build), or jump into the [Astro Discord server](https://astro.build/chat).

38
doc/astro.config.mjs Normal file
View File

@@ -0,0 +1,38 @@
// @ts-check
import starlight from "@astrojs/starlight";
import { defineConfig } from "astro/config";
// https://astro.build/config
export default defineConfig({
integrations: [
starlight({
title: "Astro PocketBase",
social: {
github: "https://github.com/gbouteiller/astro-pocketbase",
},
sidebar: [
{
label: "Start here",
items: [
{ label: "Getting Started", slug: "start-here/getting-started" },
{ label: "Manual Setup", slug: "start-here/manual-setup" },
{ label: "Usage", slug: "start-here/usage" },
],
},
{
label: "Guides",
items: [
{ label: "Schemas and Types", slug: "guides/schemas-and-types" },
{ label: "Middleware", slug: "guides/middleware" },
{ label: "Loader", slug: "guides/loader" },
{ label: "Zod PocketBase", slug: "guides/zod-pocketbase" },
],
},
{
label: "Reference",
autogenerate: { directory: "reference" },
},
],
}),
],
});

19
doc/package.json Normal file
View File

@@ -0,0 +1,19 @@
{
"name": "doc",
"type": "module",
"version": "0.4.0",
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"build": "astro check && astro build",
"preview": "astro preview",
"astro": "astro"
},
"dependencies": {
"@astrojs/check": "^0.9.4",
"@astrojs/starlight": "^0.30.3",
"astro": "^5.1.1",
"sharp": "^0.33.5",
"typescript": "^5.7.2"
}
}

1
doc/public/favicon.svg Normal file
View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128"><path fill-rule="evenodd" d="M81 36 64 0 47 36l-1 2-9-10a6 6 0 0 0-9 9l10 10h-2L0 64l36 17h2L28 91a6 6 0 1 0 9 9l9-10 1 2 17 36 17-36v-2l9 10a6 6 0 1 0 9-9l-9-9 2-1 36-17-36-17-2-1 9-9a6 6 0 1 0-9-9l-9 10v-2Zm-17 2-2 5c-4 8-11 15-19 19l-5 2 5 2c8 4 15 11 19 19l2 5 2-5c4-8 11-15 19-19l5-2-5-2c-8-4-15-11-19-19l-2-5Z" clip-rule="evenodd"/><path d="M118 19a6 6 0 0 0-9-9l-3 3a6 6 0 1 0 9 9l3-3Zm-96 4c-2 2-6 2-9 0l-3-3a6 6 0 1 1 9-9l3 3c3 2 3 6 0 9Zm0 82c-2-2-6-2-9 0l-3 3a6 6 0 1 0 9 9l3-3c3-2 3-6 0-9Zm96 4a6 6 0 0 1-9 9l-3-3a6 6 0 1 1 9-9l3 3Z"/><style>path{fill:#000}@media (prefers-color-scheme:dark){path{fill:#fff}}</style></svg>

After

Width:  |  Height:  |  Size: 696 B

BIN
doc/src/assets/houston.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

View File

@@ -0,0 +1,6 @@
import { defineCollection } from 'astro:content';
import { docsSchema } from '@astrojs/starlight/schema';
export const collections = {
docs: defineCollection({ schema: docsSchema() }),
};

View File

@@ -0,0 +1,16 @@
---
title: Loader
description: Astro PocketBase gives you access to a loader for your collections
---
```ts title="src/content/config.ts"
import { pocketbaseLoader } from "src/lib/pocketbase/loader";
import { PostsRecord } from "src/lib/pocketbase/schemas";
const posts = defineCollection({
loader: pocketbaseLoader({ collection: "posts" }),
schema: PostsRecord,
});
export const collections = { posts };
```

View File

@@ -0,0 +1,12 @@
---
title: Middleware
description: Astro PocketBase gives you access to the sdk via a middleware
---
```astro title="src/page/index.astro"
---
const posts = Astro.locals.pocketbase.collection("posts").getFullList()
---
{posts.map((post) => <PostItem {post} />)}
```

View File

@@ -0,0 +1,42 @@
---
title: Schemas and Types
description: Astro PocketBase generates schemas and types for your collections
---
Astro PocketBase generates schemas and types for your collections
## Generic type
The [PocketBase SDK](https://github.com/pocketbase/js-sdk) provides a generic type for its collections called `RecordModel`:
```ts
interface BaseModel {
[key: string]: any;
id: string;
created: string;
updated: string;
}
interface RecordModel extends BaseModel {
collectionId: string;
collectionName: string;
expand?: {
[key: string]: any;
};
}
```
## Generated types
Zod schemas and types are generated for you and available for each collection in `src/lib/pocketbase/schemas`:
```ts
import { PostsRecord } from "src/lib/pocketbase/schemas";
```
The default naming is based on the [PocketBase SDK](https://github.com/pocketbase/js-sdk) convention; so for a collection named `posts`
you will be given access to a `PostsRecord` schema and a `PostsRecord` type.
:::tip
You can customize the way your collection schemas and types are named by using the dedicated [integration options](/reference/options): `nameRecordSchema` and `nameRecordType`
:::

View File

@@ -0,0 +1,92 @@
---
title: Zod PocketBase
description: Astro PocketBase guves you access to everything from Zod PocketBase
---
Astro PocketBase gives you access to everything from [Zod PocketBase](https://zod-pocketbase.vercel.app)
## Schemas Helpers
Instead of this:
```ts
import {AuthorRecord, ImageRecord, PostRecord} from "src/lib/pocketbase/schemas";
const Post = PostRecord.pick({ content: true, title: true, updated: true })
.extend({
expand: z.object({
author: AuthorRecord.pick({ name: true })
.extend({
expand: z.object({
image: ImageRecord.pick({ alt: true, src: true }),
}),
})
.transform(({ expand, ...rest }) => ({ ...rest, ...expand })),
image: ImageRecord.pick({ alt: true, src: true }),
}),
})
.transform(({ expand, ...rest }) => ({ ...rest, ...expand }));
```
Write this:
```ts
import { select } from "astro-pocketbase";
import {AuthorRecord, ImageRecord, PostRecord} from "src/lib/pocketbase/schemas";
const Post = select(PostRecord, ["content", "title", "updated"], {
author: select(AuthorRecord, ["name"], {
image: select(ImageRecord, ["alt", "src"])
}),
image: select(ImageRecord, ["alt", "src"])
});
```
:::tip[What you get]
- `expand` properties from the PocketBase SDK are automatically transformed
- `pick` properties are simplified and easier to read
Discover [expand, pick and select helpers](https://zod-pocketbase.vercel.app/guides/schemas)
:::
## Fetch Helpers
Instead of this:
```ts
import { listOptionsFrom, select } from "astro-pocketbase";
import { AuthorRecord, PostRecord } from "src/lib/pocketbase/schemas";
const Post = select(PostRecord, ["content", "title"], {
author: select(AuthorRecord, ["name"])
});
const options = listOptionsFrom(Post, { sort: "-updated" });
const { items } = await Astro.locals.pocketbase.collection("posts").getList(1, 10, options);
const firstPosts = Post.array().parse(items);
```
Write this:
```ts
import { helpersFrom, select } from "astro-pocketbase";
import { AuthorRecord, PostRecord } from "src/lib/pocketbase/schemas";
const { getRecords } = helpersFrom({ pocketbase: Astro.locals.pocketbase, cache: "1d" });
const Post = select(PostRecord, ["content", "title"], {
author: select(AuthorRecord, ["name"])
});
const { items: firstPosts } = getRecords("posts", { perPage: 10, schema: Post, sort: "-updated" });
```
:::tip[What you get]
- data is automatically validated
- options are automatically formatted without the need of other helpers (here: `listOptionsFrom`)
Discover [fetch helpers](https://zod-pocketbase.vercel.app/guides/helpers)
:::

View File

@@ -0,0 +1,30 @@
---
title: Welcome to Astro PocketBase
description: Astro integration to ease the use of PocketBase in your Astro projects
template: splash
hero:
tagline: Add PocketBase to Astro with ease!
image:
file: ../../assets/houston.webp
actions:
- text: Get started
link: /start-here/getting-started/
icon: right-arrow
- text: View on GitHub
link: https://github.com/gbouteiller/astro-pocketbase
icon: external
variant: minimal
---
import { CardGrid, Card } from '@astrojs/starlight/components';
## Also check out...
<CardGrid stagger>
<Card title="Zod PocketBase" icon="puzzle">
Add [Zod to PocketBase](https://zod-pocketbase.vercel.app) with ease!
</Card>
<Card title="Astro Superforms" icon="puzzle">
Add [Superforms to Astro](https://astro-superforms.vercel.app) with ease!
</Card>
</CardGrid>

View File

@@ -0,0 +1,62 @@
---
title: Options
description: Astro PocketBase gives you options
---
The following reference covers all supported configuration options for Astro PocketBase.
```js title="astro.config.mjs"
import { defineConfig } from "astro/config";
import pocketbase from "astro-pocketbase";
export default defineConfig({
// ...
integrations: [
pocketbase({
// Your configuration options here...
})
],
});
```
## ignore
- **Type:** `string[]`
- **Default:** `[]`
The `ignore` option allows you to ignore specific collections from being processed.
## nameEnum
- **Type:** `(enumFieldName: string) => string`
- **Default:** `(enumFieldName) => snakeCase(enumFieldName).toUpperCase()`
## nameEnumField
- **Type:** `(collectionName: string, fieldName: string) => string`
- **Default:** `(collectionName, fieldName) => collectionName + pascalName(fieldName)`
## nameEnumSchema
- **Type:** `(enumFieldName: string) => string`
- **Default:** `(enumFieldName) => pascalName(enumFieldName)`
## nameEnumType
- **Type:** `(enumFieldName: string) => string`
- **Default:** `(enumFieldName) => pascalName(enumFieldName)`
## nameEnumValues
- **Type:** `(enumFieldName: string) => string`
- **Default:** `(enumFieldName) => enumFieldName + "Values"`
## nameRecordSchema
- **Type:** `(collectionName: string) => string`
- **Default:** `(collectionName) => pascalName(collectionName) + "Record"`
## nameRecordType
- **Type:** `(collectionName: string) => string`
- **Default:** `(collectionName) => pascalName(collectionName) + "Record"`

View File

@@ -0,0 +1,51 @@
---
title: Getting started
description: How to automatically setup the Astro PocketBase integration.
---
import { Tabs, TabItem } from '@astrojs/starlight/components';
import { Steps } from '@astrojs/starlight/components';
This integration is built on top of [Zod PocketBase](https://zod-pocketbase.vercel.app) to ease the use of **PocketBase** in your **Astro** projects by:
- generating **schemas** for your selected collections
- generating a **loader** that you can use for your content collections
- generating a **middleware** that gives you access to the [PocketBase SDK](https://github.com/pocketbase/js-sdk) and helpers in `Astro.locals`
## Installation
Astro includes an `astro add` command to automate the setup of official integrations. If you prefer, you can [install integrations manually](/start-here/manual-setup) instead.
<Steps>
1. To install `astro-pocketbase`, run the following from your project directory and follow the prompts:
<Tabs>
<TabItem label="npm">
```shell
npx astro add astro-pocketbase
```
</TabItem>
<TabItem label="pnpm">
```shell
pnpm astro add astro-pocketbase
```
</TabItem>
<TabItem label="yarn">
```shell
yarn astro add astro-pocketbase
```
</TabItem>
</Tabs>
2. You also need to provide 3 environment variables for **Astro PocketBase** to retrieve your collections:
```shell
# This will only be available when run on the server!
ASTRO_POCKETBASE_ADMIN_EMAIL="admin@mydomain.com"
ASTRO_POCKETBASE_ADMIN_PASSWORD="mypassword"
# This will be available everywhere!
PUBLIC_ASTRO_POCKETBASE_URL="https://myproject.pockethost.io"
```
</Steps>
If you run into any issues, [feel free to report them to us on GitHub](https://github.com/gbouteiller/astro-pocketbase/issues) and try the manual setup instead.

View File

@@ -0,0 +1,76 @@
---
title: Manual setup
description: How to manually setup the Astro PocketBase integration.
---
import { Tabs, TabItem } from '@astrojs/starlight/components';
import { Steps } from '@astrojs/starlight/components';
## Installation
<Steps>
1. First, install the `astro-pocketbase` package:
<Tabs>
<TabItem label="npm">
```bash
npm install astro-pocketbase
```
</TabItem>
<TabItem label="pnpm">
```bash
pnpm add astro-pocketbase
```
</TabItem>
<TabItem label="yarn">
```bash
yarn add astro-pocketbase
```
</TabItem>
</Tabs>
2. Most package managers will install associated peer dependencies as well. If you see a `"Cannot find package 'pocketbase'"` (or similar)
warning when you start up Astro, you'll need to install `pocketbase`:
<Tabs>
<TabItem label="npm">
```shell
npm install pocketbase
```
</TabItem>
<TabItem label="pnpm">
```shell
pnpm add pocketbase
```
</TabItem>
<TabItem label="yarn">
```shell
yarn add pocketbase
```
</TabItem>
</Tabs>
3. Then, apply the integration to your `astro.config.*` file using the `integrations` property:
```js ins="pocketbase()" ins={2} title="astro.config.mjs"
import { defineConfig } from "astro/config";
import pocketbase from "astro-pocketbase";
export default defineConfig({
// ...
integrations: [pocketbase()],
});
```
4. You also need to provide 3 environment variables for **Astro PocketBase** to retrieve your collections:
```shell
# This will only be available when run on the server!
ASTRO_POCKETBASE_ADMIN_EMAIL="admin@mydomain.com"
ASTRO_POCKETBASE_ADMIN_PASSWORD="mypassword"
# This will be available everywhere!
PUBLIC_ASTRO_POCKETBASE_URL="https://myproject.pockethost.io"
```
</Steps>

View File

@@ -0,0 +1,32 @@
---
title: Usage
description: How to use the Astro PocketBase integration.
---
import { Tabs, TabItem } from '@astrojs/starlight/components';
## Configuration
You can configure your integration in your `astro.config.mjs`. It allows you:
- to specify the naming of the generated schemas, types and enums from your collections
- to ignore some collections from being processed
```js title="astro.config.mjs"
export default defineConfig({
// ...
integrations: [pocketbase({
// default values
ignore: [],
nameEnum: (name: string) => snakeCase(name).toUpperCase(),
nameEnumField: (collectionName: string, fieldName: string) => `${collectionName}${pascalCase(fieldName)}`,
nameEnumSchema: (name: string) => pascalCase(name),
nameEnumType: (name: string) => pascalCase(name),
nameEnumValues: (name: string) => `${name}Values`,
nameRecordSchema: (name: string) => `${pascalCase(name)}Record`,
nameRecordType: (name: string) => `${pascalCase(name)}Record`,
})],
});
```
:::tip[Options]
For more details, see the reference [here](/reference/options).
:::

2
doc/src/env.d.ts vendored Normal file
View File

@@ -0,0 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />

3
doc/tsconfig.json Normal file
View File

@@ -0,0 +1,3 @@
{
"extends": "astro/tsconfigs/strict"
}