astro-loader-pocketbase 0.3.0-rc.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -14
- package/package.json +3 -3
- package/src/cleanup-entries.ts +2 -2
- package/src/load-entries.ts +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# astro-loader-pocketbase
|
|
2
2
|
|
|
3
3
|
<!--  -->
|
|
4
|
+
|
|
4
5
|
[](https://www.npmjs.com/package/astro-loader-pocketbase)
|
|
5
6
|
[](https://www.npmjs.com/package/astro-loader-pocketbase)
|
|
6
7
|
[](https://github.com/pawcoding/astro-loader-pocketbase/blob/master/LICENSE)
|
|
@@ -8,7 +9,7 @@
|
|
|
8
9
|
|
|
9
10
|
This package is a simple loader to load data from a PocketBase database into Astro using the [Astro Loader API](https://5-0-0-beta.docs.astro.build/en/reference/loader-reference/) introduced in Astro 5.
|
|
10
11
|
|
|
11
|
-
##
|
|
12
|
+
## Basic usage
|
|
12
13
|
|
|
13
14
|
In your content configuration file, you can use the `pocketbaseLoader` function to use your PocketBase database as a data source.
|
|
14
15
|
|
|
@@ -19,10 +20,11 @@ import { defineCollection } from "astro:content";
|
|
|
19
20
|
const blog = defineCollection({
|
|
20
21
|
loader: pocketbaseLoader({
|
|
21
22
|
url: "https://<your-pocketbase-url>",
|
|
22
|
-
collectionName: "<collection-in-pocketbase>"
|
|
23
|
-
content: "<field-in-collection>"
|
|
23
|
+
collectionName: "<collection-in-pocketbase>"
|
|
24
24
|
})
|
|
25
25
|
});
|
|
26
|
+
|
|
27
|
+
export const collections = { blog };
|
|
26
28
|
```
|
|
27
29
|
|
|
28
30
|
By default, the loader will only fetch entries that have been modified since the last build.
|
|
@@ -31,43 +33,107 @@ If you want to update your deployed site with new entries, you need to rebuild i
|
|
|
31
33
|
|
|
32
34
|
<sub>When running the dev server, you can trigger a reload by using `s + enter`.</sub>
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
## Entries
|
|
37
|
+
|
|
38
|
+
After generating the schema (see below), the loader will automatically parse the content of the entries (e.g. transform ISO dates to `Date` objects, coerce numbers, etc.).
|
|
39
|
+
|
|
40
|
+
### HTML content
|
|
41
|
+
|
|
42
|
+
You can also specify a field or an array of fields to use as content.
|
|
43
|
+
This content will then be used when calling the `render` function of [Astros content collections](https://5-0-0-beta.docs.astro.build/en/guides/content-collections/#rendering-body-content).
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
const blog = defineCollection({
|
|
47
|
+
loader: pocketbaseLoader({
|
|
48
|
+
...options,
|
|
49
|
+
content: "<field-in-collection>"
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Since the goal of the `render` function is to render the content as HTML, it's recommended to use a field of type `editor` (rich text) in PocketBase as content.
|
|
55
|
+
|
|
56
|
+
If you specify an array of fields, the loader will wrap the content of these fields in a `<section>` and concatenate them.
|
|
57
|
+
|
|
58
|
+
### Images and files
|
|
59
|
+
|
|
60
|
+
PocketBase can store images and files for each entry in a collection.
|
|
61
|
+
While the API only returns the filenames of these images and files, the loader will out of the box transform these filenames to the actual URLs of the files.
|
|
62
|
+
This doesn't mean that the files are downloaded during the build process.
|
|
63
|
+
But you can directly use these URLs in your Astro components to display images or link to the files.
|
|
64
|
+
|
|
65
|
+
## Type generation
|
|
35
66
|
|
|
36
67
|
The loader can automatically generate types for your collection.
|
|
37
68
|
This is useful for type checking and autocompletion in your editor.
|
|
38
69
|
These types can be generated in two ways:
|
|
39
70
|
|
|
40
|
-
|
|
71
|
+
### Remote schema
|
|
41
72
|
|
|
42
73
|
To use the lice remote schema, you need to provide the email and password of an admin of the PocketBase instance.
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
const blog = defineCollection({
|
|
77
|
+
loader: pocketbaseLoader({
|
|
78
|
+
...options,
|
|
79
|
+
adminEmail: "<admin-email>",
|
|
80
|
+
adminPassword: "<admin-password>"
|
|
81
|
+
})
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
43
85
|
Under the hood, the loader will use the [PocketBase API](https://pocketbase.io/docs/api-collections/#view-collection) to fetch the schema of your collection and generate types with Zod based on that schema.
|
|
44
86
|
|
|
45
|
-
|
|
87
|
+
### Local schema
|
|
46
88
|
|
|
47
89
|
If you don't want to provide the admin credentials (e.g. in a public repository), you can also provide the schema manually via a JSON file.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
const blog = defineCollection({
|
|
93
|
+
loader: pocketbaseLoader({
|
|
94
|
+
...options,
|
|
95
|
+
localSchema: "<path-to-schema-file>"
|
|
96
|
+
})
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
48
100
|
In PocketBase you can export the schema of the whole database to a `pb_schema.json` file.
|
|
49
101
|
If you provide the path to this file, the loader will use this schema to generate the types locally.
|
|
50
102
|
|
|
51
103
|
When admin credentials are provided, the loader will **always use the remote schema** since it's more up-to-date.
|
|
52
104
|
|
|
53
|
-
|
|
105
|
+
### Manual schema
|
|
54
106
|
|
|
55
107
|
If you don't want to use the automatic type generation, you can also [provide your own schema manually](https://5-0-0-beta.docs.astro.build/en/guides/content-collections/#defining-the-collection-schema).
|
|
56
108
|
This manual schema will **always override the automatic type generation**.
|
|
57
109
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
If you want to access a private collection, you also need to provide the admin credentials.
|
|
61
|
-
Otherwise, you need to make the collection public in the PocketBase dashboard.
|
|
62
|
-
|
|
63
|
-
### Options
|
|
110
|
+
## All options
|
|
64
111
|
|
|
65
112
|
| Option | Type | Required | Description |
|
|
66
113
|
| ---------------- | ------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------- |
|
|
67
114
|
| `url` | `string` | x | The URL of your PocketBase instance. |
|
|
68
115
|
| `collectionName` | `string` | x | The name of the collection in your PocketBase instance. |
|
|
69
|
-
| `content` | `string \| Array<string>` |
|
|
116
|
+
| `content` | `string \| Array<string>` | | The field in the collection to use as content. This can also be an array of fields. |
|
|
70
117
|
| `adminEmail` | `string` | | The email of the admin of the PocketBase instance. This is used for automatic type generation and access to private collections. |
|
|
71
118
|
| `adminPassword` | `string` | | The password of the admin of the PocketBase instance. This is used for automatic type generation and access to private collections. |
|
|
72
119
|
| `localSchema` | `string` | | The path to a local schema file. This is used for automatic type generation. |
|
|
73
120
|
| `forceUpdate` | `boolean` | | If set to `true`, the loader will fetch every entry instead of only the ones modified since the last build. |
|
|
121
|
+
|
|
122
|
+
## Special cases
|
|
123
|
+
|
|
124
|
+
### Private collections
|
|
125
|
+
|
|
126
|
+
If you want to access a private collection, you also need to provide the admin credentials.
|
|
127
|
+
Otherwise, you need to make the collection public in the PocketBase dashboard.
|
|
128
|
+
|
|
129
|
+
Generally, it's not recommended to use private collections, especially when users should be able to see images or other files stored in the collection.
|
|
130
|
+
|
|
131
|
+
### View collections
|
|
132
|
+
|
|
133
|
+
Out of the box, the loader also supports collections with the type `view`, though with some limitations.
|
|
134
|
+
To enable incremental builds, the loader needs to know when an entry has been modified.
|
|
135
|
+
Normal `base` collections have a `updated` field that is automatically updated when an entry is modified.
|
|
136
|
+
Thus, `view` collections that don't include this field can't be incrementally built but will be fetched every time.
|
|
137
|
+
|
|
138
|
+
You can also alias another field as `updated` (as long as it's a date field) in your view.
|
|
139
|
+
While this is possible, it's not recommended since it can lead to outdated data not being fetched.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-loader-pocketbase",
|
|
3
|
-
"version": "0.3.0
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A content loader for Astro that uses the PocketBase API",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Luis Wolf <development@pawcode.de> (https://pawcode.de)",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@eslint/js": "^9.11.1",
|
|
25
25
|
"@stylistic/eslint-plugin": "^2.8.0",
|
|
26
|
-
"@types/node": "^22.
|
|
27
|
-
"astro": "^5.0.0-beta.
|
|
26
|
+
"@types/node": "^22.7.4",
|
|
27
|
+
"astro": "^5.0.0-beta.2",
|
|
28
28
|
"eslint": "^9.11.1",
|
|
29
29
|
"globals": "^15.9.0",
|
|
30
30
|
"husky": "^9.1.6",
|
package/src/cleanup-entries.ts
CHANGED
|
@@ -26,7 +26,7 @@ export async function cleanupEntries(
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// Prepare pagination variables
|
|
29
|
-
let page =
|
|
29
|
+
let page = 0;
|
|
30
30
|
let totalPages = 0;
|
|
31
31
|
const entries = new Set<string>();
|
|
32
32
|
|
|
@@ -34,7 +34,7 @@ export async function cleanupEntries(
|
|
|
34
34
|
do {
|
|
35
35
|
// Fetch ids from the collection
|
|
36
36
|
const collectionRequest = await fetch(
|
|
37
|
-
`${collectionUrl}?page=${page}&perPage=1000&fields=id`,
|
|
37
|
+
`${collectionUrl}?page=${++page}&perPage=1000&fields=id`,
|
|
38
38
|
{
|
|
39
39
|
headers: collectionHeaders
|
|
40
40
|
}
|
package/src/load-entries.ts
CHANGED
|
@@ -40,7 +40,7 @@ export async function loadEntries(
|
|
|
40
40
|
);
|
|
41
41
|
|
|
42
42
|
// Prepare pagination variables
|
|
43
|
-
let page =
|
|
43
|
+
let page = 0;
|
|
44
44
|
let totalPages = 0;
|
|
45
45
|
let entries = 0;
|
|
46
46
|
let hasUpdatedColumn = !!lastModified;
|
|
@@ -50,7 +50,7 @@ export async function loadEntries(
|
|
|
50
50
|
// Fetch entries from the collection
|
|
51
51
|
// If `lastModified` is set, only fetch entries that have been modified since the last fetch
|
|
52
52
|
const collectionRequest = await fetch(
|
|
53
|
-
`${collectionUrl}?page=${page}&perPage=100${
|
|
53
|
+
`${collectionUrl}?page=${++page}&perPage=100${
|
|
54
54
|
lastModified
|
|
55
55
|
? `&sort=-updated,id&filter=(updated>"${lastModified}")`
|
|
56
56
|
: ""
|