astro-loader-pocketbase 2.0.2 → 2.2.0-next.1
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,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 type safety
|
|
112
|
+
|
|
113
|
+
By default PocketBase reports `number` and `boolean` fields as not required.
|
|
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 provide default values with `0` and `false` respectively.
|
|
127
|
+
PocketBase will also use these default values when creating new entries in the dashboard.
|
|
128
|
+
|
|
111
129
|
## Type generation
|
|
112
130
|
|
|
113
131
|
The loader can automatically generate types for your collection.
|
|
@@ -167,6 +185,7 @@ This manual schema will **always override the automatic type generation**.
|
|
|
167
185
|
| `superuserCredentials` | `{ email: string, password: string }` | | The email and password of the superuser of the PocketBase instance. This is used for automatic type generation. |
|
|
168
186
|
| `localSchema` | `string` | | The path to a local schema file. This is used for automatic type generation. |
|
|
169
187
|
| `jsonSchemas` | `Record<string, z.ZodSchema>` | | A record of Zod schemas to use for type generation of `json` fields. |
|
|
188
|
+
| `improveTypes` | `boolean` | | Whether to improve the types of `number` and `boolean` fields, removing `undefined` from them. |
|
|
170
189
|
|
|
171
190
|
## Special cases
|
|
172
191
|
|
package/package.json
CHANGED
package/src/generate-schema.ts
CHANGED
package/src/pocketbase-loader.ts
CHANGED
|
@@ -15,6 +15,16 @@ export function pocketbaseLoader(options: PocketBaseLoaderOptions): Loader {
|
|
|
15
15
|
return {
|
|
16
16
|
name: "pocketbase-loader",
|
|
17
17
|
load: async (context: LoaderContext): Promise<void> => {
|
|
18
|
+
if (
|
|
19
|
+
context.refreshContextData?.source === "astro-integration-pocketbase" &&
|
|
20
|
+
context.refreshContextData.collection &&
|
|
21
|
+
context.refreshContextData.collection !== options.collectionName
|
|
22
|
+
) {
|
|
23
|
+
// Skip the refresh if the reload was triggered by the `astro-integration-pocketbase`
|
|
24
|
+
// and the collection name does not match the current collection.
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
18
28
|
// Get the date of the last fetch to only update changed entries.
|
|
19
29
|
let lastModified = context.meta.get("last-modified");
|
|
20
30
|
|
|
@@ -65,4 +65,13 @@ 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
|
+
* This includes providing `0` or `false` as default values for number and boolean fields, respectively.
|
|
71
|
+
* With this option enabled, the schema will not generate `undefined` as possible value for number and boolean fields.
|
|
72
|
+
*
|
|
73
|
+
* Why do we need this option?
|
|
74
|
+
* PocketBase does use these values as the default values, even though it's not specified in the schema.
|
|
75
|
+
*/
|
|
76
|
+
improveTypes?: boolean;
|
|
68
77
|
}
|
|
@@ -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> = {};
|
|
@@ -73,9 +74,26 @@ 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
|
-
|
|
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"));
|
|
84
|
+
|
|
85
|
+
if (improveTypes) {
|
|
86
|
+
switch (field.type) {
|
|
87
|
+
case "number":
|
|
88
|
+
// If the field is a number, provide a default value of 0
|
|
89
|
+
fieldType = fieldType.default(0);
|
|
90
|
+
break;
|
|
91
|
+
case "bool":
|
|
92
|
+
// If the field is a boolean, provide a default value of false
|
|
93
|
+
fieldType = fieldType.default(false);
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
79
97
|
|
|
80
98
|
// If the field is not required, mark it as optional
|
|
81
99
|
if (!isRequired) {
|