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

Update documentation and add initial project configurations #46

Merged
merged 3 commits into from
Jan 22, 2024
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
6 changes: 3 additions & 3 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ the community.

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
<https://www.contributor-covenant.org/version/2/0/code_of_conduct.html>.

Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.
<https://www.contributor-covenant.org/faq>. Translations are available at
<https://www.contributor-covenant.org/translations>.
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
![GitHub](https://img.shields.io/github/license/vinniefranco/exandra)
[![CI](https://github.com/vinniefranco/exandra/actions/workflows/main.yml/badge.svg)](https://github.com/vinniefranco/exandra/actions/workflows/main.yml)
[![Coverage Status](https://coveralls.io/repos/github/vinniefranco/exandra/badge.svg?branch=main)](https://coveralls.io/github/vinniefranco/exandra?branch=main)
![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/hex/exandra)
![Libraries.io dependency status for the latest release](https://img.shields.io/librariesio/release/hex/exandra)

Adapter module for [Apache Cassandra](https://cassandra.apache.org/_/index.html)
and [ScyllaDB](https://www.scylladb.com/).

Uses [`Xandra`](https://github.com/lexhide/xandra) for communication with the
underlying database.

Uses [`Ecto`](https://github.com/elixir-ecto/ecto) for database interfacing,
running schema migrations, and querying operations.

## Configuration

To configure an `Ecto.Repo` that uses `Exandra` as its adapter, you can use
Expand All @@ -22,7 +25,7 @@ You can use the following options:
- Any of the options supported by `Ecto.Repo` itself, which you can see
in the `Ecto.Repo` documentation.

- Any of the option supported by `Xandra.Cluster.start_link/1`.
- Any of the options supported by `Xandra.Cluster.start_link/1`.

To configure your Ecto repository to use this adapter, you can use the
`:adapter` option. For example, when defining the repo:
Expand All @@ -33,6 +36,24 @@ defmodule MyApp.Repo do
end
```

You can configure your database connection in `config/dev.exs`. Here's an example:

```elixir
# Configure your database
config :my_app, MyApp.Repo,
migration_primary_key: [name: :id, type: :binary_id], # Overrides the default type `bigserial` used for version attribute in schema migration
contact_points: ["127.0.0.1"], # List of database connection endpoints
keyspace: "my_app_dev", # Name of your keyspace
port: 9042, # Default port
sync_connect: 5000, # Waiting time in milliseconds for the database connection
log: :info,
stacktrace: true,
show_sensitive_data_on_connection_error: true,
pool_size: 10
```

**Note:** The `bigserial` data type is specific to PostgreSQL databases and is not present in NoSQL databases.

## Schemas

You can regularly use `Ecto.Schema` with Exandra. For example:
Expand Down Expand Up @@ -73,6 +94,7 @@ If one of your fields is a UDT, you can use the `Exandra.UDT` type for it. For e
field :home_phone, Exandra.UDT, type: :phone_number
field :office_phone, Exandra.UDT, type: :phone_number
```

### Arrays

You can use arrays with the Ecto `{:array, <type>}` type. This gets translated to the
Expand Down Expand Up @@ -151,9 +173,9 @@ This is a non-comprehensive list of types you can use:
- `<udt>` - User-Defined Types (UDTs) should be specified as their name, expressed as an
atom. For example, a UDT called `full_name` would be specified as the type `:full_name`.
- `:naive_datetime`, `:naive_datetime_usec`, `:utc_datetime`, `:utc_datetime_usec` -
these get all represented as the `timestamp` type.
these are all represented as the `timestamp` type.

### User-Defined Types (UDTs)
### User-Defined Types (UDTs)

`Ecto.Migration` doesn't support creating, altering, or dropping Cassandra/Scylla **UDTs**.
To do those operations in a migration, use `Ecto.Migration.execute/1`
Expand Down
26 changes: 23 additions & 3 deletions lib/exandra.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ defmodule Exandra do
Uses [`Xandra`](https://github.com/lexhide/xandra) for communication with the
underlying database.

Uses [`Ecto`](https://github.com/elixir-ecto/ecto) for database interfacing,
running schema migrations, and querying operations.

## Configuration

To configure an `Ecto.Repo` that uses `Exandra` as its adapter, you can use
Expand All @@ -16,7 +19,7 @@ defmodule Exandra do
* Any of the options supported by `Ecto.Repo` itself, which you can see
in the `Ecto.Repo` documentation.

* Any of the option supported by `Xandra.Cluster.start_link/1`.
* Any of the options supported by `Xandra.Cluster.start_link/1`.

#{Exandra.Connection.start_opts_docs()}

Expand All @@ -27,6 +30,23 @@ defmodule Exandra do
use Ecto.Repo, otp_app: :my_app, adapter: Exandra
end

You can configure your database connection in `config/dev.exs`. Here's an example `dev configuration`:

# Configure your database
config :my_app, MyApp.Repo,
migration_primary_key: [name: :id, type: :binary_id], # Overrides the default type `bigserial` used for version attribute in schema migration
contact_points: ["127.0.0.1"], # List of database connection endpoints
keyspace: "my_app_dev", # Name of your keyspace
port: 9042, # Default port
sync_connect: 5000, # Waiting time in milliseconds for the database connection
log: :info,
stacktrace: true,
show_sensitive_data_on_connection_error: true,
pool_size: 10

**Note:** The `bigserial` data type is specific to PostgreSQL databases and is not present in Scylla/Cassandra.


## Schemas

You can regularly use `Ecto.Schema` with Exandra. For example:
Expand Down Expand Up @@ -159,7 +179,7 @@ defmodule Exandra do
* `<udt>` - User-Defined Types (UDTs) should be specified as their name, expressed as an
atom. For example, a UDT called `full_name` would be specified as the type `:full_name`.
* `:naive_datetime`, `:naive_datetime_usec`, `:utc_datetime`, `:utc_datetime_usec` -
these get all represented as the `timestamp` type.
these are all represented as the `timestamp` type.

### User-Defined Types (UDTs)

Expand All @@ -177,7 +197,7 @@ defmodule Exandra do
"""

# This overrides the checkout/3 function defined in Ecto.Adapters.SQL. That function
# is private API, and is defined by "use Ecto.Adapters.SQL". This is why this function
# is a private API, and is defined by "use Ecto.Adapters.SQL". This is why this function
# needs to appear BEFORE the call to "use Ecto.Adapters.SQL", so that we essentially
# override the call. We'll work with the Ecto team to make that callback overridable.
@doc false
Expand Down
6 changes: 3 additions & 3 deletions lib/exandra/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule Exandra.Connection do
{adapter_opts, opts} = Keyword.split(opts, @child_spec_opts_keys)
adapter_opts = NimbleOptions.validate!(adapter_opts, @child_spec_opts_schema)

# Drop the options that Ecto injects here, because Xandra will raise
# Drop the options that Ecto injects here because Xandra will raise
# for these.
opts = Keyword.drop(opts, @ecto_repo_start_opts_keys)

Expand Down Expand Up @@ -198,7 +198,7 @@ defmodule Exandra.Connection do
]

# TODO: HELP! Aaargh! Cassandra/Scylla can't do CAST in DELETE queries, it's a
# syntax error. So anyways, this is what we do for now to support rolling back
# syntax error. So anyway, this is what we do for now to support rolling back
# migrations D:. This is fixed in Ecto, we need to wait for a new release that includes
# https://github.com/elixir-ecto/ecto_sql/pull/531.
regex = ~r/^SELECT CAST\(version AS int\) FROM (?<table>\w+)$/
Expand Down Expand Up @@ -255,7 +255,7 @@ defmodule Exandra.Connection do
result = ["DELETE", from, where]

# TODO: HELP! Aaargh! Cassandra/Scylla can't do CAST in DELETE queries, it's a
# syntax error. So anyways, this is what we do for now to support rolling back
# syntax error. So anyway, this is what we do for now to support rolling back
# migrations D:. This is fixed in Ecto, we need to wait for a new release that includes
# https://github.com/elixir-ecto/ecto_sql/pull/531.
regex = ~r/^DELETE FROM (?<table>\w+) WHERE version = CAST\(\? AS int\)$/
Expand Down