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

Are we able to use string instead of new Date() ? #123

Open
tombohub opened this issue Dec 13, 2023 · 4 comments
Open

Are we able to use string instead of new Date() ? #123

tombohub opened this issue Dec 13, 2023 · 4 comments
Labels
documentation Improvements or additions to documentation duplicate This issue or pull request already exists

Comments

@tombohub
Copy link

tombohub commented Dec 13, 2023

postgres generated code for table is:

export interface OvernightDailyOvernightPerformance {
  ...
  date: Timestamp;
  ...
}

I want to filter by date:

const rows = await db
    .selectFrom("overnight.daily_overnight_performance")
    .where("date", "=", "2023-06-02")
    .execute()

but it's an error:
image

and cannot run the script.

If I change Timestamp type from:

export type Timestamp = ColumnType<Date, Date | string, Date | string>;

to:

export type Timestamp = ColumnType<Date | string, Date | string, Date | string>;

it still shows the error:
image

but at least I can run the script.

Can you make it so it generates code where date string is acceptable for selection?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@SangJunBak
Copy link

SangJunBak commented Dec 21, 2023

I too have this problem! Especially since Postgres accepts microsecond precision which JavasScript Date objects do not support.

@nick-cheatwood7
Copy link

This is not a perfect fix, but you can sort of trick Typescript into allowing a Date value, at least for timestamp columns in MySQL:

const isoDateString = new Date().toISOString()
// 👇 where "date_column" is a `timestamp` column
await db.insertInto("foo").values({ date_column: isoDateString as unknown as Date  }).execute()

I had a project where I needed to insert a timestamp value as a string and this worked.

@RobinBlomberg RobinBlomberg added bug Something isn't working question Further information is requested labels Mar 7, 2024
@RobinBlomberg
Copy link
Owner

This sounds like a duplicate of #121. Someone posted a solution there:

import { Selectable } from 'kysely'

const results: Selectable<Thing> = await db.selectFrom('thing').selectAll().execute()

Please check if that solves the issue. If so, I will add the solution to the README.

@RobinBlomberg RobinBlomberg added documentation Improvements or additions to documentation duplicate This issue or pull request already exists and removed bug Something isn't working question Further information is requested labels Mar 7, 2024
@koskimas
Copy link
Contributor

koskimas commented Mar 24, 2024

@RobinBlomberg This is a different issue. The issue is that where uses the Selectable type, which is Date in Timestamp.

@tombohub

it still shows the error:

No it doesn't. Restart vscode or reboot its typescript server. If you're able to compile that typescript, then surely the typescript running inside vscode also accepts the code.

There's probably nothing kysely-codegen should do here. If you change the selectable type, then the result type is also Date | string. What you (the user) can do is create a helper function like this:

function toDate(date: string) {
  return sql<Date>`${date}`
}

const rows = await db
    .selectFrom("overnight.daily_overnight_performance")
    .where("date", "=", toDate("2023-06-02"))
    .execute()

I'd say this is cleaner than a cast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

5 participants