Declaring types under 'kysely-codegen' module

Hey everyone, because I'm using Prisma to push schemas to my database (it's because my Zod Schemas) also getting generated from my Prisma schemas -- it's why I'm still using it incase you're wondering why I'm in this situation: I have a ./types/kysely-codegen.d.ts file which contains
import { Insertable, Selectable, Updateable } from 'kysely';
import { User } from 'kysely-codegen';

declare module 'kysely-codegen' {
export type BaseUser = Selectable<User>;
export type NewUser = Insertable<User>;
export type EditedUser = Updateable<User>;
...
}
import { Insertable, Selectable, Updateable } from 'kysely';
import { User } from 'kysely-codegen';

declare module 'kysely-codegen' {
export type BaseUser = Selectable<User>;
export type NewUser = Insertable<User>;
export type EditedUser = Updateable<User>;
...
}
in another file I have
import type { DB, BaseUser } from 'kysely-codegen';
import type { DB, BaseUser } from 'kysely-codegen';
BaseUser gives me the error that
Module '"kysely-codegen"' has no exported member 'BaseUser'.ts(2305)
Module '"kysely-codegen"' has no exported member 'BaseUser'.ts(2305)
5 Replies
NazCodeland
NazCodelandOP2y ago
Not exactly related to how Kysely functions but if anyone can help it would be appreciated, this is what my tsconfig.json looks like
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"typeRoots": ["./types", "node_modules/@types"]
}
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"typeRoots": ["./types", "node_modules/@types"]
}
I know the ./types/kysely-codegen.d.ts file works because if I change the name of BaseUser to just User it says Duplicate identifier 'User'.ts(2300) so it must be a typescript thing that it's not aware that I am doing a module declare for it to know of the new types a new day and somehow it works xD hmm, is there anyway to configure kysely-codegen to retain the information that a field is optional or not? For example, when I push this Prisma schema to the database
model User {
id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
updatedAt DateTime? @db.Timestamp()
}
model User {
id String @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
updatedAt DateTime? @db.Timestamp()
}
kysely-codegen generates this from the above
export type User = {
id: Generated<string>;
updatedAt: Timestamp | null;
};
export type User = {
id: Generated<string>;
updatedAt: Timestamp | null;
};
the Selectable of that is
type BaseUser = {
id: string;
updatedAt: Date | null;
}
type BaseUser = {
id: string;
updatedAt: Date | null;
}
I want it to know that updatedAt is optional updatedAt: Date | null?; nevermind, found a easy solution
koskimas
koskimas2y ago
Nullable means optional. Kysely automatically makes nullable fields optional in inserts and updates You can use Insertable and Updateable to get those types. In SQL, when something is "missing" in selects, you get a null
NazCodeland
NazCodelandOP2y ago
Oh WOW! I see what you mean! that's amazing
NazCodeland
NazCodelandOP2y ago
NazCodeland
NazCodelandOP2y ago
❤️ Kysely, thank you very much for that insight koskimas
Want results from more Discord servers?
Add your server