Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 994ae98

Browse files
committed
fix(preview): transform value based on schema for sql query generation
1 parent 997fc65 commit 994ae98

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/runtime/internal/preview/collection.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,27 @@ function computeValuesBasedOnCollectionSchema(collection: CollectionInfo, data:
103103

104104
sortedKeys.forEach((key) => {
105105
const value = (properties)[key]
106-
// const underlyingType = getUnderlyingType(value as ZodType<unknown, ZodOptionalDef>)
107-
const underlyingType = (value as JsonSchema7ObjectType).type
108-
106+
const type = collection.fields[key]
109107
const defaultValue = value.default !== undefined ? value.default : 'NULL'
110108
const valueToInsert = typeof data[key] !== 'undefined' ? data[key] : defaultValue
111109

112110
fields.push(key)
113-
if (collection.fields[key] === 'json') {
111+
112+
if (type === 'json') {
114113
values.push(`'${JSON.stringify(valueToInsert).replace(/'/g, '\'\'')}'`)
115114
}
116-
else if (['string', 'enum'].includes(underlyingType)) {
117-
values.push(`'${String(valueToInsert).replace(/\n/g, '\\n').replace(/'/g, '\'\'')}'`)
115+
else if (type === 'string') {
116+
// @ts-expect-error format does exist
117+
if (['data', 'datetime'].includes(value.format)) {
118+
values.push(valueToInsert !== 'NULL' ? `'${new Date(valueToInsert).toISOString()}'` : defaultValue)
119+
}
120+
else {
121+
values.push(`'${String(valueToInsert).replace(/\n/g, '\\n').replace(/'/g, '\'\'')}'`)
122+
}
123+
}
124+
else if (type === 'boolean') {
125+
values.push(valueToInsert !== 'NULL' ? !!valueToInsert : valueToInsert)
118126
}
119-
// else if (['Date'].includes(underlyingType)) {
120-
// values.push(valueToInsert !== 'NULL' ? `'${new Date(valueToInsert as string).toISOString()}'` : defaultValue)
121-
// }
122-
// else if (underlyingType.constructor.name === 'ZodBoolean') {
123-
// values.push(valueToInsert !== 'NULL' ? !!valueToInsert : valueToInsert)
124-
// }
125127
else {
126128
values.push(valueToInsert)
127129
}

0 commit comments

Comments
 (0)