astro-loader-pocketbase 2.1.0 → 2.2.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,6 +108,23 @@ 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
112
+
113
+ By default PocketBase reports `number` and `boolean` fields as not required, even though the API will always return at least `0` and `false` respectively.
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`.
116
+
117
+ ```ts
118
+ const blog = defineCollection({
119
+ loader: pocketbaseLoader({
120
+ ...options,
121
+ improveTypes: true
122
+ })
123
+ });
124
+ ```
125
+
126
+ This will remove `undefined` from the type of these fields and mark them as required.
127
+
111
128
  ## Type generation
112
129
 
113
130
  The loader can automatically generate types for your collection.
@@ -167,6 +184,7 @@ This manual schema will **always override the automatic type generation**.
167
184
  | `superuserCredentials` | `{ email: string, password: string }` | | The email and password of the superuser of the PocketBase instance. This is used for automatic type generation. |
168
185
  | `localSchema` | `string` | | The path to a local schema file. This is used for automatic type generation. |
169
186
  | `jsonSchemas` | `Record<string, z.ZodSchema>` | | A record of Zod schemas to use for type generation of `json` fields. |
187
+ | `improveTypes` | `boolean` | | Whether to improve the types of `number` and `boolean` fields, removing `undefined` from them. |
170
188
 
171
189
  ## Special cases
172
190
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-loader-pocketbase",
3
- "version": "2.1.0",
3
+ "version": "2.2.0-next.2",
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)",
@@ -22,14 +22,14 @@
22
22
  },
23
23
  "devDependencies": {
24
24
  "@eslint/js": "^9.18.0",
25
- "@stylistic/eslint-plugin": "^2.12.1",
26
- "@types/node": "^22.10.5",
27
- "astro": "^5.1.5",
25
+ "@stylistic/eslint-plugin": "^2.13.0",
26
+ "@types/node": "^22.10.7",
27
+ "astro": "^5.1.8",
28
28
  "eslint": "^9.18.0",
29
29
  "globals": "^15.14.0",
30
30
  "husky": "^9.1.7",
31
31
  "typescript": "^5.7.3",
32
- "typescript-eslint": "^8.19.1"
32
+ "typescript-eslint": "^8.21.0"
33
33
  },
34
34
  "keywords": [
35
35
  "astro",
@@ -60,7 +60,8 @@ export async function generateSchema(
60
60
  const fields = parseSchema(
61
61
  collection,
62
62
  options.jsonSchemas,
63
- hasSuperuserRights
63
+ hasSuperuserRights,
64
+ options.improveTypes ?? false
64
65
  );
65
66
 
66
67
  // Check if custom id field is present
@@ -65,4 +65,12 @@ export interface PocketBaseLoaderOptions {
65
65
  * Note that this will only be used for fields of type `json`.
66
66
  */
67
67
  jsonSchemas?: Record<string, z.ZodSchema>;
68
+ /**
69
+ * Whether to improve the types of the generated schema.
70
+ * With this option enabled, the schema will not include `undefined` as possible value for number and boolean fields and mark them as required.
71
+ *
72
+ * Why do we need this option?
73
+ * 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.
74
+ */
75
+ improveTypes?: boolean;
68
76
  }
@@ -7,7 +7,8 @@ import type {
7
7
  export function parseSchema(
8
8
  collection: PocketBaseCollection,
9
9
  customSchemas: Record<string, z.ZodType> | undefined,
10
- hasSuperuserRights: boolean
10
+ hasSuperuserRights: boolean,
11
+ improveTypes: boolean
11
12
  ): Record<string, z.ZodType> {
12
13
  // Prepare the schemas fields
13
14
  const fields: Record<string, z.ZodType> = {};
@@ -25,11 +26,11 @@ export function parseSchema(
25
26
  switch (field.type) {
26
27
  case "number":
27
28
  // Coerce the value to a number
28
- fieldType = z.coerce.number();
29
+ fieldType = z.number();
29
30
  break;
30
31
  case "bool":
31
32
  // Coerce the value to a boolean
32
- fieldType = z.coerce.boolean();
33
+ fieldType = z.boolean();
33
34
  break;
34
35
  case "date":
35
36
  case "autodate":
@@ -73,9 +74,13 @@ export function parseSchema(
73
74
  break;
74
75
  }
75
76
 
76
- // Check if the field is required (onCreate autodate fields are always set)
77
77
  const isRequired =
78
- field.required || (field.type === "autodate" && field.onCreate);
78
+ // Check if the field is required
79
+ field.required ||
80
+ // `onCreate autodate` fields are always set
81
+ (field.type === "autodate" && field.onCreate) ||
82
+ // Improve number and boolean types by providing default values
83
+ (improveTypes && (field.type === "number" || field.type === "bool"));
79
84
 
80
85
  // If the field is not required, mark it as optional
81
86
  if (!isRequired) {