Question about ColumnTypes and dates.
I have a simple yet confusing problem. In postgres I have a table with a column of type
Kysely's
The problem is that I'm not sure how to define my database types to account for that. If I create the following type I will get type error if I use string in my
and, if I change the ColumnType to be a union of
So my question is, is there any way to deal with this problem? Here is a kyse.link as well:
https://kyse.link/2CJ6I
In a certain way, the
DATE (yyyy-mm-dd).Kysely's
PostgresDialect is using the node-postgres/pg driver, which transforms date columns as javascript Date objects when it returns rows, however, you can use strings and/or Date objects as query parameters, both are valid ways to query the database. This works both in node and when you write raw SQL:The problem is that I'm not sure how to define my database types to account for that. If I create the following type I will get type error if I use string in my
.where clauses:and, if I change the ColumnType to be a union of
Date | string as acceptable values for ColumnType -> SelectType, then the inferred result for birthday in all queries will also be the same union, but we obviously know node-postgres only returns JS Date objects:So my question is, is there any way to deal with this problem? Here is a kyse.link as well:
https://kyse.link/2CJ6I
In a certain way, the
SelectType in ColumnType is more like select type (how you can select) and also a return type (how the result is inferred).Solution
And yes, the "select type" is currently used as the return type AND the query type. We could add a fourth "query" type to
But then subqueries in
ColumnType which would be used in where statements (and other similar statements).But then subqueries in
where statements would break if the select type and the query type don't match.