Skip to content

Commit 89f9228

Browse files
committed
chore: update README, add better error handling in the migration module
1 parent 01552eb commit 89f9228

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ While using the `Mongo.Encoder` protocol give you the possibility to encode your
214214
- support for id generation
215215
- support for default values
216216
- support for derived values
217+
- support for alias attribute names
218+
219+
But in the case of queries and updates, a rewrite of the attribute names does not take place. It is still up to you
220+
to use the correct attribute names.
217221

218222
When using the MongoDB driver only maps and keyword lists are used to represent documents.
219223
If you prefer to use structs instead of the maps to give the document a stronger meaning or to emphasize
@@ -313,11 +317,18 @@ iex(3)> Label.load(m, true)
313317
```
314318

315319
The background is that MongoDB always returns binarys as keys and structs use atoms as keys.
316-
317320
For more information look at the module documentation `Mongo.Collection`.
318-
319321
Of course, using the `Mongo.Collection` is not free. When loading and saving, the maps are converted into structures, which increases CPU usage somewhat. When it comes to speed, it is better to use the maps directly.
320322

323+
## Breaking changes
324+
325+
Prior to version 0.9.2 the dump function returns atoms as key. Since the dump function is the inverse function of load,
326+
which uses binary keys as default, the dump function should return binary keys as well. This increases the consistency and
327+
you can do:
328+
329+
l = Label.load(doc)
330+
doc = Label.dump(l)
331+
assert l == Label.load(doc)
321332

322333
## Using the Repo Module
323334

@@ -358,6 +369,35 @@ In addition, the convenient configuration, the `Mongo.Repo` module will also inc
358369

359370
For more information check out the `Mongo.Repo` module documentation and the `Mongo` module documentation.
360371

372+
## Breaking changes
373+
374+
Prior to version 0.9.2 some Repo functions use the dump function for the query parameter. This worked only
375+
for some query that used only the attributes of the document. In the case of nested documents, it didn't work, so
376+
it is changed to be more consistent. The Repo module is very simple without any query rewriting like Ecto does. In the case
377+
you want to use the `:name` option, you need to specify the query and updates in the Repo following
378+
the specification in the MongoDB. Example:
379+
380+
defmodule MyApp.Session do
381+
@moduledoc false
382+
use Mongo.Collection
383+
384+
alias BSON.Binary
385+
386+
collection :s do
387+
attribute :uuid, Binary.t(), name: :u
388+
end
389+
end
390+
391+
If you use the Repo module and want to fetch a specific session document, this won't work
392+
393+
MyApp.Repo.get_by(MyApp.Session, %{uuid: session_uuid})
394+
395+
because the `get_by/2` function uses the query parameter without any rewriting. You need to change the query:
396+
397+
MyApp.Repo.get_by(MyApp.Session, %{u: session_uuid})
398+
399+
A rewriting is too complex for now, because the MongoDB has a lot of options.
400+
361401
## Logging
362402

363403
You config the logging output by adding in your config file this line
@@ -606,6 +646,8 @@ you'll want to add this cipher to your `ssl_opts`:
606646
)
607647
```
608648

649+
See the example `AWSX509.Example` as well.
650+
609651
## Timeout
610652

611653
The `:timeout` option sets the maximum time that the caller is allowed to hold the connection’s state (to send and to receive data).

lib/mongo/migration.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ defmodule Mongo.Migration do
1111
unlock()
1212
end
1313
rescue
14-
_ ->
14+
error ->
15+
IO.puts("🚨 Error when migrating: #{inspect(error)}")
1516
unlock()
1617
end
1718

@@ -25,7 +26,8 @@ defmodule Mongo.Migration do
2526
unlock()
2627
end
2728
rescue
28-
_ ->
29+
error ->
30+
IO.puts("🚨 Error when dropping: #{inspect(error)}")
2931
unlock()
3032
end
3133

@@ -117,9 +119,11 @@ defmodule Mongo.Migration do
117119
end
118120

119121
defp migration_files!() do
122+
file_path = migration_file_path()
123+
120124
case File.ls(migration_file_path()) do
121125
{:ok, files} -> Enum.sort(files)
122-
{:error, _} -> raise "Could not find migrations file path"
126+
{:error, _error} -> raise "Could not find migrations file path #{inspect(file_path)}"
123127
end
124128
end
125129

lib/tasks/gen/migration.ex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ defmodule Mix.Tasks.Mongo.Gen.Migration do
66
import Macro, only: [camelize: 1, underscore: 1]
77
import Mix.Generator
88

9+
alias Mongo.Migration
10+
911
@shortdoc "Generates a new migration for Mongo"
1012

1113
@spec run([String.t()]) :: integer()
1214
def run(args) do
13-
migrations_path = migration_file_path()
15+
migrations_path = Migration.migration_file_path()
1416
name = List.first(args)
1517
base_name = "#{underscore(name)}.exs"
1618
current_timestamp = timestamp()
@@ -35,11 +37,6 @@ defmodule Mix.Tasks.Mongo.Gen.Migration do
3537
defp pad(i) when i < 10, do: <<?0, ?0 + i>>
3638
defp pad(i), do: to_string(i)
3739

38-
def migration_file_path() do
39-
path = Mongo.Migration.get_config()[:path]
40-
Path.join(["priv", path])
41-
end
42-
4340
embed_template(:migration, """
4441
defmodule <%= inspect @mod %> do
4542
def up() do

0 commit comments

Comments
 (0)