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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New