Hello.
I using Mongo.Ecto to mongodb connection, when I run in CLI commands:
person = %Friends.Person{}
Friends.Repo.insert(person)
I got result and a record is created. But with same commands in code I can’t do it. How do it?
Repository with full proj: https://github.com/dersar00/friends
I don’t want do it through iex because through iex it’s work well.
It’s a test project, I need to do it in my development, but I can’t share a main project.(settings is same)
As @kokolegorille has already pointed out, you shouldn’t run db commands outside of functions, since then they would be executed during compilation before the database connection process is started.
Put those commands in a function, and execute the function when you need to. It can be in response to a controller action or whatever else.
Also maybe read through the official elixir guide to get a better understanding of how elixir modules and functions work.
Currently you are trying to save your data during build time of your application.
This requires you to set up your database connection during build time manually and is probably not the thing you want. Especially not if this does not make sense to you.
But without knowing your exact architecture I think its hard to tell you how to do it properly.
But I assume you are doing a phoenix application here, so just call the save from your controller.
I doesn’t develop phoenix app.
My architecture is:
I build my project using a microservices architecture(rabbitmq), one of my worker need to be builded using elixir, when I get message from my queue, for example I want to save it to db, so how do it?
I do not use AMQP in elixir, but I do assume, that you have to either poll for messages one by one or register a callback or have to implement a behaviour to receive your messages.
Regardless how you receive your messages, in the code that is executed on receive you need to do your stuff.
defmodule Friends.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(Friends.Repo, []),
worker(Task, [&Friends.save/0], restart: :transient)
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Friends.Supervisor]
Supervisor.start_link(children, opts)
end
end
My architecture is:
I build my project using a microservices architecture(rabbitmq), one of my worker need to be builded using elixir, when I get message from my queue, for example I want to save it to db, so how do it?
You would call your function like Friends.save(data) from a handler which handles new messages from rabbitmq, just like a phoenix controller which handles new requests.
You wouldn’t need the worker(Task, [&Friends.save/0], restart: :transient) from above.