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
// Types from https://github.com/drizzle-team/drizzle-orm/issues/695
type Schema = typeof primarySchemas;
type TSchema = ExtractTablesWithRelations<Schema>;

export type IncludeRelation<TableName extends keyof TSchema> = DBQueryConfig<
  'one' | 'many',
  boolean,
  TSchema,
  TSchema[TableName]
>['with'];

export type InferResultType<
  TableName extends keyof TSchema,
  With extends IncludeRelation<TableName> | undefined = undefined,
> = BuildQueryResult<
  TSchema,
  TSchema[TableName],
  {
    with: With;
  }
>;

 async paginatedQuery<
    U extends keyof drizzleOrm.ExtractTablesWithRelations<typeof schema>,
    TableRelations extends IncludeRelation<U>,
  >(
    table: BaseTable,
    {
      with: queryWith,
    }: {
      with: TableRelations;
    },
  ) {

    const tableName = this.getTableName(table);
    const data: Array<InferResultType<U, typeof queryWith>> =
      await this.db.query[tableName].findMany({
        with: queryWith 
      });

    return { data, meta };
  }

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
  async paginatedQuery<
    U extends keyof drizzleOrm.ExtractTablesWithRelations<typeof schema>,
    TableRelations extends IncludeRelation<U>, <-- Delete this and directly assign the type to with
  >(
    table: BaseTable,
    {
      with: queryWith,
    }: {
      with: IncludeRelation<U>,
    },
  ) {

Above change causes to miss the relational types in the return type 😦

Any TS aficionados willing to help 🙂
Was this page helpful?