-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add MongoDB database option to Phoenix installer #6710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| services: | ||
| mongodb: | ||
| image: mongo:latest | ||
| command: ["--replSet", "rs0", "--bind_ip_all"] | ||
| ports: | ||
| - "27017:27017" | ||
| environment: | ||
| MONGO_INITDB_DATABASE: <%= @app_name %>_dev | ||
| healthcheck: | ||
| test: > | ||
| mongosh --quiet --eval | ||
| 'try { rs.status() } catch(e) { rs.initiate({_id:"rs0",members:[{_id:0,host:"127.0.0.1:27017"}]}) }' | ||
| interval: 5s | ||
| timeout: 10s | ||
| retries: 5 | ||
| start_period: 5s |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ## MongoDB | ||
|
|
||
| When the project uses `--database mongo` (adapter: `Mongo.Ecto`): | ||
|
|
||
| - **Primary keys must be `:binary_id`** — configured automatically via `config :app, :generators, binary_id: true`. All `mix phx.gen.*` commands honour this automatically; no `--binary-id` flag needed. | ||
| - **No SQL joins** — use embedded schemas (`embeds_one`, `embeds_many`) for nested data, or separate queries for associations. | ||
| - **Migrations create collections and indexes**, not tables. `create table(:name)` → MongoDB collection. `add :col, :type` → no-op (schema-less). | ||
| - **Test isolation** uses `Mongo.Ecto.truncate(Repo)` before each test, not `Ecto.Adapters.SQL.Sandbox`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. José recently cleanup agents rules, and one of the updates was to simply improve the scaffolded test setup replacing an agent rule. I think we could do the same here: scaffold the project with tests setup correctly, no need to instruct agents. |
||
| - **Connection string** uses `mongo_url:` config key. In production, set `DATABASE_URL` env var to a MongoDB URI (`mongodb://...` or `mongodb+srv://...`). | ||
| - **Atlas** — swap `DATABASE_URL` to your Atlas connection string; no code changes needed. | ||
| - `:decimal` type is not supported by `mongodb_ecto`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,17 +176,27 @@ | |
| If the token is valid `{<%= schema.singular %>, token_inserted_at}` is returned, otherwise `nil` is returned. | ||
| """ | ||
| def get_<%= schema.singular %>_by_session_token(token) do | ||
| {:ok, query} = <%= inspect schema.alias %>Token.verify_session_token_query(token) | ||
| Repo.one(query) | ||
| {:ok, query} = <%= inspect schema.alias %>Token.verify_session_token_query(token)<%= if mongo_adapter? do %> | ||
| case Repo.one(query) do | ||
| nil -> nil | ||
| token_record -> | ||
| <%= schema.singular %> = Repo.get(<%= inspect schema.alias %>, token_record.<%= schema.singular %>_id) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, noticing this does 2 queries for getting a session token. Perhaps because of the "no joins" limitation? Considering there is no transaction here, I think we need to understand the security implications of that data race before promoting it. |
||
| <%= schema.singular %> && {%{<%= schema.singular %> | authenticated_at: token_record.authenticated_at}, token_record.inserted_at} | ||
| end<% else %> | ||
| Repo.one(query)<% end %> | ||
| end | ||
|
|
||
| @doc """ | ||
| Gets the <%= schema.singular %> with the given magic link token. | ||
| """ | ||
| def get_<%= schema.singular %>_by_magic_link_token(token) do | ||
| with {:ok, query} <- <%= inspect schema.alias %>Token.verify_magic_link_token_query(token), | ||
| with {:ok, query} <- <%= inspect schema.alias %>Token.verify_magic_link_token_query(token),<%= if mongo_adapter? do %> | ||
| %<%= inspect schema.alias %>Token{} = token_record <- Repo.one(query), | ||
| %<%= inspect schema.alias %>{} = <%= schema.singular %> <- Repo.get(<%= inspect schema.alias %>, token_record.<%= schema.singular %>_id), | ||
| true <- <%= schema.singular %>.email == token_record.sent_to do | ||
| <%= schema.singular %><% else %> | ||
| {<%= schema.singular %>, _token} <- Repo.one(query) do | ||
| <%= schema.singular %> | ||
| <%= schema.singular %><% end %> | ||
| else | ||
| _ -> nil | ||
| end | ||
|
|
@@ -211,7 +221,33 @@ | |
| `mix help phx.gen.auth`. | ||
| """ | ||
| def login_<%= schema.singular %>_by_magic_link(token) do | ||
| {:ok, query} = <%= inspect schema.alias %>Token.verify_magic_link_token_query(token) | ||
| {:ok, query} = <%= inspect schema.alias %>Token.verify_magic_link_token_query(token)<%= if mongo_adapter? do %> | ||
|
|
||
| with %<%= inspect schema.alias %>Token{} = token_record <- Repo.one(query), | ||
| %<%= inspect schema.alias %>{} = <%= schema.singular %> <- Repo.get(<%= inspect schema.alias %>, token_record.<%= schema.singular %>_id), | ||
| true <- <%= schema.singular %>.email == token_record.sent_to do | ||
| # Prevent session fixation attacks by disallowing magic links for unconfirmed users with password | ||
| if is_nil(<%= schema.singular %>.confirmed_at) and not is_nil(<%= schema.singular %>.hashed_password) do | ||
| raise """ | ||
| magic link log in is not allowed for unconfirmed users with a password set! | ||
|
|
||
| This cannot happen with the default implementation, which indicates that you | ||
| might have adapted the code to a different use case. Please make sure to read the | ||
| "Mixing magic link and password registration" section of `mix help phx.gen.auth`. | ||
| """ | ||
| end | ||
|
|
||
| if is_nil(<%= schema.singular %>.confirmed_at) do | ||
| <%= schema.singular %> | ||
| |> <%= inspect schema.alias %>.confirm_changeset() | ||
| |> update_<%= schema.singular %>_and_delete_all_tokens() | ||
| else | ||
| Repo.delete!(token_record) | ||
| {:ok, {<%= schema.singular %>, []}} | ||
| end | ||
| else | ||
| _ -> {:error, :not_found} | ||
| end<% else %> | ||
|
|
||
| case Repo.one(query) do | ||
| # Prevent session fixation attacks by disallowing magic links for unconfirmed users with password | ||
|
|
@@ -235,7 +271,7 @@ | |
|
|
||
| nil -> | ||
| {:error, :not_found} | ||
| end | ||
| end<% end %> | ||
| end | ||
|
|
||
| @doc ~S""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't generate a
docker-compose.ymlfile for any other choice of database.I'd expect adding MongoDB wouldn't change that -- it's on the end user to decide how they organize their dev environment (e.g. local MongoDB, Docker container, or external server).