Ecto with contexts

Hello everyone,

I am trying to mimic phoenix 1.3 contexts with ecto only.

With phoenix, I would do

$ mix phx.gen.json Accounts User users email:string encrypted_password:string

But with ecto only, I would use something like this

mix ecto.gen.migration add_users_table

I can do it manually by naming table with accounts namespace, but I would like to know what is the preferred method to manage contexts in ecto, without phoenix.

Thanks for taking time

Contexts are very little more than a module with functions that wraps all the operations you might need. The shift is that instead of calling Repo directly, you go through functions of the context module. There’s very little specific to Phoenix in this pattern, so it should be rather simple to replicate it.

1 Like

Yes, making it manually is not too difficult, I just have to correctly name fields and tables according to contexts I want to use.

Thank You for your reply

AFAIK in master (so probably also in the next rc or final release) table names are kept “simple”, so

$ mix phx.gen.json Accounts User users email:string encrypted_password:string

will generate a migration with “just” users table.

Reference: https://github.com/phoenixframework/phoenix/commit/c3351478748779fe0b777a4f41e7ace7ae5e2452

2 Likes

Ok, Thanks for this useful info.

This is an important change to take into consideration.