Skip to content

Commit c71cc2c

Browse files
authored
Merge pull request #179 from Soremwar/add_types
Add json array and jsonb array type
2 parents a04cd67 + 2e41990 commit c71cc2c

File tree

7 files changed

+48
-13
lines changed

7 files changed

+48
-13
lines changed

connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { BufReader, BufWriter } from "./deps.ts";
3030
import { PacketWriter } from "./packet_writer.ts";
3131
import { hashMd5Password, readUInt32BE } from "./utils.ts";
3232
import { PacketReader } from "./packet_reader.ts";
33-
import { QueryConfig, QueryResult, Query } from "./query.ts";
33+
import { Query, QueryConfig, QueryResult } from "./query.ts";
3434
import { parseError } from "./error.ts";
3535
import type { ConnectionParams } from "./connection_params.ts";
3636
import { DeferredStack } from "./deferred.ts";

decode.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ function decodeIntArray(value: string): any {
195195
return parseArray(value, decodeBaseTenInt);
196196
}
197197

198+
function decodeJsonArray(value: string): unknown[] {
199+
return parseArray(value, JSON.parse);
200+
}
201+
198202
// deno-lint-ignore no-explicit-any
199203
function decodeText(value: Uint8Array, typeOid: number): any {
200204
const strValue = decoder.decode(value);
@@ -253,6 +257,9 @@ function decodeText(value: Uint8Array, typeOid: number): any {
253257
case Oid.json:
254258
case Oid.jsonb:
255259
return JSON.parse(strValue);
260+
case Oid.json_array:
261+
case Oid.jsonb_array:
262+
return decodeJsonArray(strValue);
256263
case Oid.bytea:
257264
return decodeBytea(strValue);
258265
default:

deps.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
export {
2-
BufReader,
3-
BufWriter,
4-
} from "https://deno.land/[email protected]/io/bufio.ts";
1+
export { BufReader, BufWriter } from "https://deno.land/[email protected]/io/bufio.ts";
52
export { copyBytes } from "https://deno.land/[email protected]/bytes/mod.ts";
63
export { deferred } from "https://deno.land/[email protected]/async/deferred.ts";
74
export type { Deferred } from "https://deno.land/[email protected]/async/deferred.ts";

oid.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const Oid = {
2323
xml: 142,
2424
_xml: 143,
2525
pg_node_tree: 194,
26-
_json: 199,
26+
json_array: 199,
2727
smgr: 210,
2828
index_am_handler: 325,
2929
point: 600,
@@ -146,7 +146,7 @@ export const Oid = {
146146
regdictionary: 3769,
147147
_regdictionary: 3770,
148148
jsonb: 3802,
149-
_jsonb: 3807,
149+
jsonb_array: 3807,
150150
anyrange: 3831,
151151
event_trigger: 3838,
152152
int4range: 3904,

tests/data_types.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,37 @@ testClient(async function bpcharNestedArray() {
267267
);
268268
assertEquals(result.rows[0], [[["AB1234"], ["4321BA"]]]);
269269
});
270+
271+
testClient(async function jsonArray() {
272+
const json_array = await CLIENT.query(
273+
`SELECT ARRAY_AGG(A) FROM (
274+
SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A
275+
UNION ALL
276+
SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A
277+
) A`,
278+
);
279+
280+
assertEquals(json_array.rows[0][0], [{ X: "1" }, { Y: "2" }]);
281+
282+
const json_array_nested = await CLIENT.query(
283+
`SELECT ARRAY[ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)], ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)]] FROM (
284+
SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A
285+
UNION ALL
286+
SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A
287+
) A`,
288+
);
289+
290+
assertEquals(
291+
json_array_nested.rows[0][0],
292+
[
293+
[
294+
[{ X: "1" }, { Y: "2" }],
295+
[{ X: "1" }, { Y: "2" }],
296+
],
297+
[
298+
[{ X: "1" }, { Y: "2" }],
299+
[{ X: "1" }, { Y: "2" }],
300+
],
301+
],
302+
);
303+
});

tests/pool.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import {
2-
assertEquals,
3-
assertThrowsAsync,
4-
} from "../test_deps.ts";
1+
import { assertEquals, assertThrowsAsync } from "../test_deps.ts";
52
import { Pool } from "../pool.ts";
63
import { delay } from "../utils.ts";
7-
import { TEST_CONNECTION_PARAMS, DEFAULT_SETUP } from "./constants.ts";
4+
import { DEFAULT_SETUP, TEST_CONNECTION_PARAMS } from "./constants.ts";
85

96
async function testPool(
107
t: (pool: Pool) => void | Promise<void>,

tests/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { test } = Deno;
22
import { assertEquals } from "../test_deps.ts";
3-
import { parseDsn, DsnResult } from "../utils.ts";
3+
import { DsnResult, parseDsn } from "../utils.ts";
44

55
test("testParseDsn", function () {
66
let c: DsnResult;

0 commit comments

Comments
 (0)