Skip to content

Commit 6dfda85

Browse files
committed
chore(exandra): init repo for exandra
Initializing `Repo` module to use `exandra` as adapter for appengine_api Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
1 parent fd67c54 commit 6dfda85

File tree

6 files changed

+145
-53
lines changed

6 files changed

+145
-53
lines changed

apps/astarte_appengine_api/config/config.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import Config
88
# General application configuration
99
config :astarte_appengine_api, namespace: Astarte.AppEngine.API
1010

11+
config :astarte_appengine_api, ecto_repos: [Astarte.AppEngine.API.Repo]
12+
13+
config :astarte_appengine_api, Astarte.AppEngine.API.Repo
14+
1115
# Configures the endpoint
1216
config :astarte_appengine_api, Astarte.AppEngine.APIWeb.Endpoint,
1317
url: [host: "localhost"],

apps/astarte_appengine_api/lib/astarte_appengine_api/application.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ defmodule Astarte.AppEngine.API.Application do
5555
Astarte.AppEngine.API.Rooms.AMQPClient,
5656
Astarte.AppEngine.APIWeb.Endpoint,
5757
{Xandra.Cluster, ae_xandra_opts},
58-
{Astarte.DataAccess, data_access_opts}
58+
{Astarte.DataAccess, data_access_opts},
59+
{Astarte.AppEngine.API.Repo, xandra_options}
5960
]
6061

6162
# See https://hexdocs.pm/elixir/Supervisor.html
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#
2+
# This file is part of Astarte.
3+
#
4+
# Copyright 2025 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
# TODO: Copied from astarte_data_access PR #71, see: https://github.com/astarte-platform/astarte_data_access/pull/71
22+
# use `astarte_data_access` when it will be merged
23+
defmodule Astarte.AppEngine.API.Repo do
24+
@moduledoc false
25+
use Ecto.Repo, otp_app: :astarte_appengine_api, adapter: Exandra
26+
alias Astarte.DataAccess.Config
27+
require Ecto.Query
28+
29+
@impl Ecto.Repo
30+
def init(_context, config) do
31+
config =
32+
Config.xandra_options!()
33+
|> Keyword.merge(config)
34+
35+
{:ok, config}
36+
end
37+
38+
def fetch(queryable, id, opts \\ []) do
39+
queryable
40+
|> query_for_get(id)
41+
|> fetch_one(opts)
42+
end
43+
44+
def fetch_by(queryable, clauses, opts \\ []) do
45+
queryable
46+
|> Ecto.Query.where([], ^Enum.to_list(clauses))
47+
|> fetch_one(opts)
48+
end
49+
50+
def fetch_one(queryable, opts \\ []) do
51+
{error, opts} = Keyword.pop_first(opts, :error, :not_found)
52+
53+
case all(queryable, opts) do
54+
[item] -> {:ok, item}
55+
[] -> {:error, error}
56+
other -> raise Ecto.MultipleResultsError, queryable: queryable, count: length(other)
57+
end
58+
end
59+
60+
# Copyright: elixir-ecto/ecto
61+
defp query_for_get(_queryable, nil) do
62+
raise ArgumentError, "cannot perform Ecto.Repo.get/2 because the given value is nil"
63+
end
64+
65+
# Copyright: elixir-ecto/ecto
66+
defp query_for_get(queryable, id) do
67+
query = Ecto.Queryable.to_query(queryable)
68+
schema = assert_schema!(query)
69+
70+
case schema.__schema__(:primary_key) do
71+
[pk] ->
72+
Ecto.Query.from(x in query, where: field(x, ^pk) == ^id)
73+
74+
pks ->
75+
raise ArgumentError,
76+
"Ecto.Repo.get/2 requires the schema #{inspect(schema)} " <>
77+
"to have exactly one primary key, got: #{inspect(pks)}"
78+
end
79+
end
80+
81+
# Copyright: elixir-ecto/ecto
82+
defp assert_schema!(%{from: %{source: {_source, schema}}}) when schema != nil, do: schema
83+
84+
# Copyright: elixir-ecto/ecto
85+
defp assert_schema!(query) do
86+
raise Ecto.QueryError,
87+
query: query,
88+
message: "expected a from expression with a schema"
89+
end
90+
end

apps/astarte_appengine_api/lib/astarte_appengine_api_web/gettext.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ defmodule Astarte.AppEngine.APIWeb.Gettext do
3737
3838
See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
3939
"""
40-
use Gettext, otp_app: :astarte_appengine_api
40+
use Gettext.Backend, otp_app: :astarte_appengine_api
4141
end

apps/astarte_appengine_api/mix.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ defmodule Astarte.AppEngine.API.Mixfile do
9595
{:ex_json_schema, "~> 0.7"},
9696
{:phoenix_swagger, "~> 0.8"},
9797
{:xandra, "~> 0.13"},
98+
{:exandra, "~> 0.12"},
99+
{:typed_ecto_schema, "~> 0.4"},
100+
# Fix: could not compile dependency due to an old snappy version (1.2.8). Delete when removing xandra
101+
{:snappyer, "~> 1.2.10", override: true},
98102
{:pretty_log, "~> 0.1"},
99103
{:plug_logger_with_meta, "~> 0.1"},
100104
{:telemetry, "~> 0.4"},
@@ -111,7 +115,7 @@ defmodule Astarte.AppEngine.API.Mixfile do
111115
# Test section
112116
{:excoveralls, "~> 0.15", only: :test},
113117
{:mox, "~> 0.5", only: :test},
114-
{:stream_data, "~> 0.5", only: :test}
118+
{:stream_data, "~> 0.5", only: [:test, :dev]}
115119
]
116120
end
117121
end

0 commit comments

Comments
 (0)