Skip to content

Commit a51589c

Browse files
authored
Adds options argument to some functions (#172)
* Adds options argument to some functions There were some functions in the Repo module that did not accepts options. Some of those options (such as :database) are important to be able to pass through to the underlying Mongo functions. Signed-off-by: ksmithut <[email protected]> * fixes actually adding in upsert option to insert_or_update function Signed-off-by: ksmithut <[email protected]> Signed-off-by: ksmithut <[email protected]>
1 parent 201fb74 commit a51589c

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

lib/mongo/repo.ex

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,22 @@ defmodule Mongo.Repo do
7171
end
7272
end
7373

74-
def update(%{__struct__: module, _id: id} = doc) do
74+
def update(%{__struct__: module, _id: id} = doc, opts \\ []) do
7575
collection = module.__collection__(:collection)
7676
doc = module.timestamps(doc)
7777

78-
case Mongo.update_one(@topology, collection, %{_id: id}, %{"$set" => module.dump(doc)}, []) do
78+
case Mongo.update_one(@topology, collection, %{_id: id}, %{"$set" => module.dump(doc)}, opts) do
7979
{:error, reason} -> {:error, reason}
8080
{:ok, %{modified_count: _}} -> {:ok, doc}
8181
end
8282
end
8383

84-
def insert_or_update(%{__struct__: module, _id: id} = doc) do
84+
def insert_or_update(%{__struct__: module, _id: id} = doc, opts \\ []) do
8585
collection = module.__collection__(:collection)
8686
doc = module.timestamps(doc)
87+
opts = Keyword.put(opts, :upsert, true)
8788

88-
case Mongo.update_one(@topology, collection, %{_id: id}, %{"$set" => module.dump(doc)}, upsert: true) do
89+
case Mongo.update_one(@topology, collection, %{_id: id}, %{"$set" => module.dump(doc)}, opts) do
8990
{:error, reason} -> {:error, reason}
9091
{:ok, %{upserted_ids: [id]}} -> {:ok, %{doc | _id: id}}
9192
{:ok, %{modified_count: _}} -> {:ok, doc}
@@ -119,17 +120,18 @@ defmodule Mongo.Repo do
119120
%{doc | _id: id}
120121
end
121122

122-
def update!(%{__struct__: module, _id: id} = doc) do
123+
def update!(%{__struct__: module, _id: id} = doc, opts \\ []) do
123124
collection = module.__collection__(:collection)
124125
doc = module.timestamps(doc)
125-
Mongo.update_one!(@topology, collection, %{_id: id}, %{"$set" => module.dump(doc)}, [])
126+
Mongo.update_one!(@topology, collection, %{_id: id}, %{"$set" => module.dump(doc)}, opts)
126127
doc
127128
end
128129

129-
def insert_or_update!(%{__struct__: module, _id: id} = doc) do
130+
def insert_or_update!(%{__struct__: module, _id: id} = doc, opts \\ []) do
130131
collection = module.__collection__(:collection)
131132
doc = module.timestamps(doc)
132-
update_one_result = Mongo.update_one!(@topology, collection, %{_id: id}, %{"$set" => module.dump(doc)}, upsert: true)
133+
opts = Keyword.put(opts, :upsert, true)
134+
update_one_result = Mongo.update_one!(@topology, collection, %{_id: id}, %{"$set" => module.dump(doc)}, opts)
133135

134136
case update_one_result do
135137
%{upserted_ids: [id]} -> %{doc | _id: id}
@@ -230,8 +232,10 @@ defmodule Mongo.Repo do
230232
Mongo.count_documents(@topology, collection, filter, opts)
231233
end
232234

233-
def exists?(module, filter \\ %{}) do
234-
with {:ok, count} <- count(module, filter, limit: 1) do
235+
def exists?(module, filter \\ %{}, opts \\ []) do
236+
opts = Keyword.put(opts, :limit, 1)
237+
238+
with {:ok, count} <- count(module, filter, opts) do
235239
count > 0
236240
end
237241
end
@@ -243,7 +247,7 @@ defmodule Mongo.Repo do
243247
"""
244248
@callback config() :: Keyword.t()
245249

246-
@optional_callbacks get: 3, get_by: 3, aggregate: 3, exists?: 2, all: 3, stream: 3, update_all: 4, delete_all: 3
250+
@optional_callbacks get: 3, get_by: 3, aggregate: 3, exists?: 3, all: 3, stream: 3, update_all: 4, delete_all: 3
247251

248252
@doc """
249253
Returns a single document struct for the collection defined in the given module and bson object id.
@@ -348,7 +352,7 @@ defmodule Mongo.Repo do
348352
349353
MyApp.Repo.exists?(Post, %{title: title})
350354
"""
351-
@callback exists?(module :: module(), filter :: BSON.document()) :: boolean()
355+
@callback exists?(module :: module(), filter :: BSON.document(), opts :: Keyword.t()) :: boolean()
352356

353357
@doc """
354358
Applies the updates for the documents in the given collection module and filter.
@@ -417,7 +421,7 @@ defmodule Mongo.Repo do
417421
@callback fetch_by(module :: module(), query :: BSON.document(), opts :: Keyword.t()) ::
418422
{:ok, Mongo.Collection.t()} | {:error, :not_found} | {:error, any()}
419423

420-
@optional_callbacks insert: 2, insert!: 2, update: 1, update!: 1, insert_or_update: 1, insert_or_update!: 1, delete: 2, delete!: 2, insert_all: 3
424+
@optional_callbacks insert: 2, insert!: 2, update: 2, update!: 2, insert_or_update: 2, insert_or_update!: 2, delete: 2, delete!: 2, insert_all: 3
421425

422426
@doc """
423427
Inserts a new document struct into the database and returns a `{:ok, doc}` tuple.
@@ -443,12 +447,12 @@ defmodule Mongo.Repo do
443447
post = MyApp.Repo.insert!(Post.new())
444448
MyApp.Repo.update(%{post | title: "new"})
445449
"""
446-
@callback update(doc :: Mongo.Collection.t()) :: {:ok, Mongo.Collection.t()} | {:error, any()}
450+
@callback update(doc :: Mongo.Collection.t(), opts :: Keyword.t()) :: {:ok, Mongo.Collection.t()} | {:error, any()}
447451

448452
@doc """
449453
Same as `Mongo.Repo.update/1` but raises an error.
450454
"""
451-
@callback update!(doc :: Mongo.Collection.t()) :: Mongo.Collection.t()
455+
@callback update!(doc :: Mongo.Collection.t(), opts :: Keyword.t()) :: Mongo.Collection.t()
452456

453457
@doc """
454458
Upserts a document struct and returns a `{:ok, doc}` tuple.
@@ -457,12 +461,12 @@ defmodule Mongo.Repo do
457461
458462
MyApp.Repo.insert_or_update(Post.new())
459463
"""
460-
@callback insert_or_update(doc :: Mongo.Collection.t()) :: {:ok, Mongo.Collection.t()} | {:error, any()}
464+
@callback insert_or_update(doc :: Mongo.Collection.t(), opts :: Keyword.t()) :: {:ok, Mongo.Collection.t()} | {:error, any()}
461465

462466
@doc """
463467
Same as `Mongo.Repo.insert_or_update/1` but raises an error.
464468
"""
465-
@callback insert_or_update!(doc :: Mongo.Collection.t()) :: Mongo.Collection.t()
469+
@callback insert_or_update!(doc :: Mongo.Collection.t(), opts :: Keyword.t()) :: Mongo.Collection.t()
466470

467471
@doc """
468472
Deletes a document struct from the database and returns a `{:ok, doc}` tuple.

0 commit comments

Comments
 (0)