Skip to content

fix: generate correct collection insert for object and array default values #3277

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

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/utils/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,8 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: P
const value = (collection.extendedSchema).shape[key]
const underlyingType = getUnderlyingType(value as ZodType<unknown, ZodOptionalDef>)

let defaultValue = value?._def.defaultValue ? value?._def.defaultValue() : 'NULL'
const defaultValue = value?._def.defaultValue ? value?._def.defaultValue() : 'NULL'

if (!(defaultValue instanceof Date) && typeof defaultValue === 'object') {
defaultValue = JSON.stringify(defaultValue)
}
const valueToInsert = (typeof data[key] === 'undefined' || String(data[key]) === 'null')
? defaultValue
: data[key]
Expand Down
10 changes: 8 additions & 2 deletions test/unit/generateCollectionInsert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('generateCollectionInsert', () => {
otherField: z.string().default('untitled'),
otherField2: z.boolean().default(true),
date: z.date().default(new Date('2022-01-01')),
object: z.object({ foo: z.string() }).default(() => ({ foo: 'bar' })),
array: z.array(z.string()).default(() => []),
}),
}))!
const { queries: sql } = generateCollectionInsert(collection, {
Expand All @@ -24,7 +26,7 @@ describe('generateCollectionInsert', () => {
expect(sql[0]).toBe([
`INSERT INTO ${getTableName('content')}`,
' VALUES',
' (\'foo.md\', 13, \'2022-01-01T00:00:00.000Z\', \'md\', \'{}\', \'untitled\', true, \'foo\', \'vPdICyZ7sjhw1YY4ISEATbCTIs_HqNpMVWHnBWhOOYY\');',
' (\'foo.md\', \'[]\', 13, \'2022-01-01T00:00:00.000Z\', \'md\', \'{}\', \'{"foo":"bar"}\', \'untitled\', true, \'foo\', \'bnUQ85H_Zf72faGIQhV0i9QeTEnf1ueEIaMAO8aAAGw\');',
].join(''))
})

Expand All @@ -37,6 +39,8 @@ describe('generateCollectionInsert', () => {
otherField: z.string().default('untitled'),
otherField2: z.boolean().default(true),
date: z.date().default(new Date('2022-01-01')),
object: z.object({ foo: z.string() }).default(() => ({ foo: 'bar' })),
array: z.array(z.string()).default(() => []),
}),
}))!
const { queries: sql } = generateCollectionInsert(collection, {
Expand All @@ -48,12 +52,14 @@ describe('generateCollectionInsert', () => {
otherField: 'foo',
otherField2: false,
date: new Date('2022-01-02'),
object: { foo: 'baz' },
array: ['foo'],
})

expect(sql[0]).toBe([
`INSERT INTO ${getTableName('content')}`,
' VALUES',
' (\'foo.md\', 42, \'2022-01-02T00:00:00.000Z\', \'md\', \'{}\', \'foo\', false, \'foo\', \'R5zX5zuyfvCtvXPcgINuEjEoHmZnse8kATeDd4V7I-c\');',
' (\'foo.md\', \'["foo"]\', 42, \'2022-01-02T00:00:00.000Z\', \'md\', \'{}\', \'{"foo":"baz"}\', \'foo\', false, \'foo\', \'ImMjHvkHl82Jx1bjlpanb9d3i_HQIbjNFverKKbZLME\');',
].join(''))
})

Expand Down