astro-loader-pocketbase 2.4.2-next.1 → 2.5.0-next.2

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 CHANGED
@@ -108,22 +108,24 @@ The loader will also automatically convert the value into a slug to be easily us
108
108
  It's recommended to use e.g. the title of the entry to be easily searchable and readable.
109
109
  **Do not use e.g. rich text fields as ids.**
110
110
 
111
- ### Improved types
111
+ ### Filtering entries
112
112
 
113
- By default PocketBase reports `number` and `boolean` fields as not required, even though the API will always return `0` and `false` respectively if no value is set.
114
- This means that the loader will add `undefined` to the type of these fields.
115
- If you want to enforce that these fields are always present, you can set the `improveTypes` option to `true`.
113
+ By default the loader will fetch all entries in the specified collection.
114
+ If you want to restrict the entries to a specific subset, you can use the `filter` option.
116
115
 
117
116
  ```ts
118
117
  const blog = defineCollection({
119
118
  loader: pocketbaseLoader({
120
119
  ...options,
121
- improveTypes: true
120
+ filter: "<filter>"
122
121
  })
123
122
  });
124
123
  ```
125
124
 
126
- This will remove `undefined` from the type of these fields and mark them as required.
125
+ For example, if you want to only fetch entries that are released but not deleted, you can use `"release >= @now && deleted = false"`.
126
+ This filter will be added to the PocketBase API request and will only fetch entries that match the filter.
127
+ This is in addition to the built-in filtering of the loader, which handles the incremental builds using the `updated` field.
128
+ For more information on how to use filters, check out the [PocketBase documentation](https://pocketbase.io/docs/api-records/#listsearch-records).
127
129
 
128
130
  ## Type generation
129
131
 
@@ -172,6 +174,23 @@ When superuser credentials are provided, the loader will **always use the remote
172
174
  If you don't want to use the automatic type generation, you can also [provide your own schema manually](https://docs.astro.build/en/guides/content-collections/#defining-the-collection-schema).
173
175
  This manual schema will **always override the automatic type generation**.
174
176
 
177
+ ### Improved types
178
+
179
+ By default PocketBase reports `number` and `boolean` fields as not required, even though the API will always return `0` and `false` respectively if no value is set.
180
+ This means that the loader will add `undefined` to the type of these fields.
181
+ If you want to enforce that these fields are always present, you can set the `improveTypes` option to `true`.
182
+
183
+ ```ts
184
+ const blog = defineCollection({
185
+ loader: pocketbaseLoader({
186
+ ...options,
187
+ improveTypes: true
188
+ })
189
+ });
190
+ ```
191
+
192
+ This will remove `undefined` from the type of these fields and mark them as required.
193
+
175
194
  ## All options
176
195
 
177
196
  | Option | Type | Required | Description |
@@ -181,6 +200,7 @@ This manual schema will **always override the automatic type generation**.
181
200
  | `idField` | `string` | | The field in the collection to use as unique id. Defaults to `id`. |
182
201
  | `contentFields` | `string \| Array<string>` | | The field in the collection to use as content. This can also be an array of fields. |
183
202
  | `updatedField` | `string` | | The field in the collection that stores the last update date of an entry. This is used for incremental builds. |
203
+ | `filter` | `string` | | Custom filter to use when fetching entries. Used to filter the entries by specific conditions. |
184
204
  | `superuserCredentials` | `{ email: string, password: string }` | | The email and password of the superuser of the PocketBase instance. This is used for automatic type generation. |
185
205
  | `localSchema` | `string` | | The path to a local schema file. This is used for automatic type generation. |
186
206
  | `jsonSchemas` | `Record<string, z.ZodSchema>` | | A record of Zod schemas to use for type generation of `json` fields. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-loader-pocketbase",
3
- "version": "2.4.2-next.1",
3
+ "version": "2.5.0-next.2",
4
4
  "description": "A content loader for Astro that uses the PocketBase API",
5
5
  "keywords": [
6
6
  "astro",
@@ -12,6 +12,14 @@ export async function handleRealtimeUpdates(
12
12
  context: LoaderContext,
13
13
  options: PocketBaseLoaderOptions
14
14
  ): Promise<boolean> {
15
+ // Check if a custom filter is set
16
+ if (options.filter) {
17
+ // Updating an entry directly via realtime updates is not supported when using a custom filter.
18
+ // This is because the filter can only be applied via the get request and is not considered in the realtime updates.
19
+ // Updating the entry directly would bypass the filter and could lead to inconsistent data.
20
+ return false;
21
+ }
22
+
15
23
  // Check if data was provided via the refresh context
16
24
  if (!context.refreshContextData?.data) {
17
25
  return false;
@@ -44,14 +44,31 @@ export async function loadEntries(
44
44
 
45
45
  // Fetch all (modified) entries
46
46
  do {
47
+ // Build search parameters
48
+ const searchParams = new URLSearchParams({
49
+ page: `${++page}`,
50
+ perPage: "100"
51
+ });
52
+
53
+ const filters = [];
54
+ if (lastModified && options.updatedField) {
55
+ // If `lastModified` is set, only fetch entries that have been modified since the last fetch
56
+ filters.push(`(${options.updatedField}>"${lastModified}")`);
57
+ // Sort by the updated field and id
58
+ searchParams.set("sort", `-${options.updatedField},id`);
59
+ }
60
+ if (options.filter) {
61
+ filters.push(`(${options.filter})`);
62
+ }
63
+
64
+ // Add filters to search parameters
65
+ if (filters.length > 0) {
66
+ searchParams.set("filter", filters.join("&&"));
67
+ }
68
+
47
69
  // Fetch entries from the collection
48
- // If `lastModified` is set, only fetch entries that have been modified since the last fetch
49
70
  const collectionRequest = await fetch(
50
- `${collectionUrl}?page=${++page}&perPage=100${
51
- lastModified && options.updatedField
52
- ? `&sort=-${options.updatedField},id&filter=(${options.updatedField}>"${lastModified}")`
53
- : ""
54
- }`,
71
+ `${collectionUrl}?${searchParams.toString()}`,
55
72
  {
56
73
  headers: collectionHeaders
57
74
  }
@@ -37,6 +37,23 @@ export interface PocketBaseLoaderOptions {
37
37
  * This field is used to only fetch entries that have been modified since the last build.
38
38
  */
39
39
  updatedField?: string;
40
+ /**
41
+ * Custom filter that is applied when loading data from PocketBase.
42
+ * Valid syntax can be found in the [PocketBase documentation](https://pocketbase.io/docs/api-records/#listsearch-records)
43
+ *
44
+ * The loader will also add it's own filters for incremental builds.
45
+ * These will be added to your custom filter query.
46
+ *
47
+ * Example:
48
+ * ```ts
49
+ * // config:
50
+ * filter: 'release >= @now && deleted = false'
51
+ *
52
+ * // request
53
+ * `?filter=(${loaderFilter})&&(release >= @now && deleted = false)`
54
+ * ```
55
+ */
56
+ filter?: string;
40
57
  /**
41
58
  * Credentials of a superuser to get full access to the PocketBase instance.
42
59
  * This is required to get automatic type generation without a local schema, to access all resources even if they are not public and to fetch content of hidden fields.