File tree 2 files changed +39
-7
lines changed
2 files changed +39
-7
lines changed Original file line number Diff line number Diff line change @@ -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
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments