Typing columns based on Table

I'm trying to make a generic paginateQuery function that would let me pass in the table and then an array of only columns from that table would be accepted. Is there any way to do this with the Drizzle types? I feel like I have gotten close a few times but nothing fully gets there. Thanks!

const getFilterSql = <TColumn extends AnyPgColumn>(
  filter: string,
  filterableColumns: TColumn[],
) => {
  if (!filter || filter.trim() === '') return or();

  return or(...filterableColumns.map((c) => ilike(c, `%${filter}%`)));
};


export const paginatedQuery = <TTable extends AnyPgTable>(
  context: PgDatabase<NodePgQueryResultHKT>,
  table: TTable,
  paginationDto: PaginationDto,
  filterableColumns: (/* Not too sure what to put here */)[],
  defaultWhere?: SQL,
) => {
  return context
    .select()
    .from(table)
    .where(
      and(defaultWhere, getFilterSql(paginationDto.filter, filterableColumns)), // TypeScript gives an error here
    )
    .offset(paginationDto.page * paginationDto.pageSize)
    .limit(paginationDto.pageSize);
};
Was this page helpful?