You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BREAKING CHANGE: This also removes support for PocketBase 0.22.
There are a lot of breaking changes in this new version of PocketBase,
e.g. new endpoint for login, new collection schema format, etc.
Since this version already brings a lot of changes, I used this chance
to refactor some of the internals and configuration options. Please
refer to the new README for more details.
Copy file name to clipboardExpand all lines: README.md
+50-29
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,18 @@
9
9
10
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.
11
11
12
+
> [!WARNING]
13
+
> This package is still under development.
14
+
> It will have a first stable release when Astro 5 is released.
15
+
> Until then, **breaking changes can occur at any time**.
16
+
17
+
## Compatibility
18
+
19
+
| Loader version | Astro version | PocketBase version |
In your content configuration file, you can use the `pocketbaseLoader` function to use your PocketBase database as a data source.
@@ -27,12 +39,30 @@ const blog = defineCollection({
27
39
exportconst collections = { blog };
28
40
```
29
41
30
-
By default, the loader will only fetch entries that have been modified since the last build.
31
42
Remember that due to the nature [Astros Content Layer lifecycle](https://astro.build/blog/content-layer-deep-dive#content-layer-lifecycle), the loader will **only fetch entries at build time**, even when using on-demand rendering.
32
43
If you want to update your deployed site with new entries, you need to rebuild it.
33
44
34
45
<sub>When running the dev server, you can trigger a reload by using `s + enter`.</sub>
35
46
47
+
## Incremental builds
48
+
49
+
Since PocketBase 0.23.0, the `updated` field is not mandatory anymore.
50
+
This means that the loader can't automatically detect when an entry has been modified.
51
+
To enable incremental builds, you need to provide the name of a field in your collection that stores the last update date of an entry.
52
+
53
+
```ts
54
+
const blog =defineCollection({
55
+
loader: pocketbaseLoader({
56
+
...options,
57
+
updatedField: "<field-in-collection>"
58
+
})
59
+
});
60
+
```
61
+
62
+
When this field is provided, the loader will only fetch entries that have been modified since the last build.
63
+
Ideally, this field should be of type `autodate` and have the value "Update" or "Create/Update" in the PocketBase dashboard.
64
+
This ensures that the field is automatically updated when an entry is modified.
65
+
36
66
## Entries
37
67
38
68
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.).
@@ -46,7 +76,7 @@ This content will then be used when calling the `render` function of [Astros con
46
76
const blog =defineCollection({
47
77
loader: pocketbaseLoader({
48
78
...options,
49
-
content: "<field-in-collection>"
79
+
contentFields: "<field-in-collection>"
50
80
})
51
81
});
52
82
```
@@ -70,14 +100,16 @@ These types can be generated in two ways:
70
100
71
101
### Remote schema
72
102
73
-
To use the lice remote schema, you need to provide the email and password of an admin of the PocketBase instance.
103
+
To use the lice remote schema, you need to provide superuser credentials for the PocketBase instance.
74
104
75
105
```ts
76
106
const blog =defineCollection({
77
107
loader: pocketbaseLoader({
78
108
...options,
79
-
adminEmail: "<admin-email>",
80
-
adminPassword: "<admin-password>"
109
+
superuserCredentials: {
110
+
email: "<superuser-email>",
111
+
password: "<superuser-password>"
112
+
}
81
113
})
82
114
});
83
115
```
@@ -86,7 +118,7 @@ Under the hood, the loader will use the [PocketBase API](https://pocketbase.io/d
86
118
87
119
### Local schema
88
120
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.
121
+
If you don't want to provide superuser credentials (e.g. in a public repository), you can also provide the schema manually via a JSON file.
90
122
91
123
```ts
92
124
const blog =defineCollection({
@@ -100,7 +132,7 @@ const blog = defineCollection({
100
132
In PocketBase you can export the schema of the whole database to a `pb_schema.json` file.
101
133
If you provide the path to this file, the loader will use this schema to generate the types locally.
102
134
103
-
When admin credentials are provided, the loader will **always use the remote schema** since it's more up-to-date.
135
+
When superuser credentials are provided, the loader will **always use the remote schema** since it's more up-to-date.
104
136
105
137
### Manual schema
106
138
@@ -109,36 +141,25 @@ This manual schema will **always override the automatic type generation**.
|`url`|`string`| x | The URL of your PocketBase instance. |
115
-
|`collectionName`|`string`| x | The name of the collection in your PocketBase instance. |
116
-
|`content`|`string \| Array<string>`|| The field in the collection to use as content. This can also be an array of fields. |
117
-
|`adminEmail`|`string`|| The email of the admin of the PocketBase instance. This is used for automatic type generation and access to private collections. |
118
-
|`adminPassword`|`string`|| The password of the admin of the PocketBase instance. This is used for automatic type generation and access to private collections. |
119
-
|`localSchema`|`string`|| The path to a local schema file. This is used for automatic type generation. |
120
-
|`jsonSchemas`|`Record<string, z.ZodSchema>`|| A record of Zod schemas to use for type generation of `json` fields. |
121
-
|`forceUpdate`|`boolean`|| If set to `true`, the loader will fetch every entry instead of only the ones modified since the last build. |
|`url`|`string`| x | The URL of your PocketBase instance. |
147
+
|`collectionName`|`string`| x | The name of the collection in your PocketBase instance. |
148
+
|`contentFields`|`string \| Array<string>`|| The field in the collection to use as content. This can also be an array of fields. |
149
+
|`updatedField`|`string`|| The field in the collection that stores the last update date of an entry. This is used for incremental builds. |
150
+
|`superuserCredentials`|`{ email: string, password: string }`|| The email and password of the superuser of the PocketBase instance. This is used for automatic type generation. |
151
+
|`localSchema`|`string`|| The path to a local schema file. This is used for automatic type generation. |
152
+
|`jsonSchemas`|`Record<string, z.ZodSchema>`|| A record of Zod schemas to use for type generation of `json` fields. |
122
153
123
154
## Special cases
124
155
125
-
### Private collections
156
+
### Private collections and hidden fields
126
157
127
-
If you want to access a private collection, you also need to provide the admin credentials.
158
+
If you want to access a private collection or want to access hidden fields, you also need to provide superuser credentials.
128
159
Otherwise, you need to make the collection public in the PocketBase dashboard.
129
160
130
161
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.
131
162
132
-
### View collections
133
-
134
-
Out of the box, the loader also supports collections with the type `view`, though with some limitations.
135
-
To enable incremental builds, the loader needs to know when an entry has been modified.
136
-
Normal `base` collections have a `updated` field that is automatically updated when an entry is modified.
137
-
Thus, `view` collections that don't include this field can't be incrementally built but will be fetched every time.
138
-
139
-
You can also alias another field as `updated` (as long as it's a date field) in your view.
140
-
While this is possible, it's not recommended since it can lead to outdated data not being fetched.
141
-
142
163
### JSON fields
143
164
144
165
PocketBase can store arbitrary JSON data in a `json` field.
// If the schema is not available, try to read it from a local schema file
49
38
if(!collection&&options.localSchema){
50
39
collection=awaitreadLocalSchema(
@@ -56,22 +45,29 @@ export async function generateSchema(
56
45
// If the schema is still not available, return the basic schema
57
46
if(!collection){
58
47
console.error(
59
-
`No schema available for ${options.collectionName}. Only basic types are available. Please check your configuration and provide a valid schema file or admin credentials.`
48
+
`No schema available for "${options.collectionName}". Only basic types are available. Please check your configuration and provide a valid schema file or superuser credentials.`
60
49
);
61
-
// Return the view schema since every collection has at least the view schema
62
-
returnz.object(VIEW_SCHEMA);
50
+
// Return the basic schema since every collection has at least these fields
`The field "${options.updatedField}" is not present in the schema of the collection "${options.collectionName}".\nThis will lead to errors when trying to fetch only updated entries.`
84
+
);
85
+
}else{
86
+
constupdatedField=collection.fields.find(
87
+
(field)=>field.name===options.updatedField
88
+
);
89
+
if(
90
+
!updatedField||
91
+
updatedField.type!=="autodate"||
92
+
!updatedField.onUpdate
93
+
){
94
+
console.warn(
95
+
`The field "${options.updatedField}" is not of type "autodate" with the value "Update" or "Create/Update".\nMake sure that the field is automatically updated when the entry is updated!`
96
+
);
97
+
}
98
+
}
99
+
}
86
100
87
101
// Combine the basic schema with the parsed fields
0 commit comments