Help with types relational query function
I've created a function that i use to paginate data with relations, removed all the paginating stuff for the post
right now this works, but the problem is i have to define the relations twice, once as a param for the generic type and again for the with parameter
This is what ive done, im not sure why this doesn't yield the same results
Above change causes to miss the relational types in the return type 😦
Any TS aficionados willing to help 🙂
17 Replies
The way you have it set up, typescript does not see the relations between U and BaseTable
As far as typescript is concerned, you could be quering tableA and using the with object for tableB
At least in a quick look, that's what it seems to me
table:BaseTable isn't used in the findMany query, 🤔 I'm using that to get the tableName
Let me update code snippet
Can you put your code snippet in the typescript playground? some table definitions and relations and lets play around with it
sure gimme a few mins
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.
Cool, let me take a look
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.
You were almost there
This is cool helper, totally type safe
nice, thanks a lot now i have bunch of cleaning upto do 😁
you can delete this line:
const tableName = getTableName(schema[table])
hahah @Angelelz just following you around this discord - I am trying to implement your playground and I'm getting an error with my with condition. It's complaining about two different types with same name It looks like on the ExtractTableWithRelations type but not positive if maybes its something in my schema
If you put it together in a playground I can take a look at it
okay let me throw something together
thank you!
okay i think it has something to do with importing my schema definition from a barrel file
when I import schema like so
import * as schema from '@server/db/schema/addresses';
i get that error however if i do this it works so im curious if it has something to do with barrel files and import * as
Yeah, if it's pulling other stuff in that import, maybe
yeah i just refactored and made sure i only have pgTables and relations exporting out of my schema files - can not get the barrel file to work and i really don't want to individually import my tables and relations from 15 different files just to build a
const schema = {locations, locationsRelations.......
object for all my exports
just to go even further incase my schema definitions were not setup correct - i created a testDirectory with a barrel file exporting only the schemas you defined in your playground. Importing with * as
breaks but importing each table individually and building the schema object seems to work - very confused
okay looks like if i spread the * as
into another object this might fix the problem but curious what the difference is - i know the * as
is a namespace import but i thought it was identical to an object
hahah sorry just rambling in here incase someone stumbles upon this later - so the spread syntax seemed to fix the issue however that required the db being defined in the same file as the paginatedQuery function... if i import the db i get the with
error again
okay - so i had to import both the db and the spread schema object from the db file instead of building the same schema spread object in the consuming paginatedQuery file
so doing this
db.ts
and making sure I import schema from db and not to just rebuild the object by importing and spreading again
it never ends.... went to clean up the function and build into a class or helper function that can be imported but now I am getting typescript error about the inferred type exceeding max length for ts compiler
has anyone found out how to do this yet? We hare having issues where we are using joins with one and many, but it has no types associated with it, so we are getting any as a result.