Phoenix v1.3.0-rc.1 Mix.Tasks.Phx.Gen.Json

Generates controller, views, and context for an JSON resource.

mix phx.gen.json Accounts User users name:string age:integer

The first argument is the context module followed by the schema module and its plural name (used as the schema table name).

The context is an Elixir module that serves as an API boundary for the given resource. A context often holds many related resources. Therefore, if the context already exists, it will be augmented with functions for the given resource. Note a resource may also be split over distinct contexts (such as Accounts.User and Payments.User).

The schema is responsible for mapping the database fields into an Elixir struct.

Overall, this generator will add the following files to lib/your_app:

  • a context module in accounts/accounts.ex, serving as the API boundary
  • a schema in accounts/user.ex, with an accounts_users table
  • a view in web/views/user_view.ex
  • a controller in web/controllers/user_controller.ex

A migration file for the repository and test files for the context and controller features will also be generated.

table

By default, the table name for the migration and schema will be the plural name provided for the resource. To customize this value, a --table option may be provided. For example:

mix phx.gen.json Accounts User users --table cms_users

binary_id

Generated migration can use binary_id for schema’s primary key and its references with option --binary-id.

Default options

This generator uses default options provided in the :generators configuration of your application. These are the defaults:

config :your_app, :generators,
  migration: true,
  binary_id: false,
  sample_binary_id: "11111111-1111-1111-1111-111111111111"

You can override those options per invocation by providing corresponding switches, e.g. --no-binary-id to use normal ids despite the default configuration or --migration to force generation of the migration.

Read the documentation for phx.gen.schema for more information on attributes.

Summary

Functions

A task needs to implement run which receives a list of command line args

Functions

copy_new_files(context, paths, binding)
run(args)

A task needs to implement run which receives a list of command line args.

Callback implementation for Mix.Task.run/1.