Skip to content

Add ability to pass typed array to select() method #265

Open
@prescience-data

Description

@prescience-data

Feature request

Currently, the select() method of the QueryBuilder accepts a string of comma-separated values.

https://github.com/supabase/postgrest-js/blob/v0.37.2/src/lib/PostgrestQueryBuilder.ts#L33

This does not allow for any type-checking and feels a bit "raw" (personal opinion only).

Describe the solution you'd like

Allow a typed array to be passed as the columns property.

export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
// ... 
  select (
    columns: (keyof T )[] | string = '*',
    {
      head = false,
      count = null,
    }: {
      head?: boolean
      count?: null | 'exact' | 'planned' | 'estimated'
    } = {}
  ): PostgrestFilterBuilder<T> {
    this.method = 'GET'
    // Remove whitespaces except when quoted
    let quoted = false
    
    const cleanedColumns = (Array.isArray(columns) ? columns.join(',') : columns)
      .split('')
      .map((c) => {
        if (/\s/.test(c) && !quoted) {
          return ''
        }
        if (c === '"') {
          quoted = !quoted
        }
        return c
      })
      .join('')
    // ...
  }
}

Describe alternatives you've considered

This is more a "nice to have" as it works fine as is, and this approach can be implemented manually.

However, given the minimal overhead and typing upside, I thought it might be worthwhile suggesting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions