Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v0.5.0

* Added support for `:select` query option, to select specific fields from the database.

## v0.4.0

* Added support for `new_#{record}` function
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Add `context_kit` to your list of dependencies in `mix.exs`:

def deps do
[
{:context_kit, "~> 0.4.0"}
{:context_kit, "~> 0.5.0"}
]
end

Expand Down
8 changes: 7 additions & 1 deletion lib/context_kit/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ defmodule ContextKit.Query do
- `:limit` - Limit number of results
- `:preload` - Preload associations
- `:paginate` - Enable pagination with optional configuration
- `:select` - Select only specific fields from the database
"""

import Ecto.Query
Expand All @@ -75,7 +76,8 @@ defmodule ContextKit.Query do
:limit,
:order_by,
:paginate,
:preload
:preload,
:select
]

def new(schema) do
Expand Down Expand Up @@ -321,6 +323,10 @@ defmodule ContextKit.Query do
limit(query, ^limit)
end

defp apply_query_option({:select, fields}, query, _binding_name) do
select(query, ^fields)
end

defp apply_query_option({:preload, preload}, query, _binding_name) do
preload(query, ^preload)
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ContextKit.MixProject do
use Mix.Project

@version "0.4.0"
@version "0.5.0"
@source_url "https://github.com/egze/context_kit"

def project do
Expand Down
10 changes: 10 additions & 0 deletions test/context_kit/crud/scoped_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,17 @@ defmodule ContextKit.CRUD.ScopedTest do

assert [db_book] = ScopedBooks.list_scoped_books()

assert db_book.id == scoped_book.id
assert db_book.title == "My Book"
end

test "selects specific fields" do
assert {:ok, scoped_book} = Repo.insert(%ScopedBook{title: "My Book"})

assert [db_book] = ScopedBooks.list_scoped_books(select: [:id])

assert scoped_book.id == db_book.id
assert db_book.title == nil
end

test "filters by scope" do
Expand Down
12 changes: 11 additions & 1 deletion test/context_kit/crud_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ defmodule ContextKit.CRUDTest do

assert [db_book] = Books.list_books()

assert book.id == db_book.id
assert db_book.id == book.id
assert db_book.title == "My Book"
end

test "selects specific fields" do
assert {:ok, scoped_book} = Repo.insert(%Book{title: "My Book"})

assert [db_book] = Books.list_books(select: [:id])

assert scoped_book.id == db_book.id
assert db_book.title == nil
end

test "filters by fields in keyword list" do
Expand Down