elixir

In elixir, How do i install packages globally?

家住魔仙堡 提交于 2019-12-08 15:13:12
问题 Can I use mix to install some packages globally? I'd like a behaviour like npm 's global option or gem 's install - it could be useful for packages I use everywhere like csv or yaml . 回答1: There is no such thing in Elixir, you always use dependencies in the context of a project. Solutions like archives or escripts are meant to solve specific problems, they do not allow package sharing between projects. However, there is no need to worry about sharing frequently used packages. Hex, the package

How to use connection with session in phoenix?

為{幸葍}努か 提交于 2019-12-08 15:05:34
问题 I have an authentication plug and I want to test my controllers. The problem is that the line in this plug has user_id = get_session(conn, :user_id) And it's always nil when I'm using this method (I used dirty hack before, but I no longer want to do it): @session Plug.Session.init([ store: :cookie, key: "_app", encryption_salt: "secret", signing_salt: "secret", encrypt: false ]) user = MyApp.Factory.create(:user) conn() |> put_req_header("accept", "application/vnd.api+json") |> put_req_header

Kubernetes cluster and Phoenix - automate `mix ecto.migrate`?

徘徊边缘 提交于 2019-12-08 05:11:15
问题 I'm pushing my Phoenix app to a Kubernetes cluster for review. I use GitLab to create a service for the web server and another service for a temporary postgres pod. What I would like to do is automate mix ecto.create and mix ecto.migrate . However, there is a timing issue - there's a short period of time when the postgres server is not ready yet. I could poll the postgres service in my deployment script before creating the web application service. But is this the most practical method? 回答1:

How to execute elixir code in a js file?

血红的双手。 提交于 2019-12-08 04:48:15
问题 I have a js "my_js_file.js" file which I include only into a single page. In that js file I need to get an url by calling a phoenix helper: # my_js_file.js function func1(userLogin) { var createUserUlr = "<%= user_home_path(@conn, :create, userLogin) %>"; //........... But it doesn't work. When I rename it to "my_js_file.js.eex", it still doesn't work -- the value between "<%= %>" isn't getting evaluated. Why not? And what are the options? Note that I'm passing a variable to the url. 回答1: I

Phoenix - return Ecto query results to a specific client

醉酒当歌 提交于 2019-12-08 02:43:46
问题 I'm currently trying to devise a scheme where the following happens. Client A is subscribed/connected to topic/channel T . A sends a message in the form of a select query to T . Only A receives the query results, no other subscribers. Is this even possible using Channels? The main reason I chose Channels was for the excellent websocket support - however I'm open to other Phoenix solutions. 回答1: Yes, Channels should do the work you want. You can push the query results down to the user who sent

Is there a built-in mechanism for us to launch Mnesia in Elixir?

南笙酒味 提交于 2019-12-08 02:07:58
问题 It seems like that we can launch Mnesia only by typing iex --erl "--mnesia dir '~/doc/'" --name mynode . Can we just launch it without passing arguments to erl ? 回答1: You can set it up dynamically in your code. All the --erl instruction above does is to configure the mnesia application before it is started. You could achieve this by: # First load mnesia iex(1)> :application.load(:mnesia) :ok # Now configure the desired directory iex(2)> :application.set_env(:mnesia, :dir, 'sample') :ok #

Elixir rename and wrap Erlang module?

[亡魂溺海] 提交于 2019-12-07 16:48:44
问题 Is it possible to rename an existing Erlang module? I have some code in several Erlang modules that I want to use in an Elixir project (assertion library). I don't want to convert all the Erlang modules to Elixir as they are complete, but I want to rename them, and possible add additional functions to them. Both modules are simply collections of functions, they don't implement behaviors or do anything unusual. I want to be able to take an existing Erlang module: -module(foo_erl). -export(

elixir官方教程 元编程(一) 引用与去引用

旧街凉风 提交于 2019-12-07 15:53:24
#引用与去引用 引用(Quoting) 去引用(Unquoting) 释放(Escaping) 一个Elixir程序可以用它自己的数据结构来表现.本章,我们将会学习这些结构体的特点和如何组成它们.本章我们要学习的概念是为宏的积木(building blocks),在下一章中我们将深入研究它. #引用(Quoting) Elixir程序中的积木是一个三元素元组.例如,函数 sum(1, 2, 3) 的内部表示是: {:sum, [], [1, 2, 3]} 你可以用 quote 宏来得到任何表达式的内部表现: iex> quote do: sum(1, 2, 3) {:sum, [], [1, 2, 3]} 第一个元素是函数名,第二个元素是一个包含了元数据的关键词列表,第三个元素是参数列表. 操作符也可以用元组来表示: iex> quote do: 1 + 2 {:+, [context: Elixir, import: Kernel], [1, 2]} 甚至一个映射都被表示成对 %{} 的调用: iex> quote do: %{1 => 2} {:%{}, [], [{1, 2}]} 变量也能这样三段式表示,只不过最后的元素换成了一个原子: iex> quote do: x {:x, [], Elixir} 当引用更复杂的表达式时,代码被表示成了一个树状的嵌套元组

Elixir: How to test Phoenix Controllers when they need records in the database? With seeds or mocks?

对着背影说爱祢 提交于 2019-12-07 12:30:27
问题 When testing controllers it can be useful to have some data in the test database. And sometimes you might want to test the data creation. What is the correct way to set this up? A test for controllers should test if the create function of the controller is working, not the model. As an example I want to test a Session controller and I have two tests. One is testing that a created user can login. The other that he cannot if the password is wrong. Both rely on a user being in the database. How

Ecto: How to preload records with selecting another joined columns

不想你离开。 提交于 2019-12-07 12:23:10
问题 Are there any ways to preload records by selecting another joined columns? # table structure # User 1---* Post 1---* PostTag *---1 Tag # extract definition of scheme scheme "posts" do ... has_many :post_tags, PostTag has_many :tags, [:post_tags, :tag] end Following pseudo-code expresses my goal(but not work). query = from post in Post, join: user in User, on post.user_id == user.id, select: %{ id: post.id, title: post.title, user_name: user.name, # <= column at joined table }, preload: [:tags