Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

alex-tan/postgrest-queries

NOTE: This package has been moved to alex-tan/postgrest-client

postgrest-client should be a drop in replacement for the latest version of this package.

Build Status

This package allows you to easily construct Postgrest query strings using Elm.

Most query operators are currently supported:

View Full Documentation

Using select

If you're not selecting any nested resources in your request, you can use attributes:

-- "select=id,name"
P.toQueryString
    [ P.select <| P.attributes [ "id", "name" ]
    ]

If you want to select attributes and resources, you can use the attribute and resource functions:

-- select=id,name,grades(percentage)
P.toQueryString
  [ P.select
      [ P.attribute "id"
      , P.attribute "name"
      , P.resource "grades"
          [ P.attribute "percentage"
          ]
      ]
  ]

The library also provides a nice abstraction that allows you to both specify nested resources in a select clause, as well as use other postgrest parameters on those nested resources such as order, limit, and all of the usual conditional parameters such as eq:

-- select=id,name,grades(percentage)&grades.order=percentage.desc&grades.limit=10
P.toQueryString
  [ P.select
      [ P.attribute "id"
      , P.attribute "name"
      , P.resourceWithParams "grades"
          [ P.order [ P.desc "percentage" ], P.limit 10 ]
          [ P.attribute "percentage"
          ]
      ]
  ]

Conditions

The library currently supports the most commonly used query parameters. Here's a sampling of how they can be used in combination with one another:

-- student_id=eq.100&grade=gte.90&or=(self_evaluation.gte.90,self_evaluation.is.null)
P.toQueryString
  [ P.param "student_id" <| P.eq <| P.int 100
  , P.param "grade" <| P.gte <| P.int 90
  , P.or
      [ P.param "self_evaluation" <| P.gte <| P.int 90
      , P.param "self_evaluation" P.null
      ]
  ]

The in operator can be used with inList. The second parameter is a list of whatever values you're using in your app and the first argument is the function that will transform the items in that list into the library's Value type such as string or int.

-- name=in.("Chico","Harpo","Groucho")
P.toQueryString
  [ P.param "name" <| P.inList P.string [ "Chico", "Harpo", "Groucho" ]
  ]

Order

You can order results by multiple columns as well as using nullsfirst or nullslast.

-- order=age.asc.nullsfirst,created_at.desc
P.toQueryString
  [ P.order
      [ P.asc "age" |> P.nullsfirst
      , P.desc "created_at"
      ]
  ]

Combining Params

Maybe you have default parameters that you want to reuse across multiple functions. You can combine them using combineParams:

defaultParams : P.Params
defaultParams =
    [ P.select <| P.attributes [ "id", "name" ]
    , P.limit 10
    ]


constructParams : P.Params -> P.Params
constructParams =
    P.combineParams defaultParams


-- limit=100&select=id,name
constructParams [ P.limit 100 ]

Note that the merging of the two sets is not recursive. The two are merged by the final query parameter name such as order or children.order, etc... and the second set's value is always preferred.

About

Construct postgrest queries in elm

Resources

License

Packages

No packages published

Languages