Skip to content

Commit e09df83

Browse files
authored
fix: encode empty Uint8Array as NULL for BYTEA parameters (#146)
* fix: encode empty Uint8Array as NULL for BYTEA parameters Problem: When calling view actions with optional BYTEA parameters, there was no way to pass SQL NULL from JavaScript. Passing JavaScript `null` resulted in VarType.NULL (generic null), which didn't work correctly with SQL `IS NULL` checks for BYTEA columns. Root Cause: In kwilEncoding.ts, empty Uint8Array(0) was always encoded as "not null empty bytea" using encodeNotNull(), which produces '\x' in PostgreSQL. This is semantically different from NULL and causes SQL `IS NULL` checks to fail. Solution: Treat empty Uint8Array(0) as BYTEA NULL by encoding it with encodeNull() instead of encodeNotNull(). This provides a JavaScript-idiomatic way to represent BYTEA NULL while maintaining backward compatibility. Changes: - src/utils/kwilEncoding.ts (encodeValue function): * Add length check for Uint8Array before encoding * Empty Uint8Array (length === 0) → encodeNull() * Non-empty Uint8Array (length > 0) → encodeNotNull(value) resolves: #145 * chore: apply suggestion * chore: apply suggestion * chore: bump version
1 parent 7484110 commit e09df83

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@trufnetwork/kwil-js",
3-
"version": "0.9.9",
3+
"version": "0.9.10",
44
"description": "",
55
"main": "./dist/index.js",
66
"module": "./dist-esm/index.mjs",

src/utils/kwilEncoding.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ export function encodeValue(value: ValueType, o?: VarType): Uint8Array {
220220

221221
// handle Uint8Array case
222222
if (value instanceof Uint8Array) {
223+
// Treat empty Uint8Array as NULL for BYTEA type
224+
// This allows JavaScript code to pass new Uint8Array(0) to represent BYTEA NULL
225+
if (value.length === 0) {
226+
return encodeNull();
227+
}
223228
return encodeNotNull(value);
224229
}
225230

@@ -263,8 +268,14 @@ function overrideValue(v: ValueType, o: VarType): Uint8Array {
263268
return encodeNotNull(stringToBytes(v.toString()));
264269
case VarType.UUID:
265270
return encodeNotNull(convertUuidToBytes(v as string));
266-
case VarType.BYTEA:
267-
return encodeNotNull(v as Uint8Array);
271+
case VarType.BYTEA: {
272+
// Treat empty Uint8Array as NULL for consistency with normal encoding path
273+
const byteaValue = v as Uint8Array;
274+
if (byteaValue.length === 0) {
275+
return encodeNull();
276+
}
277+
return encodeNotNull(byteaValue);
278+
}
268279
default:
269280
throw new Error('invalid scalar value');
270281
}

0 commit comments

Comments
 (0)