Skip to content

Commit

Permalink
Parse postgres datetime in UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
markusahlstrand committed Feb 12, 2021
1 parent 4ab8c19 commit 181e410
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knex-aurora-data-api-client",
"version": "1.5.0",
"version": "1.5.1",
"description": "Knex Aurora Data API Client",
"main": "index.js",
"files": [
Expand Down
6 changes: 2 additions & 4 deletions src/data-api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-param-reassign */
/* eslint-disable no-plusplus */
/* eslint-disable no-undef */

const dataApiClient = require('data-api-client');
const util = require('util');
Expand Down Expand Up @@ -129,12 +127,12 @@ function dataAPI(ClientRDSDataAPI, Client, dialect) {
const { records, columnMetadata } = obj.response;

// Iterate through the data
for (let i = 0; i < columnMetadata.length; i++) {
for (let i = 0; i < columnMetadata.length; i += 1) {
const { tableName } = columnMetadata[i];
const { label } = columnMetadata[i];

// Iterate through responses
for (let j = 0; j < records.length; j++) {
for (let j = 0; j < records.length; j += 1) {
if (!res[j]) res[j] = {};
if (!res[j][tableName]) res[j][tableName] = {};
res[j][tableName][label] = records[j][label];
Expand Down
8 changes: 7 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ function applyRecord(columnMetadata, record) {
switch (column.typeName) {
case 'timestamp':
case 'timestamptz':
parsedColumns[column.name] = new Date(record[column.name]);
// Postgres format 2001-01-01 00:00:00
const [, year, month, day, hour, minute, second] = record[column.name].match(
/^(\d{1,4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/,
);
parsedColumns[column.name] = new Date(
Date.UTC(year, month - 1, day, hour, minute, second),
);
break;
case 'json':
case 'jsonb':
Expand Down
3 changes: 2 additions & 1 deletion test/integration/common-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async function queryForATimestampField(knex) {
table.timestamp('date');
});

const date = new Date();
const date = new Date('2001-01-01');

await knex.table(tableName).insert({ date });

Expand All @@ -88,6 +88,7 @@ async function queryForATimestampField(knex) {
expect(rows.length).to.equal(1);

expect(Object.prototype.toString.call(rows[0].date)).to.equal('[object Date]');
expect(rows[0].date.toUTCString()).to.equal(date.toUTCString());
}

async function queryForASingleJSONField(knex) {
Expand Down

0 comments on commit 181e410

Please sign in to comment.