Cannot query on dates using Generated<Timestamp>
Closed this issue · 1 comments
When upgrading prisma-kysely
from 1.3.0 to 1.4.0 I noticed the Generated
type changed by using the one provided by kysely
This has the side effect that you now cannot query on fields that have this Generated<Timestamp>
type:
const example = await kysely
.selectFrom('Example')
.where('createdAt', '>=', start)
.where('createdAt', '<=', end)
.executeTakeFirst();
// TS error on .where('createdAt'):
// Argument of type 'Date | null' is not assignable to parameter of type 'OperandValueExpressionOrList<DB, "Example", "createdAt">'.
Above query worked fine, without TS errors, before upgrading.
This is a simplified version of how it currently generates the types:
import type { ColumnType, Generated, GeneratedAlways } from 'kysely';
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
export type Example = {
id: GeneratedAlways<number>;
name: string;
createdAt: Generated<Timestamp>;
updatedAt: Generated<Timestamp>;
};
The Timestamp
type is using ColumnType
. According to the Kysely maintainer, you should not use Generated
when also using ColumnType
: kysely-org/kysely#452 (comment)
So when I manually change the type to the following, it works without errors:
import type { ColumnType, Generated, GeneratedAlways } from 'kysely';
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
export type Example = {
id: GeneratedAlways<number>;
name: string;
createdAt: Timestamp; // Removed Generated type
updatedAt: Generated<Timestamp>;
};
But this has the side-effect that it now expects a createdAt
field to be present on inserts -_-
This is a simplified version of my Prisma schema:
model Example {
id Int @id @default(autoincrement()) @db.UnsignedInt
name String
createdAt DateTime @default(now())
updatedAt DateTime @default(dbgenerated("CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"))
}
So i'm not really sure on what the solution here is
This is how the generated types file looked like before and worked fine, both on selects and inserts:
import type { ColumnType } from 'kysely';
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
export type Example = {
id: Generated<number>;
name: string;
createdAt: Generated<Timestamp>;
updatedAt: Generated<Timestamp>;
};
So maybe this should be restored?
Oops, I thought I was cleaning things up but created a regression. Sorry about that. PR on the way.