-
Notifications
You must be signed in to change notification settings - Fork 4
feat(expand): Enables the use of Pocketbases 'Expand' property #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
cf-david-easton
wants to merge
10
commits into
pawcoding:master
Choose a base branch
from
cf-david-easton:add-expand-option-to-loader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6b3284e
feat(expand): added fetching and schema gen for expand property from …
cf-david-easton 4134e77
Apply suggestions from code review
cf-david-easton 75d2917
feat(expand): added ability to call multiple layers of depth on expan…
cf-david-easton 258f04d
feat(expand): added length guard for expand option
cf-david-easton 9d20391
feat(expand): added guard for expand nesting when no more chained seg…
cf-david-easton 3933c35
feat(expand): updated expand schema parsing to more accurately repr…
cf-david-easton e6b541f
feat(expand): implemented feedback and simplified expand typing
cf-david-easton bc1cca0
feat(expand): removed unused const
cf-david-easton 0f342cb
Merge branch 'master' into add-expand-option-to-loader
cf-david-easton 3f51a05
feat(expand): corrected typing for collection in test
cf-david-easton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,9 +1,9 @@ | ||||||||||||||||
import type { ZodSchema } from "astro/zod"; | ||||||||||||||||
import { z } from "astro/zod"; | ||||||||||||||||
import type { PocketBaseCollection } from "../types/pocketbase-collection.type"; | ||||||||||||||||
import type { PocketBaseLoaderOptions } from "../types/pocketbase-loader-options.type"; | ||||||||||||||||
import type { PocketBaseCollection } from "../types/pocketbase-schema.type"; | ||||||||||||||||
import { getRemoteSchema } from "./get-remote-schema"; | ||||||||||||||||
import { parseSchema } from "./parse-schema"; | ||||||||||||||||
import { parseExpandedSchemaField, parseSchema } from "./parse-schema"; | ||||||||||||||||
import { readLocalSchema } from "./read-local-schema"; | ||||||||||||||||
import { transformFiles } from "./transform-files"; | ||||||||||||||||
|
||||||||||||||||
|
@@ -35,6 +35,7 @@ export async function generateSchema( | |||||||||||||||
token: string | undefined | ||||||||||||||||
): Promise<ZodSchema> { | ||||||||||||||||
let collection: PocketBaseCollection | undefined; | ||||||||||||||||
const expandedFields: Record<string, z.ZodType> = {}; | ||||||||||||||||
|
||||||||||||||||
if (token) { | ||||||||||||||||
// Try to get the schema directly from the PocketBase instance | ||||||||||||||||
|
@@ -133,10 +134,51 @@ export async function generateSchema( | |||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// Combine the basic schema with the parsed fields | ||||||||||||||||
if (options.expand && options.expand.length > 0) { | ||||||||||||||||
for (const expandedFieldName of options.expand) { | ||||||||||||||||
const [currentLevelFieldName, ...deeperExpandFields] = | ||||||||||||||||
getCurrentLevelExpandedFieldName(expandedFieldName); | ||||||||||||||||
|
||||||||||||||||
const expandedFieldDefinition = collection.fields.find( | ||||||||||||||||
(field) => field.name === currentLevelFieldName | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
if (!expandedFieldDefinition) { | ||||||||||||||||
throw new Error( | ||||||||||||||||
`The provided field in the expand property "${expandedFieldName}" is not present in the schema of the collection "${options.collectionName}".\nThis will lead to use unable to provide a definition for this field.` | ||||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if (!expandedFieldDefinition.collectionId) { | ||||||||||||||||
throw new Error( | ||||||||||||||||
`The provided field in the expand property "${expandedFieldName}" does not have an associated collection linked to it, we need this in order to know the shape of the related schema.` | ||||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
const expandedSchema = await generateSchema( | ||||||||||||||||
{ | ||||||||||||||||
collectionName: expandedFieldDefinition.collectionId, | ||||||||||||||||
superuserCredentials: options.superuserCredentials, | ||||||||||||||||
expand: deeperExpandFields.length ? deeperExpandFields : undefined, | ||||||||||||||||
localSchema: options.localSchema, | ||||||||||||||||
jsonSchemas: options.jsonSchemas, | ||||||||||||||||
improveTypes: options.improveTypes, | ||||||||||||||||
url: options.url | ||||||||||||||||
}, | ||||||||||||||||
token | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
expandedFields[expandedFieldName] = parseExpandedSchemaField( | ||||||||||||||||
expandedFieldDefinition, | ||||||||||||||||
expandedSchema | ||||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
const schema = z.object({ | ||||||||||||||||
...BASIC_SCHEMA, | ||||||||||||||||
...fields | ||||||||||||||||
...fields, | ||||||||||||||||
expand: z.optional(z.object(expandedFields)) | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
// Get all file fields | ||||||||||||||||
|
@@ -154,3 +196,15 @@ export async function generateSchema( | |||||||||||||||
transformFiles(options.url, fileFields, entry) | ||||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
function getCurrentLevelExpandedFieldName(s: string): Array<string> { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
const fields = s.split("."); | ||||||||||||||||
|
||||||||||||||||
if (fields.length >= 7) { | ||||||||||||||||
throw new Error( | ||||||||||||||||
`Expand value ${s} exceeds 6 levels of depth that Pocketbase allows` | ||||||||||||||||
); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
return fields; | ||||||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { PocketBaseSchemaEntry } from "./pocketbase-schema.type"; | ||
|
||
/** | ||
* Base interface for all PocketBase collections. | ||
*/ | ||
export interface PocketBaseCollection { | ||
/** | ||
* ID of the collection. | ||
*/ | ||
id: string; | ||
/** | ||
* Name of the collection | ||
*/ | ||
name: string; | ||
/** | ||
* Type of the collection. | ||
*/ | ||
type: "base" | "view" | "auth"; | ||
/** | ||
* Schema of the collection. | ||
*/ | ||
fields: Array<PocketBaseSchemaEntry>; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like with the fields above, I'd keep this as a console error and don't throw an error. We can still get the base data of the current collection, but the expanded object will just be empty.