From 9fdc4d6371b610cc152daf5b865af3bb2b8a888e Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Thu, 28 Nov 2024 10:07:36 +0100 Subject: [PATCH] fix(collection): respect default value in `null` fields --- src/utils/collection.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/utils/collection.ts b/src/utils/collection.ts index f8ff196aa..0dfa426a2 100644 --- a/src/utils/collection.ts +++ b/src/utils/collection.ts @@ -112,9 +112,17 @@ 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, '\'\'')}'`) } @@ -122,10 +130,10 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: R 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)