© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
27 replies
Suji

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 };
  }
// 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>,
    },
  ) {
  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 🙂
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Help with this relational query?
Drizzle TeamDTDrizzle Team / help
3y ago
Nullable relational query?
Drizzle TeamDTDrizzle Team / help
3y ago
Relational query problem
Drizzle TeamDTDrizzle Team / help
3y ago
Generate relational select types
Drizzle TeamDTDrizzle Team / help
2y ago