Handle optional json columns

This commit is contained in:
Kenny Daniel 2025-07-28 17:04:46 -07:00
parent c4df408063
commit 22e40cf6c4
No known key found for this signature in database
GPG Key ID: 90AB653A8CAD7E45
3 changed files with 18 additions and 4 deletions

@ -56,10 +56,10 @@
},
"devDependencies": {
"@babel/eslint-parser": "7.28.0",
"@types/node": "24.0.10",
"@types/node": "24.1.0",
"@vitest/coverage-v8": "3.2.4",
"eslint": "9.30.1",
"eslint-plugin-jsdoc": "51.3.3",
"eslint": "9.32.0",
"eslint-plugin-jsdoc": "52.0.0",
"typescript": "5.8.3",
"vitest": "3.2.4"
}

@ -30,7 +30,7 @@ export function unconvert(element, values) {
if (ctype === 'JSON') {
if (!Array.isArray(values)) throw new Error('JSON must be an array')
const encoder = new TextEncoder()
return values.map(v => encoder.encode(JSON.stringify(v)))
return values.map(v => v === undefined ? undefined : encoder.encode(JSON.stringify(v)))
}
if (ctype === 'UTF8') {
if (!Array.isArray(values)) throw new Error('strings must be an array')

@ -28,6 +28,20 @@ describe('unconvert', () => {
expect(new TextDecoder().decode(result[1])).toEqual(JSON.stringify({ hello: 'world' }))
})
it('should handle undefined values in JSON arrays', () => {
/** @type {SchemaElement} */
const schema = { name: 'test', converted_type: 'JSON' }
const input = [{ foo: 'bar' }, undefined, { hello: 'world' }]
const result = unconvert(schema, input)
expect(result).toHaveLength(3)
expect(result[0]).toBeInstanceOf(Uint8Array)
expect(result[1]).toBeUndefined()
expect(result[2]).toBeInstanceOf(Uint8Array)
expect(new TextDecoder().decode(result[0])).toEqual(JSON.stringify({ foo: 'bar' }))
expect(new TextDecoder().decode(result[2])).toEqual(JSON.stringify({ hello: 'world' }))
})
it('should convert string array to Uint8Array when converted_type = UTF8', () => {
/** @type {SchemaElement} */
const schema = { name: 'test', converted_type: 'UTF8' }