From 67bd75f093b32e0178e8bd10ebd0b7c456760ead Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Fri, 3 May 2024 14:35:31 +0200 Subject: [PATCH] Implement Mongo.Repo.child_spec/1 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. --- README.md | 4 ++-- lib/mongo/repo.ex | 8 ++++++-- test/mongo/repo_test.exs | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index de41a1c4..e68d08ac 100644 --- a/README.md +++ b/README.md @@ -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, # ... ] ``` diff --git a/lib/mongo/repo.ex b/lib/mongo/repo.ex index 1b4d007b..8e45e310 100644 --- a/lib/mongo/repo.ex +++ b/lib/mongo/repo.ex @@ -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, # ... ] @@ -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) diff --git a/test/mongo/repo_test.exs b/test/mongo/repo_test.exs index de9f2995..396157bb 100644 --- a/test/mongo/repo_test.exs +++ b/test/mongo/repo_test.exs @@ -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