Skip to content

Commit

Permalink
Implement Mongo.Repo.child_spec/1
Browse files Browse the repository at this point in the history
To make it more convenient to add to an application's supervision tree,
similar to how Ecto.Repo-based modules are typically added to the
supervision tree.
  • Loading branch information
rhcarvalho committed May 3, 2024
1 parent 6dc6416 commit 67bd75f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,12 @@ config :my_app, MyApp.Repo,
queue_target: 5_000
```

Finally, we can add the `Mongo` instance to our application supervision tree:
Finally, we can add the `Mongo.Repo` instance to our application supervision tree:

```elixir
children = [
# ...
{Mongo, MyApp.Repo.config()},
MyApp.Repo,
# ...
]
```
Expand Down
8 changes: 6 additions & 2 deletions lib/mongo/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ defmodule Mongo.Repo do
For a complete list of configuration options take a look at `Mongo`.
Finally we can add the `Mongo` instance to our application supervision tree
Finally we can add the `Mongo.Repo` instance to our application supervision tree
children = [
# ...
{Mongo, MyApp.Repo.config()},
MyApp.Repo,
# ...
]
Expand Down Expand Up @@ -60,6 +60,10 @@ defmodule Mongo.Repo do
|> Keyword.put_new(:name, @topology)
end

def child_spec(_opts) do
Mongo.child_spec(config())
end

unless @read_only do
def insert(%{__struct__: module} = doc, opts \\ []) do
collection = module.__collection__(:collection)
Expand Down
2 changes: 1 addition & 1 deletion test/mongo/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Mongo.RepoTest do
end

setup do
assert {:ok, pid} = start_supervised({Mongo, MyRepo.config()})
assert {:ok, pid} = start_supervised(MyRepo)
Mongo.drop_database(pid, nil, w: 3)
{:ok, [pid: pid]}
end
Expand Down

0 comments on commit 67bd75f

Please sign in to comment.