View Source mix flowy.gen.json (Flowy v0.1.4)

Generates controller, JSON view, and context for a JSON resource.

mix flowy.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.

The schema is responsible for mapping the database fields into an Elixir struct. It is followed by an optional list of attributes, with their respective names and types. See mix flowy.gen.schema for more information on attributes.

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

  • a core module in lib/app/core/users.ex for the accounts API
  • a schema in lib/app/schemas/user.ex, with an users table
  • a controller in lib/app_web/controllers/api/user_controller.ex
  • a JSON view collocated with the controller in lib/app_web/controllers/user_json.ex

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

API Prefix

By default, the prefix "/api" will be generated for API route paths. This can be customized via the :api_prefix generators configuration:

config :your_app, :generators,
  api_prefix: "/api/i1"

The context app

The location of the web files (controllers, json views, etc) in an umbrella application will vary based on the :context_app config located in your applications :generators configuration. When set, the Phoenix generators will generate web files directly in your lib and test folders since the application is assumed to be isolated to web specific functionality. If :context_app is not set, the generators will place web related lib and test files in a web/ directory since the application is assumed to be handling both web and domain specific functionality. Example configuration:

config :my_app_web, :generators, context_app: :my_app

Alternatively, the --context-app option may be supplied to the generator:

mix flowy.gen.json Sales User users --context-app warehouse

Customizing the context, schema, tables and migrations

In some cases, you may wish to bootstrap JSON views, controllers, and controller tests, but leave internal implementation of the context or schema to yourself. You can use the --no-core and --no-schema flags for file generation control.

You can also change the table name or configure the migrations to use binary ids for primary keys, see mix flowy.gen.schema for more information.