sassoftware / postgrest-client

General purpose type-safe TypeScript client for PostgREST. Supports select, insert, update, upsert, delete queries with composite joins, resource embedding, full JSON column data handling and typing.
Apache License 2.0
7 stars 0 forks source link

feat: top level ordering #14

Closed bojan88 closed 3 weeks ago

bojan88 commented 1 month ago

Support for top level ordering using dot notation. https://postgrest.org/en/v12/references/api/resource_embedding.html#top-level-ordering

No interface changes. The only change is supported fields for column in .order([{ column: 'column_name' }]). Instead of only accepting current table column names, it now accepts embedded fields with dot notation to drill down.

Example:

const query = pgClient
  .query('films')
  .select('title')
  .select(pgClient.embeddedQuery('directors', 'one').select('*'))
  .order([{ column: 'directors.last_name', order: 'desc' }]);

Alternative considerations

We considered different approaches to implement this and current implementation was more difficult to implement but better for developer experience.

One of the alternatives we considered: Using more properties for order method parameters to say it's a top level query (top: true). Example:

const query = pgClient
  .query('films')
  .select('title')
  .select(pgClient.embeddedQuery('directors', 'one')
    .select('*')
    .order([{ column: 'last_name', order: 'desc', top: true }])
  );

The problem with this approach was with order precedence when top level and root orders are used together. We had problems constructing a query with both of these options order=id,films(id) and order=films(id),id. We considered 2 solutions - precedence based on method call order, and one more property for precedence like weight: number. Both of these options looked like unintuitive, so instead we ended up with dot notation on root query (implementation in this PR)


Note that documentation will be added later after this is released.

kegjon commented 3 weeks ago

I like the interface. The dot-notation for referencing embedded fields I think is fairly clear.

My only request was going to be to add the docs, but given you intend to already, I have nothing to add. Looks good.