-
-
Notifications
You must be signed in to change notification settings - Fork 752
fix: preserve null for optional booleans and strip empty JSON objects in refineContentFields #3783
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
base: main
Are you sure you want to change the base?
Changes from all commits
72a12b0
b279c22
4e2a322
c9abd1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,31 @@ | ||
| import type { CollectionInfo } from '@nuxt/content' | ||
| import contentManifest from '#content/manifest' | ||
|
|
||
| /** | ||
| * Refine raw fields from D1/SQLite query results into their proper JS types. | ||
| * Handles JSON parsing, boolean coercion (preserving null for absent fields), | ||
| * and removes empty JSON objects that arise from D1 column defaults. | ||
| */ | ||
| export function refineContentFields<T>(sql: string, doc: T) { | ||
| const fields = findCollectionFields(sql) | ||
| const item = { ...doc } as T | ||
| for (const key in item) { | ||
| if (fields[key as string] === 'json' && item[key] && item[key] !== 'undefined') { | ||
| item[key] = JSON.parse(item[key] as string) | ||
| const parsed = JSON.parse(item[key] as string) | ||
| if (key !== 'meta' && parsed && typeof parsed === 'object' && !Array.isArray(parsed) && Object.keys(parsed).length === 0) { | ||
| Reflect.deleteProperty(item as object, key) | ||
| } | ||
| else { | ||
| item[key] = parsed | ||
| } | ||
|
Comment on lines
13
to
+20
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.
The guard at line 8 only excludes the string Fix: extend the guard to exclude 🐛 Proposed fix- if (fields[key as string] === 'json' && item[key] && item[key] !== 'undefined') {
+ if (fields[key as string] === 'json' && item[key] && item[key] !== 'undefined' && item[key] !== 'NULL') {As a secondary concern, 🤖 Prompt for AI Agents |
||
| } | ||
| if (fields[key as string] === 'boolean' && item[key] !== 'undefined') { | ||
| item[key] = Boolean(item[key]) as never | ||
| if (item[key] == null) { | ||
| Reflect.deleteProperty(item as object, key) | ||
| } | ||
| else { | ||
| item[key] = Boolean(item[key]) as never | ||
| } | ||
|
Comment on lines
22
to
+28
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. Handle At Line 22, Treat Also applies to: 27-30 🧰 Tools🪛 GitHub Check: ubuntu[failure] 19-19: 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -21,6 +37,7 @@ export function refineContentFields<T>(sql: string, doc: T) { | |
| return item | ||
| } | ||
|
|
||
| /** Extract the field type map for the collection referenced in the SQL query. */ | ||
| function findCollectionFields(sql: string): Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'> { | ||
| const table = sql.match(/FROM\s+(\w+)/) | ||
| if (!table) { | ||
|
|
@@ -31,6 +48,7 @@ function findCollectionFields(sql: string): Record<string, 'string' | 'number' | | |
| return info?.fields || {} | ||
| } | ||
|
|
||
| /** Strip the `_content_` prefix from a D1 table name to get the collection name. */ | ||
| function getCollectionName(table: string) { | ||
| return table.replace(/^_content_/, '') | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.