diff --git a/README.md b/README.md index f3d08bd..ebb12e6 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,40 @@ UserByEmail |> User.one ``` +## Comparison / Range Queries + +Select messages created before **timestamp** + +```elixir +MessagesByDate +|> select([:message_id, :text]) +|> where(channel_id: 1, created: ["<=": timestamp]) +|> limit(20) +|> MessagesByDate.all +``` + +Select messages created between **timestamp_a** and **timestamp_b** + +```elixir +MessagesByDate +|> select([:message_id, :text]) +|> where(channel_id: 1, created: [">=": timestamp_a], created: [<: timestamp_b]) +|> MessagesByDate.all +``` + +## Streaming + +Stream all messages + +```elixir +MessagesByDate +|> select(:all) +|> where(channel_id: 1) +|> MessagesByDate.stream(page_size: 20) +``` + +Which returns {:ok, stream} or {:error, msg} + ## Inserting, Updating, & Deleting Again, lets import Triton.Query for the necessary macros. @@ -319,3 +353,40 @@ User |> where(user_id: 10) |> User.save ``` + +## Pre-populating data + +You can pre-populate data with Triton at compile time with **Triton.Setup** + +```elixir +defmodule PrepopulateModule do + use Triton.Setup + import Triton.Query + require Schema.User + alias Schema.User + + # create an admin user if it doesn't exist + + setup do + User + |> insert( + user_id: @admin_user_id, + username: @admin_user_username, + display_name: @admin_user_display_name, + password: Bcrypt.hashpwsalt(@admin_user_password), + email: @admin_user_email, + created: @admin_user_created + ) |> if_not_exists + end +end +``` + +## Automatic Schema Creation + +Triton attempts to create your keyspace, tables, and materialized views at compile time if they do not exist. + +This means that your build server will need access to your production DB if you want to automatically create your schema in prod. The alternative is simply to create your production schemas yourself. + +## Consistency levels + +For dev, you may want to consider running ccm with more than 1 node if you are doing queries at anything more than consistency: :one. diff --git a/changelog.md b/changelog.md index af2a58e..94eae5a 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## v 0.1.10 + +**Breaking** - Table.streamed renamed to Table.stream + +Added more documentation for range queries and streaming to readme. + ## v 0.1.9 Change supervision tree so that clusters cannot interrupt each other. diff --git a/lib/triton/executor.ex b/lib/triton/executor.ex index 51231e5..d8d516c 100644 --- a/lib/triton/executor.ex +++ b/lib/triton/executor.ex @@ -8,8 +8,8 @@ defmodule Triton.Executor do end end - def streamed(query, options \\ []) do - case Triton.Executor.execute([{:streamed, true} | query], options) do + def stream(query, options \\ []) do + case Triton.Executor.execute([{:stream, true} | query], options) do {:error, err} -> {:error, err.message} {:ok, results} -> {:ok, transform_results(query, results)} end @@ -101,7 +101,7 @@ defmodule Triton.Executor do defp build_cql(query) do case Triton.Helper.query_type(query) do - :streamed -> {:ok, :streamed, Triton.CQL.Select.build(query)} + :stream -> {:ok, :stream, Triton.CQL.Select.build(query)} :count -> {:ok, :count, Triton.CQL.Select.build(query)} :select -> {:ok, :select, Triton.CQL.Select.build(query)} :insert -> {:ok, :insert, Triton.CQL.Insert.build(query)} @@ -111,14 +111,14 @@ defmodule Triton.Executor do end end - defp execute_cql(conn, :streamed, cql, nil, options) do + defp execute_cql(conn, :stream, cql, nil, options) do with pages <- Xandra.stream_pages!(conn, cql, [], [pool: Xandra.Cluster] ++ options) do results = pages |> Stream.flat_map(fn page -> Enum.to_list(page) |> format_results end) {:ok, results} end end - defp execute_cql(conn, :streamed, cql, prepared, options) do + defp execute_cql(conn, :stream, cql, prepared, options) do with {:ok, statement} <- Xandra.prepare(conn, cql, [pool: Xandra.Cluster] ++ options), pages <- Xandra.stream_pages!(conn, statement, atom_to_string_keys(prepared), [pool: Xandra.Cluster] ++ options) do diff --git a/lib/triton/helper.ex b/lib/triton/helper.ex index 0a21927..c294b07 100644 --- a/lib/triton/helper.ex +++ b/lib/triton/helper.ex @@ -1,7 +1,7 @@ defmodule Triton.Helper do def query_type(query) do cond do - query[:streamed] -> :streamed + query[:stream] -> :stream query[:count] -> :count query[:select] -> :select query[:insert] -> :insert diff --git a/mix.exs b/mix.exs index 871623a..2ddb6f4 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Triton.Mixfile do use Mix.Project - @version "0.1.9" + @version "0.1.10" @url "https://github.com/blitzstudios/triton" @maintainers ["Weixi Yen"]