Skip to content

Commit f9bf727

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 7e97a84 + ce39de4 commit f9bf727

File tree

9 files changed

+955
-28
lines changed

9 files changed

+955
-28
lines changed

.credo.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@
122122
{Credo.Check.Refactor.Apply, []},
123123
{Credo.Check.Refactor.CondStatements, []},
124124
{Credo.Check.Refactor.FunctionArity, []},
125-
{Credo.Check.Refactor.LongQuoteBlocks, []},
126125
{Credo.Check.Refactor.MatchInCondition, []},
127126
{Credo.Check.Refactor.MapJoin, []},
128127
{Credo.Check.Refactor.NegatedConditionsInUnless, []},

README.md

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ it will be written to database, as:
100100

101101
While using the `Mongo.Encoder` protocol give you the possibility to encode your structs into maps the opposite way to decode those maps into structs is missing. To handle it you can use the `Mongo.Collection` which provides some boilerplate code for a better support of structs while using the MongoDB driver
102102

103-
* automatic load and dump function
104-
* reflection functions
105-
* type specification
106-
* support for embedding one and many structs
107-
* support for `after load` function
108-
* support for `before dump` function
109-
* support for id generation
110-
* support for default values
111-
* support for derived values
103+
- automatic load and dump function
104+
- reflection functions
105+
- type specification
106+
- support for embedding one and many structs
107+
- support for `after load` function
108+
- support for `before dump` function
109+
- support for id generation
110+
- support for default values
111+
- support for derived values
112112

113113
When using the MongoDB driver only maps and keyword lists are used to represent documents.
114114
If you would prefer to use structs instead of the maps to give the document a stronger meaning or to emphasize
@@ -141,11 +141,12 @@ converts the atom keys into strings (Or use the `Mongo.Encode` protocol)
141141
iex> Map.drop(label, [:__struct__])
142142
%{color: :red, name: "warning"}
143143
```
144+
144145
If you use nested structures, the work becomes a bit more complex. In this case, you have to use the inner structures
145146
convert manually, too. If you take a closer look at the necessary work, two basic functions can be derived:
146147

147-
* `load` Conversion of the map into a struct.
148-
* `dump` Conversion of the struct into a map.
148+
- `load` Conversion of the map into a struct.
149+
- `dump` Conversion of the struct into a map.
149150

150151
`Mongo.Collection` provides the necessary macros to automate this boilerplate code. The above example can be rewritten as follows:
151152

@@ -159,6 +160,7 @@ defmodule Label do
159160
end
160161
end
161162
```
163+
162164
This results in the following module:
163165

164166
```elixir
@@ -197,6 +199,7 @@ iex(1)> m = %{"color" => :red, "name" => "warning"}
197199
iex(2)> Label.load(m)
198200
%Label{color: :red, name: "warning"}
199201
```
202+
200203
If you would now expect atoms as keys, the result of the conversion is not correct in this case:
201204

202205
```elixir
@@ -274,15 +277,54 @@ Failing operations return a `{:error, error}` tuple where `error` is a
274277
}}
275278
```
276279

280+
### Using the Repo Module
281+
282+
For convenience you can also `use` the `Mongo.Repo` module in your application to configure the MongoDB application.
283+
284+
Simply create a new module and include the `use Mongo.Repo` macro:
285+
286+
```elixir
287+
defmodule MyApp.Repo do
288+
use Mongo.Repo,
289+
otp_app: :my_app,
290+
topology: :mongo
291+
end
292+
```
293+
294+
To configure the MongoDB add the configuration to your `config.exs`:
295+
296+
```elixir
297+
config :my_app, MyApp.Repo,
298+
url: "mongodb://localhost:27017/my-app-dev",
299+
timeout: 60_000,
300+
idle_interval: 10_000,
301+
queue_target: 5_000
302+
```
303+
304+
Finally we can add the `Mongo` instance to our application supervision tree:
305+
306+
```elixir
307+
children = [
308+
# ...
309+
{Mongo, MyApp.Repo.config()},
310+
# ...
311+
]
312+
```
313+
314+
In addition the the convenient configuration, the `Mongo.Repo` module will also include query functions to use with your
315+
`Mongo.Collection` modules.
316+
317+
For more information check out the `Mongo.Repo` module documentation and the `Mongo` module documentation.
318+
277319
### Logging
278320

279-
You config the logging output by adding in your config file this line
321+
You config the logging output by adding in your config file this line
280322

281323
```elixir
282324
config :mongodb_driver, log: true
283325
```
284326

285-
The attribute `log` supports `true`, `false` or a log level like `:info`. The default value is `false`. If you turn
327+
The attribute `log` supports `true`, `false` or a log level like `:info`. The default value is `false`. If you turn
286328
logging on, then you will see log output (command, collection, parameters):
287329

288330
```

config/config.exs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,4 @@ config :logger, :console,
1717
config :mongodb_driver,
1818
log: true
1919

20-
# Sample configuration:
21-
#
22-
# config :logger, :console,
23-
# level: :info,
24-
# format: "$date $time [$level] $metadata$message\n",
25-
# metadata: [:user_id]
26-
27-
# It is also possible to import configuration files, relative to this
28-
# directory. For example, you can emulate configuration per environment
29-
# by uncommenting the line below and defining dev.exs, test.exs and such.
30-
# Configuration from the imported file will override the ones defined
31-
# here (which is why it is important to import them last).
32-
#
33-
# import_config "#{Mix.env}.exs"
20+
import_config "#{Mix.env()}.exs"

config/dev.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

config/prod.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

config/test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Config
2+
3+
config :mongodb_driver, Mongo.RepoTest.MyRepo,
4+
url: "mongodb://127.0.0.1:27017/mongodb_test",
5+
show_sensitive_data_on_connection_error: true
6+
7+
config :mongodb_driver,
8+
log: false

lib/mongo/collection.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ defmodule Mongo.Collection do
361361
362362
"""
363363

364+
@type t() :: struct()
365+
364366
alias Mongo.Collection
365367

366368
@doc false

0 commit comments

Comments
 (0)