Phoenix Live Generator

Learn how to generate code using Phoenix Live Generator.

The Phoenix Live generator is a utility that generates code supporting full CRUD functionality for a given resource. This includes the backend schema and context code, as well as the frontend code including routes, LiveView, and templates. Before we dive into using the generator, it’s worth discussing just what’s so great about this generated code in the first place.

Phoenix Live generator provides us with a quick and easy way to build CRUD features, taking over the often tedious and repetitive work of building out this common functionality. It does so in a way that is adherent to best practices for organizing Phoenix code in general, and LiveView code specifically. This makes it easy for developers to build on top of—and customize—the generated code. The Phoenix Live generator is just one of many reasons why Phoenix and LiveView developers can be so highly productive.

Now that you understand what the Phoenix Live generator is and what it does for us at a high level, we’re ready to use it.

Let’s get started.

How to run the Phoenix live generator

We’ll use the Phoenix Live generator to create a feature for managing products. If you are familiar with web development with Phoenix or even other languages, you know that many web libraries and frameworks have a concept called a resource. A resource is a collection of like entities. In this case, Product will be our resource.

Running the generator will give us all of the code needed to support the CRUD interactions for this resource. The generated frontend code, including the LiveViews, will reside in pento/lib/pento_web. Backend code, on the other hand, will live in pento/lib/pento. It will deal with database interactions with the schema and provide an API through which to manage those interactions, called the context.

When we’re done, we’ll have a schema for a Product, a Catalog context, along with the LiveViews for managing a product. As this figure demonstrates, all of these pieces of generated code will work together to make up the CRUD interactions for the Product resource.

Get hands-on with 1400+ tech skills courses.