Skip to content

Commit 9fdc4d6

Browse files
committed
fix(collection): respect default value in null fields
1 parent 006c615 commit 9fdc4d6

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/utils/collection.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,28 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: R
112112
if (!(defaultValue instanceof Date) && typeof defaultValue === 'object') {
113113
defaultValue = JSON.stringify(defaultValue)
114114
}
115-
const valueToInsert = typeof data[key] !== 'undefined' ? data[key] : defaultValue
115+
const valueToInsert = (typeof data[key] === 'undefined' || String(data[key]) === 'null')
116+
? defaultValue
117+
: data[key]
116118

117119
fields.push(key)
120+
121+
if (valueToInsert === 'NULL') {
122+
values.push(valueToInsert)
123+
return
124+
}
125+
118126
if ((collection.jsonFields || []).includes(key)) {
119127
values.push(`'${JSON.stringify(valueToInsert).replace(/'/g, '\'\'')}'`)
120128
}
121129
else if (['ZodString', 'ZodEnum'].includes(underlyingType.constructor.name)) {
122130
values.push(`'${String(valueToInsert).replace(/\n/g, '\\n').replace(/'/g, '\'\'')}'`)
123131
}
124132
else if (['ZodDate'].includes(underlyingType.constructor.name)) {
125-
values.push(valueToInsert !== 'NULL' ? `'${new Date(valueToInsert as string).toISOString()}'` : defaultValue)
133+
values.push(`'${new Date(valueToInsert as string).toISOString()}'`)
126134
}
127135
else if (underlyingType.constructor.name === 'ZodBoolean') {
128-
values.push(valueToInsert !== 'NULL' ? !!valueToInsert : valueToInsert)
136+
values.push(!!valueToInsert)
129137
}
130138
else {
131139
values.push(valueToInsert)

0 commit comments

Comments
 (0)