`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
koskimas2y 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
Igal2y ago
@bonsaye Thanks for opening the issue in prisma-kysely, that could be of use to many users actually.
NazCodeland
NazCodelandOP2y 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
Igal2y 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
NazCodelandOP2y 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
Igal2y ago
No problem
NazCodeland
NazCodelandOP2y ago
first, does it mean that Selectable defines a table type that is only selectable and not editable?
Igal
Igal2y 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
NazCodelandOP2y 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
Igal2y 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
NazCodelandOP2y ago
ah so you can use that as a type?
Igal
Igal2y ago
yeah, if you're being explicit about return types
NazCodeland
NazCodelandOP2y 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
Igal2y 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
NazCodelandOP2y 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
NazCodeland
NazCodelandOP2y ago
NazCodeland
NazCodelandOP2y ago
I am using Prisma schema to define my schema so since I don't have a kysely file where I am defining my interfaces(schemas), I think I need to import the interface from node_modules/kysely-codegen/dist/db.d.ts and setup the Selectable, Insertable, Updatable in there or, I define them inside of my Kysely.ts file where I have the connection info/Kysely db instance that's a silly question, I'll try defining them inside of kysely.ts and if that works i'll use that otherwise try the kysely.d.ts thanks again! I'll close this
Igal
Igal2y ago
its probably safer to define these extras in your src folder instead and commit.
NazCodeland
NazCodelandOP2y ago
my kysely.ts file is inside of /src/database/kysely/kysely.ts whereas the kysely.d.ts file is located at the root /types/kysely.d.ts but I could move that /types folder inside of src so it get's committed (if that's what you mean by commit) to the repo
Igal
Igal2y ago
yeah
NazCodeland
NazCodelandOP2y ago
🙏
Want results from more Discord servers?
Add your server