Skip to content

Commit

Permalink
fix(collection): respect default value in null fields
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Nov 28, 2024
1 parent 006c615 commit 9fdc4d6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/utils/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,28 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: R
if (!(defaultValue instanceof Date) && typeof defaultValue === 'object') {
defaultValue = JSON.stringify(defaultValue)
}
const valueToInsert = typeof data[key] !== 'undefined' ? data[key] : defaultValue
const valueToInsert = (typeof data[key] === 'undefined' || String(data[key]) === 'null')
? defaultValue
: data[key]

fields.push(key)

if (valueToInsert === 'NULL') {
values.push(valueToInsert)
return
}

if ((collection.jsonFields || []).includes(key)) {
values.push(`'${JSON.stringify(valueToInsert).replace(/'/g, '\'\'')}'`)
}
else if (['ZodString', 'ZodEnum'].includes(underlyingType.constructor.name)) {
values.push(`'${String(valueToInsert).replace(/\n/g, '\\n').replace(/'/g, '\'\'')}'`)
}
else if (['ZodDate'].includes(underlyingType.constructor.name)) {
values.push(valueToInsert !== 'NULL' ? `'${new Date(valueToInsert as string).toISOString()}'` : defaultValue)
values.push(`'${new Date(valueToInsert as string).toISOString()}'`)
}
else if (underlyingType.constructor.name === 'ZodBoolean') {
values.push(valueToInsert !== 'NULL' ? !!valueToInsert : valueToInsert)
values.push(!!valueToInsert)
}
else {
values.push(valueToInsert)
Expand Down

0 comments on commit 9fdc4d6

Please sign in to comment.