astro-loader-pocketbase 2.2.0-next.2 → 2.2.1-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
@@ -110,7 +110,7 @@ It's recommended to use e.g. the title of the entry to be easily searchable and
110
110
 
111
111
  ### Improved types
112
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.
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
114
  This means that the loader will add `undefined` to the type of these fields.
115
115
  If you want to enforce that these fields are always present, you can set the `improveTypes` option to `true`.
116
116
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-loader-pocketbase",
3
- "version": "2.2.0-next.2",
3
+ "version": "2.2.1-next.1",
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)",
@@ -5,6 +5,7 @@ import { generateSchema } from "./generate-schema";
5
5
  import { loadEntries } from "./load-entries";
6
6
  import type { PocketBaseLoaderOptions } from "./types/pocketbase-loader-options.type";
7
7
  import { getSuperuserToken } from "./utils/get-superuser-token";
8
+ import { shouldRefresh } from "./utils/should-refresh";
8
9
 
9
10
  /**
10
11
  * Loader for collections stored in PocketBase.
@@ -15,13 +16,12 @@ export function pocketbaseLoader(options: PocketBaseLoaderOptions): Loader {
15
16
  return {
16
17
  name: "pocketbase-loader",
17
18
  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.
19
+ // Check if the collection should be refreshed.
20
+ const refresh = shouldRefresh(
21
+ context.refreshContextData,
22
+ options.collectionName
23
+ );
24
+ if (!refresh) {
25
25
  return;
26
26
  }
27
27
 
@@ -25,11 +25,9 @@ export function parseSchema(
25
25
  // Determine the field type and create the corresponding Zod type
26
26
  switch (field.type) {
27
27
  case "number":
28
- // Coerce the value to a number
29
28
  fieldType = z.number();
30
29
  break;
31
30
  case "bool":
32
- // Coerce the value to a boolean
33
31
  fieldType = z.boolean();
34
32
  break;
35
33
  case "date":
@@ -0,0 +1,32 @@
1
+ import type { LoaderContext } from "astro/loaders";
2
+
3
+ /**
4
+ * Checks if the collection should be refreshed.
5
+ */
6
+ export function shouldRefresh(
7
+ context: LoaderContext["refreshContextData"],
8
+ collectionName: string
9
+ ): boolean {
10
+ // Check if the refresh was triggered by the `astro-integration-pocketbase`
11
+ // and the correct metadata is provided.
12
+ if (
13
+ !context ||
14
+ context.source !== "astro-integration-pocketbase" ||
15
+ !context.collection
16
+ ) {
17
+ return true;
18
+ }
19
+
20
+ // Check if the collection name matches the current collection.
21
+ if (typeof context.collection === "string") {
22
+ return context.collection === collectionName;
23
+ }
24
+
25
+ // Check if the collection is included in the list of collections.
26
+ if (Array.isArray(context.collection)) {
27
+ return context.collection.includes(collectionName);
28
+ }
29
+
30
+ // Should not happen but return true to be safe.
31
+ return true;
32
+ }