postgrest-js icon indicating copy to clipboard operation
postgrest-js copied to clipboard

Add ability to pass typed array to `select()` method

Open prescience-data opened this issue 3 years ago • 0 comments

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.

prescience-data avatar Apr 10 '22 09:04 prescience-data