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 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
4 changes: 4 additions & 0 deletions libs/application_runner/.dialyzer_ignore.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{"lib/mongo_storage.ex", :pattern_match, 112},
{"lib/mongo_storage.ex", :pattern_match, 93}
]
45 changes: 45 additions & 0 deletions libs/application_runner/lib/controllers/docs_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,51 @@ defmodule ApplicationRunner.DocsController do
end
end

def insert_many(
conn,
%{"coll" => coll},
%{"documents" => docs},
%{environment: env, transaction_id: transaction_id},
replace_params
) do
with filtered_docs <- Enum.map(docs, fn doc -> Map.delete(doc, "_id") end),
{:ok, docs_res} <-
MongoInstance.run_mongo_task(env.id, MongoStorage, :insert_many, [
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 insert_many(
conn,
%{"coll" => coll},
%{"documents" => docs},
%{environment: env},
replace_params
) do
with filtered_docs <- Enum.map(docs, fn doc -> Map.delete(doc, "_id") end),
{:ok, docs_res} <-
MongoInstance.run_mongo_task(env.id, MongoStorage, :insert_many, [
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
41 changes: 41 additions & 0 deletions libs/application_runner/lib/mongo_storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,47 @@ defmodule ApplicationRunner.MongoStorage do
# DATA #
########

@doc """
Creates documents from the specified `docs`
"""
def insert_many(env_id, coll, docs) do
Logger.debug(
"#{__MODULE__} insert_many 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, %Mongo.InsertManyResult{} = res} ->
{:ok, %{"insertedIds" => res.inserted_ids}}
end
end

def insert_many(env_id, coll, docs, session_uuid) do
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, %Mongo.InsertManyResult{} = res} ->
{:ok, %{"insertedIds" => res.inserted_ids}}
end
end

@spec create_doc(number(), String.t(), map(), any()) ::
{:ok, map()} | {:error, TechnicalErrorType.t()}
def create_doc(env_id, coll, doc) do
Expand Down
1 change: 1 addition & 0 deletions libs/application_runner/lib/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule ApplicationRunner.Router do
# Mongo functions
post("/data/colls/:coll/find", DocsController, :find)
post("/data/colls/:coll/updateMany", DocsController, :update_many)
post("/data/colls/:coll/insertMany", DocsController, :insert_many)
# Transactions
post("/data/transaction", DocsController, :transaction)
post("/data/transaction/commit", DocsController, :commit_transaction)
Expand Down
23 changes: 23 additions & 0 deletions libs/application_runner/test/controllers/docs_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@ defmodule ApplicationRunner.DocsControllerTest do
end
end

describe "ApplicationRunner.DocsController.insert_many" do
test "Should insert multiple docs", %{
conn: conn,
token: token,
mongo_pid: mongo_pid
} do
assert {:ok, body} = Jason.encode(%{"documents" => [%{"foo" => "bar"}, %{"foo" => "baz"}]})

conn =
conn
|> Plug.Conn.put_req_header("authorization", "Bearer " <> token)
|> Plug.Conn.put_req_header("content-type", "application/json")
|> post(Routes.docs_path(conn, :insert_many, "insert_many"), body)

assert %{"insertedIds" => ids} = json_response(conn, 200)

assert length(ids) == 2

assert [%{"foo" => "bar"}, %{"foo" => "baz"}] =
Mongo.find(mongo_pid, "insert_many", %{}) |> Enum.to_list()
end
end

describe "ApplicationRunner.DocsController.update" do
test "should be protected with a token", %{conn: conn, doc_id: doc_id} do
conn = put(conn, Routes.docs_path(conn, :update, @coll, doc_id), %{"foo" => "bar"})
Expand Down
Loading