Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Create repository query guide with examples #344

Merged
merged 4 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/guides.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ categories:
- path: overview
- path: database-configuration
- path: repositories
- path: query-examples
title: Query Examples
- path: entities
- path: data-types
- path: postgresql
Expand Down
73 changes: 73 additions & 0 deletions source/guides/models/query-examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
title: Guides - Query Examples
---

## Order

For add `order` to your relation just call `#order` method with block:

```ruby
repo = UserRepository.new

repo.users.order { created_at.desc }
repo.users.order { created_at.asc }

# you can set any field for order
repo.users.order { name.asc }
repo.users.order { last_name.asc }

# you can use other relation for ordering
repo.users.order(repo.books[:title].qualified.asc)
```

## SQL Functions

You can use any SQL functions like `LIKE`, `ILIKE`, etc. For this call this functions in `where` block:

```ruby
repo = UserRepository.new

repo.users.where { name.ilike('%anton%') }
repo.users.where { name.like('%anton%') }

# you can call IN functions too
repo.users.where { id.in(1) }
repo.users.where { id.in(1..100) }
repo.users.where { id > 1 || id < 100 }

# you can call NOT functions too
repo.users.where { id.not(1) }
repo.users.where { id.not(1..100) }
```

```ruby
name = repo.user[:name].qualified
repo.where { length(name) > 10) }
```

```ruby
repo.users.where { id.in(1..100) }.limit(100)
```

```ruby
repo.users.where(id: 1) == repo.users.by_pk(1)
```

## Joins
https://stackoverflow.com/questions/43080678/join-query-in-hanami-model/43156257#43156257

```ruby

repo.users.join(:posts)
repo.users.join(:posts, id: :user_id)
repo.users.join(repo.posts)
```

also you can use `#inner_join` method

## Group by

Examples with groyp you can find here:
https://github.com/rom-rb/rom-sql/blob/master/spec/unit/relation/group_spec.rb

Also you can't use `#select` in hanami model. YOu need to use `#project` instead