mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-20 05:23:18 +12:00
improvement: add postgres expressions guide
This commit is contained in:
parent
0e6bcd047e
commit
25883978de
1 changed files with 40 additions and 0 deletions
40
documentation/topics/postgres-expressions.md
Normal file
40
documentation/topics/postgres-expressions.md
Normal file
|
@ -0,0 +1,40 @@
|
|||
# Postgres Expressions
|
||||
|
||||
In addition to the expressions listed in the [Ash expressions guide](https://hexdocs.pm/ash/expressions.html), AshPostgres provides the following expressions
|
||||
|
||||
## Fragments
|
||||
`fragment` allows you to embed raw sql into the query. Use question marks to interpolate values from the outer expression.
|
||||
|
||||
For example:
|
||||
|
||||
```elixir
|
||||
Ash.Query.filter(User, fragment("? IS NOT NULL", first_name))
|
||||
```
|
||||
|
||||
# Like and ILike
|
||||
|
||||
These wrap the postgres builtin like and ilike operators.
|
||||
|
||||
Please be aware, these match *patterns* not raw text. Use `contains/1` if you want to match text without supporting patterns, i.e `%` and `_` have semantic meaning!
|
||||
|
||||
For example:
|
||||
|
||||
```elixir
|
||||
Ash.Query.filter(User, ilike(name, "%obo%")) # name contains obo anywhere in the string, case sensitively
|
||||
```
|
||||
|
||||
```elixir
|
||||
Ash.Query.filter(User, ilike(name, "%ObO%")) # name contains ObO anywhere in the string, case insensitively
|
||||
```
|
||||
|
||||
# Trigram similarity
|
||||
|
||||
To use this expression, you must have the `pg_trgm` extension in your repos `installed_extensions` list.
|
||||
|
||||
This calls the `similarity` function from that extension. See more https://www.postgresql.org/docs/current/pgtrgm.htmlhere: https://www.postgresql.org/docs/current/pgtrgm.html
|
||||
|
||||
For example:
|
||||
|
||||
```elixir
|
||||
Ash.Query.filter(User, trigram_similarity(first_name, "fred") > 0.8)
|
||||
```
|
Loading…
Reference in a new issue