`Selectable`, `Insertable` and `Updateable` wrappers

Hey everyone, I am using this package: https://github.com/valtyr/prisma-kysely and I noticed that the output file does not include
export type Person = Selectable<PersonTable>
export type NewPerson = Insertable<PersonTable>
export type EditedPerson = Updateable<PersonTable>
export type Person = Selectable<PersonTable>
export type NewPerson = Insertable<PersonTable>
export type EditedPerson = Updateable<PersonTable>
for the tables that I have in my schema, I don't know if that's not required because of my setup or if that is still required, I opened an issue in the repo but figured I ask here too
21 Replies
koskimas
koskimas15mo ago
Those types are not required in any sense. They can be handy in function input types and such. You can create them yourself if the type generator doesn't do that. Nothing in Kysely "reads" the typing file in any way. All kysely knows about the types is what you pass the Kysely class as type arguments.
Igal
Igal15mo ago
@bonsaye Thanks for opening the issue in prisma-kysely, that could be of use to many users actually.
NazCodeland
NazCodeland15mo ago
I've switched from using prisma-kysely package to using the Kysely-codegen package, but I still use Prisma models to push my schema to the database (mainly because I want to generate Zod schemas from the Prisma models. However, is it not possible for me to use
// You should not use the table schema interfaces directly. Instead, you should
// use the `Selectable`, `Insertable` and `Updateable` wrappers. These wrappers
// make sure that the correct types are used in each operation.
export type Person = Selectable<PersonTable>
export type NewPerson = Insertable<PersonTable>
export type EditedPerson = Updateable<PersonTable>
// You should not use the table schema interfaces directly. Instead, you should
// use the `Selectable`, `Insertable` and `Updateable` wrappers. These wrappers
// make sure that the correct types are used in each operation.
export type Person = Selectable<PersonTable>
export type NewPerson = Insertable<PersonTable>
export type EditedPerson = Updateable<PersonTable>
now that I am using Kysely-codegen, if I am not using Kysely interfaces to define my schema? Feel like I am misunderstanding something
Igal
Igal15mo ago
Its a common mistake we've seen in issues and discord, that users would use Database['person'] as the type returned from a select query. so this part tells you to use the helpers around Database['person'] to extract the proper type based on use case
NazCodeland
NazCodeland15mo ago
damn man, sorry for being a nuisance just wanna understand. I really don't get this part. for example,
export interface PetTable {
id: Generated<number>
name: string
owner_id: number
species: 'dog' | 'cat'
}

export type Pet = Selectable<PetTable>
export type NewPet = Insertable<PetTable>
export type EditedPet = Updateable<PetTable>
export interface PetTable {
id: Generated<number>
name: string
owner_id: number
species: 'dog' | 'cat'
}

export type Pet = Selectable<PetTable>
export type NewPet = Insertable<PetTable>
export type EditedPet = Updateable<PetTable>
Igal
Igal15mo ago
No problem
NazCodeland
NazCodeland15mo ago
first, does it mean that Selectable defines a table type that is only selectable and not editable?
Igal
Igal15mo ago
people would use PetTable as the return type of a method that does some select query. instead of Selectable<PetTable> Generated<T> is ColumnType<SelectType = T, InsertType | null, UpdateType | null> iirc
NazCodeland
NazCodeland15mo ago
are you able to show me what you mean by `people would use PetTable as return type based on this example
ts
const persons = await db
.selectFrom('person')
.select('id')
.where('first_name', '=', 'Arnold')
.execute()
ts
const persons = await db
.selectFrom('person')
.select('id')
.where('first_name', '=', 'Arnold')
.execute()
or
export async function findPersonById(id: Person['id']) {
return await db.selectFrom('person')
.where('id', '=', id)
.selectAll()
.executeTakeFirst()
}
export async function findPersonById(id: Person['id']) {
return await db.selectFrom('person')
.where('id', '=', id)
.selectAll()
.executeTakeFirst()
}
sorry but if you want you can use the example in the docs associated with that step. If that's easier
Igal
Igal15mo ago
Selectable for example, takes PetTable and turns it from
{
id: Generated<number>
name: string
owner_id: number
species: 'dog' | 'cat'
}
{
id: Generated<number>
name: string
owner_id: number
species: 'dog' | 'cat'
}
to
{
id: number
name: string
owner_id: number
species: 'dog' | 'cat'
}
{
id: number
name: string
owner_id: number
species: 'dog' | 'cat'
}
NazCodeland
NazCodeland15mo ago
ah so you can use that as a type?
Igal
Igal15mo ago
yeah, if you're being explicit about return types
NazCodeland
NazCodeland15mo ago
what is the difference betwen the Selectable ( I see what it does ) what about Insertable/Updateable - also are these the only 3 of their kind?
Igal
Igal15mo ago
ColumnType has 3 generic slots, one for select type, one for insert type and one for update type Selectable extracts the select type Insertable extracts the insert type Updateable extracts the update type
NazCodeland
NazCodeland15mo ago
beautiful, now I get it and which one to use when making Kysely queries thank you very much Igal before I let you go xD wanna show you one thing
Want results from more Discord servers?
Add your server