astro-loader-pocketbase 0.3.0 → 0.4.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 CHANGED
@@ -14,7 +14,7 @@ This package is a simple loader to load data from a PocketBase database into Ast
14
14
  In your content configuration file, you can use the `pocketbaseLoader` function to use your PocketBase database as a data source.
15
15
 
16
16
  ```ts
17
- import { pocketbaseLoader } from "astro-pocketbase-loader";
17
+ import { pocketbaseLoader } from "astro-loader-pocketbase";
18
18
  import { defineCollection } from "astro:content";
19
19
 
20
20
  const blog = defineCollection({
@@ -58,7 +58,7 @@ If you specify an array of fields, the loader will wrap the content of these fie
58
58
  ### Images and files
59
59
 
60
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.
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
62
  This doesn't mean that the files are downloaded during the build process.
63
63
  But you can directly use these URLs in your Astro components to display images or link to the files.
64
64
 
@@ -109,15 +109,16 @@ This manual schema will **always override the automatic type generation**.
109
109
 
110
110
  ## All options
111
111
 
112
- | Option | Type | Required | Description |
113
- | ---------------- | ------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------- |
114
- | `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
- | `forceUpdate` | `boolean` | | If set to `true`, the loader will fetch every entry instead of only the ones modified since the last build. |
112
+ | Option | Type | Required | Description |
113
+ | ---------------- | ----------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------- |
114
+ | `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. |
121
122
 
122
123
  ## Special cases
123
124
 
@@ -137,3 +138,15 @@ Thus, `view` collections that don't include this field can't be incrementally bu
137
138
 
138
139
  You can also alias another field as `updated` (as long as it's a date field) in your view.
139
140
  While this is possible, it's not recommended since it can lead to outdated data not being fetched.
141
+
142
+ ### JSON fields
143
+
144
+ PocketBase can store arbitrary JSON data in a `json` field.
145
+ While this data is checked to be valid JSON, there's no schema attached or enforced.
146
+ Theoretically, every entry in a collection can have a different structure in such a field.
147
+ This is why by default the loader will treat these fields as `unknown` and will not generate types for them.
148
+
149
+ If you have your own schema for these fields, you can provide them via the `jsonSchemas` option.
150
+ The keys of this record should be the field names of your json fields, while the values are Zod schemas.
151
+ This way, the loader will also generate types for these fields and **validate the data against these schemas**.
152
+ So be sure that the schema is correct, up-to-date and all entries in the collection adhere to it.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-loader-pocketbase",
3
- "version": "0.3.0",
3
+ "version": "0.4.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)",
@@ -63,7 +63,7 @@ export async function generateSchema(
63
63
  }
64
64
 
65
65
  // Parse the schema
66
- const fields = parseSchema(collection);
66
+ const fields = parseSchema(collection, options.jsonSchemas);
67
67
 
68
68
  // Check if the content field is present
69
69
  if (typeof options.content === "string" && !fields[options.content]) {
@@ -1,3 +1,5 @@
1
+ import type { z } from "astro/zod";
2
+
1
3
  /**
2
4
  * Options for the PocketBase loader.
3
5
  */
@@ -36,6 +38,14 @@ export interface PocketBaseLoaderOptions {
36
38
  * If admin credentials are provided (see `adminEmail` and `adminPassword`), this option will be ignored.
37
39
  */
38
40
  localSchema?: string;
41
+ /**
42
+ * Record of zod schemas for all JSON fields in the collection.
43
+ * The key must be the name of a field in the collection.
44
+ * If no schema is provided for a field, the value will be treated as `unknown`.
45
+ *
46
+ * Note that this will only be used for fields of type `json`.
47
+ */
48
+ jsonSchemas?: Record<string, z.ZodSchema>;
39
49
  /**
40
50
  * By default, the loader will only fetch entries that have been modified since the last fetch.
41
51
  * If you want to fetch all entries, set this to `true`.
@@ -5,7 +5,8 @@ import type {
5
5
  } from "../types/pocketbase-schema.type";
6
6
 
7
7
  export function parseSchema(
8
- collection: PocketBaseCollection
8
+ collection: PocketBaseCollection,
9
+ customSchemas: Record<string, z.ZodType> | undefined
9
10
  ): Record<string, z.ZodType> {
10
11
  // Prepare the schemas fields
11
12
  const fields: Record<string, z.ZodType> = {};
@@ -51,8 +52,13 @@ export function parseSchema(
51
52
  fieldType = parseSingleOrMultipleValues(field, z.string());
52
53
  break;
53
54
  case "json":
54
- // Parse the field as an object
55
- fieldType = z.object({});
55
+ if (customSchemas && customSchemas[field.name]) {
56
+ // Use the user defined custom schema for the field
57
+ fieldType = customSchemas[field.name];
58
+ } else {
59
+ // Parse the field as unknown JSON
60
+ fieldType = z.unknown();
61
+ }
56
62
  break;
57
63
  default:
58
64
  // Default to a string