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

feat: Implement mongo insertMany #505

Merged
merged 22 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import_deps: [:phoenix],
inputs: [
"*.{ex,exs}",
"{config,lib,test,priv}/**/*.{ex,exs}",
"{config,lib,test,priv,libs}/**/*.{ex,exs}",
jonas-martinez marked this conversation as resolved.
Show resolved Hide resolved
"apps/*/{config,lib,test,priv}/**/*.{ex,exs}"
],
line_length: 120
Expand Down
56 changes: 44 additions & 12 deletions libs/application_runner/lib/controllers/docs_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,46 @@ defmodule ApplicationRunner.DocsController do
end
end

def create(
conn,
%{"coll" => coll},
%{"_json" => docs},
%{environment: env, transaction_id: transaction_id},
replace_params
)
when is_list(docs) do
with filtered_docs <- Enum.map(docs, fn doc -> Map.delete(doc, "_id") end),
{:ok, docs_res} <-
MongoInstance.run_mongo_task(env.id, MongoStorage, :create_docs, [
env.id,
coll,
Parser.replace_params(filtered_docs, replace_params),
transaction_id
]) do
Logger.debug(
"#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with res #{inspect(docs_res)}"
)

reply(conn, docs_res)
end
end

def create(conn, %{"coll" => coll}, %{"_json" => docs}, %{environment: env}, replace_params) when is_list(docs) do
with filtered_docs <- Enum.map(docs, fn doc -> Map.delete(doc, "_id") end),
{:ok, docs_res} <-
MongoInstance.run_mongo_task(env.id, MongoStorage, :create_docs, [
env.id,
coll,
Parser.replace_params(filtered_docs, replace_params)
]) do
Logger.debug(
"#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with res #{inspect(docs_res)}"
)

reply(conn, docs_res)
end
end

def create(
conn,
%{"coll" => coll},
Expand Down Expand Up @@ -168,9 +208,7 @@ defmodule ApplicationRunner.DocsController do
doc_id,
transaction_id
]) do
Logger.debug(
"#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with status :ok"
)
Logger.debug("#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with status :ok")

reply(conn)
end
Expand All @@ -185,9 +223,7 @@ defmodule ApplicationRunner.DocsController do
) do
with :ok <-
MongoInstance.run_mongo_task(env.id, MongoStorage, :delete_doc, [env.id, coll, doc_id]) do
Logger.debug(
"#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with status :ok"
)
Logger.debug("#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with status :ok")

reply(conn)
end
Expand Down Expand Up @@ -310,9 +346,7 @@ defmodule ApplicationRunner.DocsController do
transaction_id,
env.id
]) do
Logger.debug(
"#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with status :ok"
)
Logger.debug("#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with status :ok")

reply(conn)
end
Expand All @@ -330,9 +364,7 @@ defmodule ApplicationRunner.DocsController do
transaction_id,
env.id
]) do
Logger.debug(
"#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with status :ok"
)
Logger.debug("#{__MODULE__} respond to #{inspect(conn.method)} on #{inspect(conn.request_path)} with status :ok")

reply(conn)
end
Expand Down
69 changes: 48 additions & 21 deletions libs/application_runner/lib/mongo_storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ defmodule ApplicationRunner.MongoStorage do

@spec get_mongo_user_link!(number(), number()) :: any
def get_mongo_user_link!(env_id, user_id) do
repo().one!(
from(mul in MongoUserLink, where: mul.user_id == ^user_id and mul.environment_id == ^env_id)
)
repo().one!(from(mul in MongoUserLink, where: mul.user_id == ^user_id and mul.environment_id == ^env_id))
end

@spec has_user_link?(number(), number()) :: any()
Expand All @@ -73,12 +71,51 @@ defmodule ApplicationRunner.MongoStorage do
# DATA #
########

@doc """
Creates documents from the specified `docs`
"""
def create_docs(env_id, coll, docs) do
Logger.debug("#{__MODULE__} create_docs for env_id: #{env_id}, coll: #{coll}, docs: #{inspect(docs)}")

decoded_docs = Enum.map(docs, fn doc -> decode_ids(doc) end)

env_id
|> mongo_instance()
|> Mongo.insert_many(coll, decoded_docs)
|> case do
{:error, err} ->
TechnicalError.mongo_error_tuple(err)

{:ok, res} ->
# TODO: Think about something to return
{:ok, %{}}
end
end

def create_docs(env_id, coll, docs, session_uuid) do
jonas-martinez marked this conversation as resolved.
Show resolved Hide resolved
Logger.debug(
"#{__MODULE__} create_doc for env_id: #{env_id}, coll: #{coll}, doc: #{inspect(docs)}, session_uuid: #{inspect(session_uuid)}"
)

decoded_docs = Enum.map(docs, fn doc -> decode_ids(doc) end)

env_id
|> mongo_instance()
|> Mongo.insert_many(coll, decoded_docs, session: Swarm.whereis_name(session_uuid))
|> case do
{:error, err} ->
TechnicalError.mongo_error_tuple(err)

{:ok, res} ->
# TODO: Think about something to return
{:ok, %{}}
end
end

@spec create_doc(number(), String.t(), map(), any()) ::
{:ok, map()} | {:error, TechnicalErrorType.t()}
def create_doc(env_id, coll, doc) do
Logger.debug(
"#{__MODULE__} create_doc for env_id: #{env_id}, coll: #{coll}, doc: #{inspect(doc)}"
)
Logger.debug("#{__MODULE__} create_doc for env_id: #{env_id}, coll: #{coll}, doc: #{inspect(doc)}")

decoded_doc = decode_ids(doc)

Expand Down Expand Up @@ -115,19 +152,15 @@ defmodule ApplicationRunner.MongoStorage do

@spec fetch_doc(number(), String.t(), term()) :: {:ok, map()} | {:error, TechnicalErrorType.t()}
def fetch_doc(env_id, coll, doc_id) when is_bitstring(doc_id) do
Logger.debug(
"#{__MODULE__} fetch_doc for env_id: #{env_id}, coll: #{coll}, doc: #{inspect(doc_id)}"
)
Logger.debug("#{__MODULE__} fetch_doc for env_id: #{env_id}, coll: #{coll}, doc: #{inspect(doc_id)}")

with {:ok, bson_doc_id} <- decode_object_id(doc_id) do
fetch_doc(env_id, coll, bson_doc_id)
end
end

def fetch_doc(env_id, coll, bson_doc_id) when is_struct(bson_doc_id, BSON.ObjectId) do
Logger.debug(
"#{__MODULE__} fetch_doc for env_id: #{env_id}, coll: #{coll}, doc: #{inspect(bson_doc_id)}"
)
Logger.debug("#{__MODULE__} fetch_doc for env_id: #{env_id}, coll: #{coll}, doc: #{inspect(bson_doc_id)}")

env_id
|> mongo_instance()
Expand Down Expand Up @@ -161,9 +194,7 @@ defmodule ApplicationRunner.MongoStorage do
@spec filter_docs(number(), String.t(), map(), keyword()) ::
{:ok, list(map())} | {:error, TechnicalErrorType.t()}
def filter_docs(env_id, coll, filter, opts \\ []) do
Logger.debug(
"#{__MODULE__} filter_docs for env_id: #{env_id}, coll: #{coll}, filter: #{inspect(filter)}"
)
Logger.debug("#{__MODULE__} filter_docs for env_id: #{env_id}, coll: #{coll}, filter: #{inspect(filter)}")

clean_filter = decode_ids(filter)

Expand Down Expand Up @@ -212,9 +243,7 @@ defmodule ApplicationRunner.MongoStorage do
{_value, filtered_doc} <- Map.pop(decoded_doc, "_id") do
env_id
|> mongo_instance()
|> Mongo.replace_one(coll, %{"_id" => bson_doc_id}, filtered_doc,
session: Swarm.whereis_name(session_uuid)
)
|> Mongo.replace_one(coll, %{"_id" => bson_doc_id}, filtered_doc, session: Swarm.whereis_name(session_uuid))
|> case do
{:error, err} ->
TechnicalError.mongo_error_tuple(err)
Expand Down Expand Up @@ -243,9 +272,7 @@ defmodule ApplicationRunner.MongoStorage do

@spec delete_doc(number(), String.t(), String.t()) :: :ok | {:error, TechnicalErrorType.t()}
def delete_doc(env_id, coll, doc_id) do
Logger.debug(
"#{__MODULE__} update_doc for env_id: #{env_id}, coll: #{coll}, doc_id: #{inspect(doc_id)}"
)
Logger.debug("#{__MODULE__} update_doc for env_id: #{env_id}, coll: #{coll}, doc_id: #{inspect(doc_id)}")

with {:ok, bson_doc_id} <- decode_object_id(doc_id) do
env_id
Expand Down
4 changes: 2 additions & 2 deletions libs/application_runner/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule ApplicationRunner.MixProject do
defp deps do
[
{:credo, "~> 1.6.7", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.2", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.4.2", only: [:dev, :test], runtime: false},
{:ex_component_schema,
git: "https://github.com/lenra-io/ex_component_schema", ref: "v1.0.0-beta.6"},
{:jason, "~> 1.4"},
Expand All @@ -46,7 +46,7 @@ defmodule ApplicationRunner.MixProject do
{:phoenix, "~> 1.6.15"},
{:finch, "~> 0.14"},
{:bypass, "~> 2.1", only: :test},
{:mongodb_driver, "~> 1.0.2"},
{:mongodb_driver, "~> 1.2.1"},
{:crontab, "~> 1.1.13"},
{:quantum, "~> 3.0"},
{:query_parser, "~> 1.0.0-beta.27"},
Expand Down
Loading
Loading