Skip to content

feat: Add date array support #231

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

Merged
merged 1 commit into from
Jan 31, 2021
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
3 changes: 3 additions & 0 deletions decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
decodeBytea,
decodeByteaArray,
decodeDate,
decodeDateArray,
decodeDatetime,
decodeDatetimeArray,
decodeInt,
Expand Down Expand Up @@ -103,6 +104,8 @@ function decodeText(value: Uint8Array, typeOid: number): any {
return decodeByteaArray(strValue);
case Oid.date:
return decodeDate(strValue);
case Oid.date_array:
return decodeDateArray(strValue);
case Oid.int8:
return decodeBigint(strValue);
case Oid.int8_array:
Expand Down
3 changes: 2 additions & 1 deletion oid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export const Oid = {
timestamp: 1114,
// deno-lint-ignore camelcase
timestamp_array: 1115,
_date: 1182,
// deno-lint-ignore camelcase
date_array: 1182,
// deno-lint-ignore camelcase
time_array: 1183,
timestamptz: 1184,
Expand Down
22 changes: 13 additions & 9 deletions query/decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ function decodeByteaHex(byteaStr: string): Uint8Array {
return bytes;
}

export function decodeDate(dateStr: string): Date {
export function decodeDate(dateStr: string): Date | number {
// there are special `infinity` and `-infinity`
// cases representing out-of-range dates
if (dateStr === "infinity") {
return Number(Infinity);
} else if (dateStr === "-infinity") {
return Number(-Infinity);
}

const matches = DATE_RE.exec(dateStr);

if (!matches) {
Expand All @@ -100,20 +108,16 @@ export function decodeDate(dateStr: string): Date {
return date;
}

export function decodeDateArray(value: string) {
return parseArray(value, decodeDate);
}

export function decodeDatetime(dateStr: string): number | Date {
/**
* Postgres uses ISO 8601 style date output by default:
* 1997-12-17 07:37:16-08
*/

// there are special `infinity` and `-infinity`
// cases representing out-of-range dates
if (dateStr === "infinity") {
return Number(Infinity);
} else if (dateStr === "-infinity") {
return Number(-Infinity);
}

const matches = DATETIME_RE.exec(dateStr);

if (!matches) {
Expand Down
4 changes: 4 additions & 0 deletions test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export {
decode as decodeBase64,
encode as encodeBase64,
} from "https://deno.land/[email protected]/encoding/base64.ts";
export {
format as formatDate,
parse as parseDate,
} from "https://deno.land/[email protected]/datetime/mod.ts";
33 changes: 32 additions & 1 deletion tests/data_types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { assertEquals, decodeBase64, encodeBase64 } from "../test_deps.ts";
import {
assertEquals,
decodeBase64,
encodeBase64,
formatDate,
parseDate,
} from "../test_deps.ts";
import { Client } from "../mod.ts";
import TEST_CONNECTION_PARAMS from "./config.ts";
import { getTestClient } from "./helpers.ts";
Expand Down Expand Up @@ -674,3 +680,28 @@ testClient(async function tidArray() {

assertEquals(result.rows[0][0], [[4681n, 1869n], [0n, 17476n]]);
});

testClient(async function date() {
const date = "2020-01-01";

const result = await CLIENT.queryArray<[Timestamp, Timestamp]>(
"SELECT $1::DATE, 'Infinity'::Date",
date,
);

assertEquals(result.rows[0], [parseDate(date, "yyyy-MM-dd"), Infinity]);
});

testClient(async function dateArray() {
const dates = ["2020-01-01", formatDate(new Date(), "yyyy-MM-dd")];

const result = await CLIENT.queryArray<[Timestamp, Timestamp]>(
"SELECT ARRAY[$1::DATE, $2]",
...dates,
);

assertEquals(
result.rows[0][0],
dates.map((date) => parseDate(date, "yyyy-MM-dd")),
);
});