Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json array and jsonb array type #179

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
7 changes: 7 additions & 0 deletions decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
4 changes: 2 additions & 2 deletions oid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions tests/data_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" }],
],
],
);
});
7 changes: 2 additions & 5 deletions tests/pool.ts
Original file line number Diff line number Diff line change
@@ -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<void>,
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down