On the Rails app, we can print the record count of a database table by a command like this:
rails r "puts User.count"
With my knowledge, if we want the same thing on the Phoenix/Ecto environment, we should run such a command:
mix run -e "IO.puts SampleApp.Repo.aggregate(SampleApp.User, :count, :id)"
That is OK, but is too lengthy for a daily usage. Is there any shorter way?
Out of the box, there is not.
You can however define a function in the SampleApp
module to make it shorter. Something like this should do the trick (I haven't tested this):
defmodule SampleApp do
...
def count(model)
IO.puts Repo.aggregate(model, :count, :id)
end
end
Then you can do this:
mix run -e "SampleApp.count(SampleApp.User)"