Cassandrax
Cassandrax is a Cassandra ORM built on top of Xandra library and Ecto data mapping.
Cassandrax is inspired by the Triton and Ecto projects.
It allows you to build and run CQL statements as well as map results to Elixir structs.
The docs can be found at https://hexdocs.pm/cassandrax.
Installation
def deps do
[
{:cassandrax, "~> 0.1.0"}
]
endSetup
test_conn_attrs = [
nodes: ["127.0.0.1:9043"],
username: "cassandra",
password: "cassandra"
]
child = Cassandrax.Supervisor.child_spec(Cassandrax.MyConn, test_conn_attrs)
Cassandrax.start_link([child])Defining a new keyspace module.
defmodule MyKeyspace do
use Cassandrax.Keyspace, cluster: Cassandrax.MyConn, name: "my_keyspace"
endCreating a keyspace.
statement = \"""
CREATE KEYSPACE IF NOT EXISTS my_keyspace
WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1}
\"""
Cassandrax.cql(Cassandrax.MyConn, statement)Creating a table in the Keyspace.
statement = [
"CREATE TABLE IF NOT EXISTS ",
"my_keyspace.user(",
"id integer, ",
"user_name text, ",
"svalue set<text>, ",
"PRIMARY KEY (id))"
]
{:ok, _result} = Cassandrax.cql(Cassandrax.MyConn, statement)Usage
Inserting data.
user = %User{id: 1, user_name: "alice"}
{:ok, user} = MyKeyspace.insert(user)
user = MyKeyspace.insert!(user)Updating data.
changeset = Changeset.change(user, user_name: "bob")
{:ok, updated_user} = MyKeyspace.update(changeset)
updated_user = MyKeyspace.update!(changeset)Deleting data.
{:ok, user} = MyKeyspace.delete(user)
user = MyKeyspace.delete!(user)Batch operations.
user = %User{id: 1, user_name: "alice"}
changeset = MyKeyspace.get(TestData, id: 2) |> Changeset.change(user_name: "eve")
MyKeyspace.batch(fn batch ->
batch
|> MyKeyspace.batch_insert(user)
|> MyKeyspace.batch_update(changeset)
end)Querying data.
Get records.
MyKeyspace.get(User, id: 0)Get all records.
MyKeyspace.all(User)Get one record.
User |> where(id: 0) |> MyKeyspace.one()