dersar00
Save data to mongodb through code
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
Most Liked
kokolegorille
Friend is a module… it should contains functions You can call.
In your code…
defmodule Friends do
IO.puts "start"
person = %Friends.Person{}
Friends.Repo.insert(person)
IO.puts "saved"
end
it could be
defmodule Friends do
def save_a_new_person() do
IO.puts "start"
person = %Friends.Person{}
Friends.Repo.insert(person)
IO.puts "saved"
end
end
And then You can call it like this
iex> Friends.save_a_new_person()
Why would You expect your code in the module to run? or when?
idi527
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.
Last Post!
idi527
I want to connect to the database before my function start
Then you can run this function as a Task after supervisor(Repo, []) in your application.
Replace https://github.com/dersar00/friends/blob/master/lib/friends/application.ex with
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
Don’t forget to delete https://github.com/dersar00/friends/blob/master/lib/friends.ex#L10
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









