dersar00

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

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

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

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.

Where Next?

Popular in Questions Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement