8 Replies
More specifically, how do I make the type used by select(), I guess would be a better question.
And get the return type of said query.
I want to be able to use one function for all gets on a table, but without having to lose types when I only want certain fields, basically.
I don't really like this type of abstractions
But it can be done, you just need to make
Something
generic..TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
what do you dislike about it?
what does it get you? you effectively have to strongly type it to constrain the input table type (or selection type) because each is tied to the other
if you want to select a certain group of columns every time, define that elsewhere and pass it into select; if you want to have a certain where condition every time, define that condition elsewhere too
the actual drizzle query methods are the least onerous parts that you'd need to abstract out, IMO
I guess I'm saying this seems like a weird inbetween middle level of abstraction that is not useful. either abstract out the query inputs and use those, or abstract out the entire query itself to a reusable "get me X thing" function, but ending up in the middle is a bunch of extra work for not that much gain
I take it a bit further, I put all the methods that I want in a interface, say
DataSource
and then create a class that implements it. I define the type that I want in the end, before implementing the query.
I keep all database logic behind the interface, and the application can only interact with the database through that interface. I love that pattern because I can refactor my queries however I want and the application never notices, as long as I deliver what the interface has definedyeah this is definitely a valid pattern. I was just looking into the patter OP posted for a usecase where I would select users accounts with sensitive data and wanted to create something like
your approach would be to create
-
unsecureUser
- user
in the interface and the some wrapper like
user(id, unsecure)
is that correct?I'm not totally sure I understood. But if I wanted to get the user with sensitive data, I would create a different method for what I want