-
-
Notifications
You must be signed in to change notification settings - Fork 722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE]: Better types #3673
Comments
After experimenting with drizzle yesterday, came here to post the exact same issue. 💯 I think it is actually possible (will post an example when I get to my computer, but it's unwieldy and hard to find). |
I think you can use something like:
This is pretty gross, but so far the only way I've found to achieve what you're asking. This should definitely be improved upon. |
@CaptainStiggz it is gross, hope the team take a look at this |
Hello. I am also doing something similar, and I found this trick to work quite well: import * as d from "drizzle-orm/pg-core"
const id = d.text().primaryKey()
export type PostgresTableWithId = ReturnType<typeof d.pgTable<string, { id: typeof id }>> And then on the repository class file: import type { PostgresTableWithId } from "..."
export class PostgresDrizzleRepository<Table extends PostgresTableWithId> {
/* ... */
} HOWEVER this has been working up until const insertResult = await this.client.db
.insert(this.table)
.values(item) Of course, I understand that drizzle is still in |
Feature hasn't been suggested before.
Describe the enhancement you want to request
Issue Description
I'm trying to implement a Repository pattern in my application, but I'm running into issues with typing, especially when using the
AnyPgTable
type in Drizzle ORM.The main problem is that
AnyPgTable
does not allow me to enforce certain field types for the tables. For instance, I want to ensure that a table used with a class has a field likeid
of typestring
andcreatedAt
of typetimestamp
. However, sinceAnyPgTable
cannot be extended, I have to work around it by using a type intersection likeAnyPgTable & { id: AnyPgColumn, updatedAt: AnyPgColumn }
.While this seems like a solution, it causes errors in practice. For example, if I try to update a record with a new field, such as
deletedAt
, I get an error because the field isn't recognized by the type system. This issue also extends to usingInferSelectModel
when paired withInferSelectModel<TTable> & { id: string }
, as it doesn't work properly due to type mismatches.Goal
I am creating a series of classes to help organize the app and would like to leverage some predefined functions that my classes can inherit by extending a base class. Here is an example of how I want the classes to work:
Example Code: Repository Class
Problem
In the
softRemove
method of theRepo
class, the code below doesn't recognize thatdeletedAt
is part of theCustomAnyPgTable
type, and as a result, TypeScript throws an error.Even though
deletedAt
is defined in theCustomAnyPgTable
type, TypeScript doesn't infer it correctly in this case, causing issues with the type system.Example Usage of the Repository Class
Summary of Issues:
AnyPgTable
: Can't enforce specific fields (likeid
,createdAt
) for tables usingAnyPgTable
.AnyPgTable & { id: AnyPgColumn, deletedAt: AnyPgColumn }
seems like a solution, it leads to type errors.deletedAt
field not recognized: In thesoftRemove
method, TypeScript doesn't recognizedeletedAt
as part of the table schema, causing errors during database updates.The text was updated successfully, but these errors were encountered: