-
Notifications
You must be signed in to change notification settings - Fork 42
generate metadata #145
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
Open
jackyscript
wants to merge
14
commits into
patmood:main
Choose a base branch
from
jackyscript:field_constraints
base: main
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.
Open
generate metadata #145
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9471294
generate field constraints
jackyscript b529617
add cli options
jackyscript 1777165
adjust gitignore
jackyscript 5115df4
update typegen
jackyscript dd47780
adjust gitignore
jackyscript b712c10
adjust according to feedback
jackyscript 55df741
remove redundant check
jackyscript 4551582
provide default to pass typecheck
jackyscript 1094831
extract createFieldMetadata
jackyscript 3a04063
set metadataOut optional to pass typecheck
jackyscript 8cc3797
add tests and snapshots
jackyscript 922bc5a
format
jackyscript 452d92d
adjust tests and types
jackyscript d3655f4
add newline
jackyscript 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| node_modules | ||
| pocketbase-types.ts | ||
| pocketbase-metadata.ts | ||
| coverage | ||
| *.db-shm | ||
| *.db-wal | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,65 @@ | ||
| import { FieldSchema } from "./types" | ||
| import { toPascalCase } from "./utils" | ||
|
|
||
| export function createFieldMetadata( | ||
| name: string, | ||
| schema: Array<FieldSchema> | ||
| ): string { | ||
|
|
||
| const metadata = schema | ||
| .map((field) => { | ||
| const parts: string[] = [] | ||
|
|
||
| // numeric range | ||
| if (typeof field.min === "number" && field.min !== 0) { | ||
| parts.push(`\t\tmin: ${field.min},`) | ||
| } | ||
| if (typeof field.max === "number" && field.max !== 0) { | ||
| parts.push(`\t\tmax: ${field.max},`) | ||
| } | ||
|
|
||
| // selection and file-related | ||
| if (typeof (field as any).maxSelect === "number" && (field as any).maxSelect !== 0) { | ||
| parts.push(`\t\tmaxSelect: ${(field as any).maxSelect},`) | ||
| } | ||
| if (typeof (field as any).maxSize === "number" && (field as any).maxSize !== 0) { | ||
| parts.push(`\t\tmaxSize: ${(field as any).maxSize},`) | ||
| } | ||
| if (Array.isArray((field as any).mimeTypes) && (field as any).mimeTypes.length > 0) { | ||
| parts.push(`\t\tmimeTypes: ${JSON.stringify((field as any).mimeTypes)},`) | ||
| } | ||
|
|
||
| // presence / uniqueness | ||
| if (typeof field.required === "boolean") { | ||
| parts.push(`\t\trequired: ${field.required},`) | ||
| } | ||
| if (typeof field.unique === "boolean") { | ||
| parts.push(`\t\tunique: ${field.unique},`) | ||
| } | ||
|
|
||
| // pattern / values | ||
| if (typeof (field as any).pattern === "string" && (field as any).pattern) { | ||
| parts.push(`\t\tpattern: ${JSON.stringify((field as any).pattern)},`) | ||
| } | ||
| if (Array.isArray((field as any).values) && (field as any).values.length > 0) { | ||
| parts.push(`\t\tvalues: ${JSON.stringify((field as any).values)},`) | ||
| } | ||
|
|
||
| // lifecycle flags | ||
| if (typeof (field as any).onCreate === "boolean") { | ||
| parts.push(`\t\tonCreate: ${(field as any).onCreate},`) | ||
| } | ||
| if (typeof (field as any).onUpdate === "boolean") { | ||
| parts.push(`\t\tonUpdate: ${(field as any).onUpdate},`) | ||
| } | ||
|
|
||
| if (parts.length === 0) return null | ||
|
|
||
| return `\t${field.name}: {\n${parts.join("\n")}\n\t},` | ||
| }) | ||
| .filter(Boolean) | ||
| if (metadata.length === 0) return "" | ||
|
|
||
| const typeName = toPascalCase(name) | ||
| return `export const ${typeName}FieldMetadata = {\n${metadata.join("\n")}\n} as const\n` | ||
| } |
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,76 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`createFieldMetadata creates min/max metadata 1`] = ` | ||
| "export const UsersFieldMetadata = { | ||
| id: { | ||
| min: 15, | ||
| max: 15, | ||
| required: true, | ||
| unique: false, | ||
| }, | ||
| } as const | ||
| " | ||
| `; | ||
|
|
||
| exports[`createFieldMetadata generates metadata for the everything collection 1`] = ` | ||
| "export const EverythingFieldMetadata = { | ||
| id: { | ||
| min: 15, | ||
| max: 15, | ||
| required: true, | ||
| unique: false, | ||
| pattern: "^[a-z0-9]+$", | ||
| }, | ||
| select_field: { | ||
| maxSelect: 1, | ||
| required: false, | ||
| unique: false, | ||
| values: ["optionA","OptionA","optionB"], | ||
| }, | ||
| json_field: { | ||
| maxSize: 2000000, | ||
| required: false, | ||
| unique: false, | ||
| }, | ||
| three_files_field: { | ||
| maxSelect: 99, | ||
| required: false, | ||
| unique: false, | ||
| }, | ||
| custom_relation_field: { | ||
| maxSelect: 999, | ||
| required: false, | ||
| unique: false, | ||
| }, | ||
| created: { | ||
| required: false, | ||
| unique: false, | ||
| onCreate: true, | ||
| onUpdate: false, | ||
| }, | ||
| updated: { | ||
| required: false, | ||
| unique: false, | ||
| onCreate: false, | ||
| onUpdate: true, | ||
| }, | ||
| } as const | ||
| " | ||
| `; | ||
|
|
||
| exports[`createFieldMetadata includes other metadata fields when present 1`] = ` | ||
| "export const MediaFieldMetadata = { | ||
| avatar: { | ||
| maxSelect: 1, | ||
| maxSize: 1024, | ||
| mimeTypes: ["image/png"], | ||
| required: false, | ||
| unique: true, | ||
| pattern: "^abc$", | ||
| values: ["a","b"], | ||
| onCreate: true, | ||
| onUpdate: false, | ||
| }, | ||
| } as const | ||
| " | ||
| `; |
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.
Uh oh!
There was an error while loading. Please reload this page.