diff --git a/connection.ts b/connection.ts index 52c6d492..c571aad2 100644 --- a/connection.ts +++ b/connection.ts @@ -30,7 +30,7 @@ import { BufReader, BufWriter } from "./deps.ts"; import { PacketWriter } from "./packet_writer.ts"; import { hashMd5Password, readUInt32BE } from "./utils.ts"; import { PacketReader } from "./packet_reader.ts"; -import { QueryConfig, QueryResult, Query } from "./query.ts"; +import { Query, QueryConfig, QueryResult } from "./query.ts"; import { parseError } from "./error.ts"; import type { ConnectionParams } from "./connection_params.ts"; import { DeferredStack } from "./deferred.ts"; diff --git a/decode.ts b/decode.ts index 192495ca..3e32098f 100644 --- a/decode.ts +++ b/decode.ts @@ -195,6 +195,10 @@ function decodeIntArray(value: string): any { return parseArray(value, decodeBaseTenInt); } +function decodeJsonArray(value: string): unknown[] { + return parseArray(value, JSON.parse); +} + // deno-lint-ignore no-explicit-any function decodeText(value: Uint8Array, typeOid: number): any { const strValue = decoder.decode(value); @@ -253,6 +257,9 @@ function decodeText(value: Uint8Array, typeOid: number): any { case Oid.json: case Oid.jsonb: return JSON.parse(strValue); + case Oid.json_array: + case Oid.jsonb_array: + return decodeJsonArray(strValue); case Oid.bytea: return decodeBytea(strValue); default: diff --git a/deps.ts b/deps.ts index fbd99e49..fdb5cc5b 100644 --- a/deps.ts +++ b/deps.ts @@ -1,7 +1,4 @@ -export { - BufReader, - BufWriter, -} from "https://deno.land/std@0.69.0/io/bufio.ts"; +export { BufReader, BufWriter } from "https://deno.land/std@0.69.0/io/bufio.ts"; export { copyBytes } from "https://deno.land/std@0.67.0/bytes/mod.ts"; export { deferred } from "https://deno.land/std@0.69.0/async/deferred.ts"; export type { Deferred } from "https://deno.land/std@0.69.0/async/deferred.ts"; diff --git a/oid.ts b/oid.ts index 5bdbb8cd..300407d8 100644 --- a/oid.ts +++ b/oid.ts @@ -23,7 +23,7 @@ export const Oid = { xml: 142, _xml: 143, pg_node_tree: 194, - _json: 199, + json_array: 199, smgr: 210, index_am_handler: 325, point: 600, @@ -146,7 +146,7 @@ export const Oid = { regdictionary: 3769, _regdictionary: 3770, jsonb: 3802, - _jsonb: 3807, + jsonb_array: 3807, anyrange: 3831, event_trigger: 3838, int4range: 3904, diff --git a/tests/data_types.ts b/tests/data_types.ts index 46feb2a1..e30a185b 100644 --- a/tests/data_types.ts +++ b/tests/data_types.ts @@ -267,3 +267,37 @@ testClient(async function bpcharNestedArray() { ); assertEquals(result.rows[0], [[["AB1234"], ["4321BA"]]]); }); + +testClient(async function jsonArray() { + const json_array = await CLIENT.query( + `SELECT ARRAY_AGG(A) FROM ( + SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A + UNION ALL + SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A + ) A`, + ); + + assertEquals(json_array.rows[0][0], [{ X: "1" }, { Y: "2" }]); + + const json_array_nested = await CLIENT.query( + `SELECT ARRAY[ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)], ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)]] FROM ( + SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A + UNION ALL + SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A + ) A`, + ); + + assertEquals( + json_array_nested.rows[0][0], + [ + [ + [{ X: "1" }, { Y: "2" }], + [{ X: "1" }, { Y: "2" }], + ], + [ + [{ X: "1" }, { Y: "2" }], + [{ X: "1" }, { Y: "2" }], + ], + ], + ); +}); diff --git a/tests/pool.ts b/tests/pool.ts index 09df7959..dd941b76 100644 --- a/tests/pool.ts +++ b/tests/pool.ts @@ -1,10 +1,7 @@ -import { - assertEquals, - assertThrowsAsync, -} from "../test_deps.ts"; +import { assertEquals, assertThrowsAsync } from "../test_deps.ts"; import { Pool } from "../pool.ts"; import { delay } from "../utils.ts"; -import { TEST_CONNECTION_PARAMS, DEFAULT_SETUP } from "./constants.ts"; +import { DEFAULT_SETUP, TEST_CONNECTION_PARAMS } from "./constants.ts"; async function testPool( t: (pool: Pool) => void | Promise, diff --git a/tests/utils.ts b/tests/utils.ts index 2b022f4b..d0fd81a1 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -1,6 +1,6 @@ const { test } = Deno; import { assertEquals } from "../test_deps.ts"; -import { parseDsn, DsnResult } from "../utils.ts"; +import { DsnResult, parseDsn } from "../utils.ts"; test("testParseDsn", function () { let c: DsnResult;