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

Timestamp columns and Date values #34

Open
lagnat opened this issue Nov 21, 2023 · 4 comments
Open

Timestamp columns and Date values #34

lagnat opened this issue Nov 21, 2023 · 4 comments

Comments

@lagnat
Copy link

lagnat commented Nov 21, 2023

We need this fix: a2c26d4
Edit: The fix is not correct for Postgres timestamp columns. Implied UTC needs to be added.

@arcdev1
Copy link

arcdev1 commented Nov 24, 2023

I've resorted to doing this, for now.

export function pgDate(timestamptz: string) {
  const tszRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}$/;
  const patched = tszRegex.test(timestamptz) ? `${timestamptz}Z` : timestamptz;
  return new Date(patched).toISOString();
}

pgDate("2023-11-24 01:35:27.658") // "2023-11-24T01:35:27.658Z"

@lagnat
Copy link
Author

lagnat commented Nov 24, 2023

Honestly, after looking at pg-types I feel it would be wise to integrate it. It does a pile of conversions which I'd bet most people would expect to have. There are some details to work out, namely how to map rds-data types to Postgres oids and what to do about mysql. Would it possible to use pg-types for both... maybe?

@lagnat
Copy link
Author

lagnat commented Nov 24, 2023

Here's my shameful hack to get around the issue:

import { getTypeParser } from 'pg-types';

class TimestampRDSDataHax extends RDSData {
    readonly dateNoTzParser = getTypeParser(1114);

    public override async executeStatement(...args: [any]): Promise<ExecuteStatementCommandOutput> {
        const r = await super.executeStatement(...args);

        if (r.columnMetadata) {
            for (const row of r.records || []) {
                for (let i = 0; i < row.length; i++) {
                    const md = r.columnMetadata[i];
                    const field = row[i];
                    const value = field.stringValue;
                    if (value?.length && !field.isNull) {
                        switch (md.typeName) {
                            case 'timestamp':
                                // Force it into the stringValue.  We know that kysely-data-api doesn't care that it's not actually a string.
                                field.stringValue = this.dateNoTzParser(value) as string;
                                break;

                            default:
                                continue;
                        }
                    }
                }
            }
        }

        return r;
    }
}

        this.db = new Kysely<Database>({
            dialect: new DataApiDialect({
                mode: 'postgres',
                driver: {
                    stuff....
                    client: new TimestampRDSDataHax(),
                },
            }),
        });

@lagnat lagnat changed the title When are you planning to make a new npm release? Timestamp columns and Date values Nov 24, 2023
@juanmguzmann
Copy link

juanmguzmann commented Sep 26, 2024

About this they are also converting type "date" values into JS dates, so we are saving values with timestamps into date type columns. In postgres date columns should be in this format: 'yyyy-mm-dd'.

So this PR solves the issue partially, only for timestamps. But not for dates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants