Skip to content

Commit 4f6e759

Browse files
committed
docs(README): add documentation for filter option
1 parent 4ccad1b commit 4f6e759

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

README.md

+26-6
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,24 @@ The loader will also automatically convert the value into a slug to be easily us
108108
It's recommended to use e.g. the title of the entry to be easily searchable and readable.
109109
**Do not use e.g. rich text fields as ids.**
110110

111-
### Improved types
111+
### Filtering entries
112112

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.
116115

117116
```ts
118117
const blog = defineCollection({
119118
loader: pocketbaseLoader({
120119
...options,
121-
improveTypes: true
120+
filter: "<filter>"
122121
})
123122
});
124123
```
125124

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).
127129

128130
## Type generation
129131

@@ -172,6 +174,23 @@ When superuser credentials are provided, the loader will **always use the remote
172174
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).
173175
This manual schema will **always override the automatic type generation**.
174176

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+
175194
## All options
176195

177196
| Option | Type | Required | Description |
@@ -181,6 +200,7 @@ This manual schema will **always override the automatic type generation**.
181200
| `idField` | `string` | | The field in the collection to use as unique id. Defaults to `id`. |
182201
| `contentFields` | `string \| Array<string>` | | The field in the collection to use as content. This can also be an array of fields. |
183202
| `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. |
184204
| `superuserCredentials` | `{ email: string, password: string }` | | The email and password of the superuser of the PocketBase instance. This is used for automatic type generation. |
185205
| `localSchema` | `string` | | The path to a local schema file. This is used for automatic type generation. |
186206
| `jsonSchemas` | `Record<string, z.ZodSchema>` | | A record of Zod schemas to use for type generation of `json` fields. |

src/types/pocketbase-loader-options.type.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ export interface PocketBaseLoaderOptions {
3737
* This field is used to only fetch entries that have been modified since the last build.
3838
*/
3939
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;
4057
/**
4158
* Credentials of a superuser to get full access to the PocketBase instance.
4259
* 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.
@@ -73,21 +90,4 @@ export interface PocketBaseLoaderOptions {
7390
* The PocketBase API does always return at least `0` or `false` as the default values, even though the fields are not marked as required in the schema.
7491
*/
7592
improveTypes?: boolean;
76-
/**
77-
* Custom filter that is applied when loading data from PocketBase.
78-
* Valid syntax can be found in the [PocketBase documentation](https://pocketbase.io/docs/api-records/#listsearch-records)
79-
*
80-
* The loader will also add it's own filters for incremental builds.
81-
* These will be added to your custom filter query.
82-
*
83-
* Example:
84-
* ```ts
85-
* // config:
86-
* filter: 'release >= @now && deleted = false'
87-
*
88-
* // request
89-
* `?filter=(${loaderFilter})&&(release >= @now && deleted = false)`
90-
* ```
91-
*/
92-
filter?: string;
9393
}

0 commit comments

Comments
 (0)