How to type a Table
I want my data access layer to depend on my entities layer. I want to define a ts type for an entity, say a Student type, and then implement a drizzle table that's based off that type. Something like
const studentTable = sqliteTable<Student>(...)
would be perfect the syntax imo. I would expect this to throw errors if it was missing a field or incorrectly typed field.
Is there a way to do this atm? Maybe a hack with drizzle-zod
?5 Replies
I think you can use
type DbLayerType = typeof table.$inferSelect
and inferInsert if that's needed
so you will pass mapped object from a database between entities and db layers
which are always selected values = typeof table.$inferSelect
and sometimes inferInsert, if it's objects passed to add some data to dbDrizzle ORM - Goodies
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Thanks for the help but Im not sure this is what Im looking for.
I typed my data access layer function
getAssociationByIdFromDb
as the Promise<type of associations.$inferSelect | undefined>
but it doesn't solve my issue as seen below.See how this function in my
@/application
layer doesnt mind returning the incorrect type? It correctly infers the type of my query fn in my data access layer as having a content: string
field, but it thinks its returning AsyncTaskResult<Association>
which doesn't have the content: string
field.
This is why I want an error to occur in my table definition as I modify some type in my @/entities
layer


Im pretty sure this is a typescript skill issue but I think being able to define the type of a table as Im writing it would help.
I would settle for a hack using
associations.$inferSelect
and Associations
where it checks if theyre equal somehow