Skip to content

Commit

Permalink
chore: foreignTable -> referencedTable
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Nov 2, 2023
1 parent 18f580c commit 7eabd65
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
16 changes: 12 additions & 4 deletions src/PostgrestFilterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,19 @@ export default class PostgrestFilterBuilder<
* It's currently not possible to do an `.or()` filter across multiple tables.
*
* @param filters - The filters to use, following PostgREST syntax
* @param foreignTable - Set this to filter on foreign tables instead of the
* current table
* @param options - Named parameters
* @param options.referencedTable - Set this to filter on referenced tables
* instead of the parent table
* @param options.foreignTable - Deprecated, use `referencedTable` instead
*/
or(filters: string, { foreignTable }: { foreignTable?: string } = {}): this {
const key = foreignTable ? `${foreignTable}.or` : 'or'
or(
filters: string,
{
foreignTable,
referencedTable = foreignTable,
}: { foreignTable?: string; referencedTable?: string } = {}
): this {
const key = referencedTable ? `${referencedTable}.or` : 'or'
this.url.searchParams.append(key, `(${filters})`)
return this
}
Expand Down
70 changes: 55 additions & 15 deletions src/PostgrestTransformBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ export default class PostgrestTransformBuilder<
return this as unknown as PostgrestTransformBuilder<Schema, Row, NewResultOne[], Relationships>
}

order<ColumnName extends string & keyof Row>(
column: ColumnName,
options?: { ascending?: boolean; nullsFirst?: boolean; referencedTable?: undefined }
): this
order(
column: string,
options?: { ascending?: boolean; nullsFirst?: boolean; referencedTable?: string }
): this
/**
* @deprecated Use `options.referencedTable` instead of `options.foreignTable`
*/
order<ColumnName extends string & keyof Row>(
column: ColumnName,
options?: { ascending?: boolean; nullsFirst?: boolean; foreignTable?: undefined }
): this
/**
* @deprecated Use `options.referencedTable` instead of `options.foreignTable`
*/
order(
column: string,
options?: { ascending?: boolean; nullsFirst?: boolean; foreignTable?: string }
Expand All @@ -55,26 +69,34 @@ export default class PostgrestTransformBuilder<
*
* You can call this method multiple times to order by multiple columns.
*
* You can order foreign tables, but it doesn't affect the ordering of the
* current table.
* You can order referenced tables, but it only affects the ordering of the
* parent table if you use `!inner` in the query.
*
* @param column - The column to order by
* @param options - Named parameters
* @param options.ascending - If `true`, the result will be in ascending order
* @param options.nullsFirst - If `true`, `null`s appear first. If `false`,
* `null`s appear last.
* @param options.foreignTable - Set this to order a foreign table by foreign
* columns
* @param options.referencedTable - Set this to order a referenced table by
* its columns
* @param options.foreignTable - Deprecated, use `options.referencedTable`
* instead
*/
order(
column: string,
{
ascending = true,
nullsFirst,
foreignTable,
}: { ascending?: boolean; nullsFirst?: boolean; foreignTable?: string } = {}
referencedTable = foreignTable,
}: {
ascending?: boolean
nullsFirst?: boolean
foreignTable?: string
referencedTable?: string
} = {}
): this {
const key = foreignTable ? `${foreignTable}.order` : 'order'
const key = referencedTable ? `${referencedTable}.order` : 'order'
const existingOrder = this.url.searchParams.get(key)

this.url.searchParams.set(
Expand All @@ -91,11 +113,19 @@ export default class PostgrestTransformBuilder<
*
* @param count - The maximum number of rows to return
* @param options - Named parameters
* @param options.foreignTable - Set this to limit rows of foreign tables
* instead of the current table
* @param options.referencedTable - Set this to limit rows of referenced
* tables instead of the parent table
* @param options.foreignTable - Deprecated, use `options.referencedTable`
* instead
*/
limit(count: number, { foreignTable }: { foreignTable?: string } = {}): this {
const key = typeof foreignTable === 'undefined' ? 'limit' : `${foreignTable}.limit`
limit(
count: number,
{
foreignTable,
referencedTable = foreignTable,
}: { foreignTable?: string; referencedTable?: string } = {}
): this {
const key = typeof referencedTable === 'undefined' ? 'limit' : `${referencedTable}.limit`
this.url.searchParams.set(key, `${count}`)
return this
}
Expand All @@ -110,12 +140,22 @@ export default class PostgrestTransformBuilder<
* @param from - The starting index from which to limit the result
* @param to - The last index to which to limit the result
* @param options - Named parameters
* @param options.foreignTable - Set this to limit rows of foreign tables
* instead of the current table
* @param options.referencedTable - Set this to limit rows of referenced
* tables instead of the parent table
* @param options.foreignTable - Deprecated, use `options.referencedTable`
* instead
*/
range(from: number, to: number, { foreignTable }: { foreignTable?: string } = {}): this {
const keyOffset = typeof foreignTable === 'undefined' ? 'offset' : `${foreignTable}.offset`
const keyLimit = typeof foreignTable === 'undefined' ? 'limit' : `${foreignTable}.limit`
range(
from: number,
to: number,
{
foreignTable,
referencedTable = foreignTable,
}: { foreignTable?: string; referencedTable?: string } = {}
): this {
const keyOffset =
typeof referencedTable === 'undefined' ? 'offset' : `${referencedTable}.offset`
const keyLimit = typeof referencedTable === 'undefined' ? 'limit' : `${referencedTable}.limit`
this.url.searchParams.set(keyOffset, `${from}`)
// Range is inclusive, so add 1
this.url.searchParams.set(keyLimit, `${to - from + 1}`)
Expand Down
10 changes: 0 additions & 10 deletions src/select-query-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ type Letter = Alphabet | Digit | '_'

type Json = string | number | boolean | null | { [key: string]: Json } | Json[]

// /**
// * Parsed node types.
// * Currently only `*` and all other fields.
// */
// type ParsedNode =
// | { star: true }
// | { name: string; original: string }
// | { name: string; foreignTable: true }
// | { name: string; type: T };

/**
* Parser errors.
*/
Expand Down

0 comments on commit 7eabd65

Please sign in to comment.