Skip to content

Commit 29e7026

Browse files
committed
feat: add support for Uint8Array
1 parent bc41e1e commit 29e7026

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ export const escape = (
388388
if (isDate(value)) return dateToString(value, timezone || 'local');
389389
if (Array.isArray(value)) return arrayToList(value, timezone);
390390
if (Buffer.isBuffer(value)) return bufferToString(value);
391+
if (value instanceof Uint8Array)
392+
return bufferToString(Buffer.from(value));
391393
if (hasSqlString(value)) return String(value.toSqlString());
392394
if (!(stringifyObjects === undefined || stringifyObjects === null))
393395
return escapeString(String(value));
@@ -453,6 +455,7 @@ export const format = (
453455
!hasSqlString(currentValue) &&
454456
!Array.isArray(currentValue) &&
455457
!Buffer.isBuffer(currentValue) &&
458+
!(currentValue instanceof Uint8Array) &&
456459
!isDate(currentValue) &&
457460
isRecord(currentValue)
458461
) {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type SqlValue =
88
| boolean
99
| Date
1010
| Buffer
11+
| Uint8Array
1112
| Raw
1213
| Record<string, unknown>
1314
| SqlValue[]

test/stringify-objects-as-false.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,42 @@ describe('Object placeholder after SET but outside SET clause', () => {
209209
});
210210
});
211211

212+
describe('Uint8Array parameter', () => {
213+
it('should format Uint8Array as hex string', () => {
214+
const data = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]); // "Hello" in hex
215+
const query = format('SELECT * FROM files WHERE data = ?', [data]);
216+
217+
assert.strictEqual(query, "SELECT * FROM files WHERE data = X'48656c6c6f'");
218+
});
219+
220+
it('should format Uint8Array in INSERT statements', () => {
221+
const data = new Uint8Array([0xde, 0xad, 0xbe, 0xef]);
222+
const query = format('INSERT INTO files (name, data) VALUES (?, ?)', [
223+
'test',
224+
data,
225+
]);
226+
227+
assert.strictEqual(
228+
query,
229+
"INSERT INTO files (name, data) VALUES ('test', X'deadbeef')"
230+
);
231+
});
232+
233+
it('should format empty Uint8Array', () => {
234+
const data = new Uint8Array([]);
235+
const query = format('SELECT * FROM files WHERE data = ?', [data]);
236+
237+
assert.strictEqual(query, "SELECT * FROM files WHERE data = X''");
238+
});
239+
240+
it('should not expand Uint8Array in SET clause', () => {
241+
const data = new Uint8Array([0x01, 0x02]);
242+
const query = format('UPDATE files SET ?', [data]);
243+
244+
assert.strictEqual(query, "UPDATE files SET X'0102'");
245+
});
246+
});
247+
212248
describe('SET as a column name', () => {
213249
it('should not expand object when SET is a column name in WHERE', () => {
214250
const query = format('SELECT * FROM t WHERE SET = ? AND id = ?', [

test/stringify-objects-as-true.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,42 @@ describe('Object placeholder after SET but outside SET clause', () => {
203203
});
204204
});
205205

206+
describe('Uint8Array parameter', () => {
207+
it('should format Uint8Array as hex string', () => {
208+
const data = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]); // "Hello" in hex
209+
const query = format('SELECT * FROM files WHERE data = ?', [data]);
210+
211+
assert.strictEqual(query, "SELECT * FROM files WHERE data = X'48656c6c6f'");
212+
});
213+
214+
it('should format Uint8Array in INSERT statements', () => {
215+
const data = new Uint8Array([0xde, 0xad, 0xbe, 0xef]);
216+
const query = format('INSERT INTO files (name, data) VALUES (?, ?)', [
217+
'test',
218+
data,
219+
]);
220+
221+
assert.strictEqual(
222+
query,
223+
"INSERT INTO files (name, data) VALUES ('test', X'deadbeef')"
224+
);
225+
});
226+
227+
it('should format empty Uint8Array', () => {
228+
const data = new Uint8Array([]);
229+
const query = format('SELECT * FROM files WHERE data = ?', [data]);
230+
231+
assert.strictEqual(query, "SELECT * FROM files WHERE data = X''");
232+
});
233+
234+
it('should not expand Uint8Array in SET clause', () => {
235+
const data = new Uint8Array([0x01, 0x02]);
236+
const query = format('UPDATE files SET ?', [data]);
237+
238+
assert.strictEqual(query, "UPDATE files SET X'0102'");
239+
});
240+
});
241+
206242
describe('SET as a column name', () => {
207243
it('should stringify object when SET is a column name in WHERE', () => {
208244
const query = format('SELECT * FROM t WHERE SET = ? AND id = ?', [

0 commit comments

Comments
 (0)