Hi @Gilou06 ,
@zachallaun already answered correctly to your (very common) doubt.
I just want to add that CubDB.start_link already starts a GenServer, which is why you can start it as a child of a Supervisor and/or give it a name.
If you give it a name, you can then use that name instead of the db variable:
{:ok, db} = CubDB.start_link(data_dir: "tmp/foo", name: :my_db)
# These two are now equivalent:
CubDB.get(db, "some-key")
CubDB.get(:my_db, "some-key")
As pointed out by @zachallaun , instead of manually calling start_link, you would probably add it to the list of children of your application supervisor. Then, the supervisor will call start_link for you, and you will still be able to refer to the CubDB process by name.
Using a name instead of the pid is usually better when running a process under supervision, because the supervisor might restart the process in case of a crash: then, the old pid won’t be valid anymore, but the name will still be valid (and refer to the new process).
These things are not obvious when starting with Elixir, and take some time and some thinking to get used to. Unfortunately, we often give them for granted in docs, where we simply say {:ok, pid} = MyProcess.start_link(...) - which works well in the console, but is not the way processes are usually started in a real application - and assume the reader knows what to do. I will try to improve CubDB docs on this aspect.






















