Using Catalyst::Plugin::AutoCRUD: Tutorial with Examples for Newbies

  • Introduction
  • Don't Blink!
  • As Simple As That!
  • What Next?

Introduction

This is the third in a series of Catalyst tutorials for Newbies, the first two were:

In the first two tutorials we use jqGrid to display and edit a MySQL or SQLite table using 1) Catalyst::View::JSON, 2) Catalyst::Controller::REST and Catalyst::View::JSON, and 3) Catalyst::Controller::REST without the JSON view. We use Catalyst::TraitFor::Controller::jQuery::jqGrid to make our work easier.

In this third tutorial we will use Catalyst::Plugin::AutoCRUD to create a grid to display our MySQL or SQLite table. There really is nothing to it, it is so quick and easy you'll miss it if you blink your eyes!

Don't Blink!

From the Catalyst::Plugin::AutoCRUD docs:

You have a database, and wish to have a basic web interface supporting Create, Retrieve, Update, Delete and Search, with little effort. This module is able to create such interfaces on the fly.

If you already have a Catalyst app with DBIx::Class models configured:

use Catalyst qw(AutoCRUD); # <-- add the plugin name here in MyApp.pm

Now load your app in a web browser, but add /autocrud to the URL path.

As Simple As That!

Configure Catalyst::Plugin::AutoCRUD in the application class of your DBIx::Class configured application and point your browser to /autocrud, as simple as that:

MyFirstGrid/lib/MyFirstGrid.pm:

package MyFirstGrid;
use Moose;
use namespace::autoclean;

use Catalyst::Runtime 5.80;

use Catalyst qw/
    -Debug
    ConfigLoader
    Static::Simple
    AutoCRUD 
/;

extends 'Catalyst';

our $VERSION = '0.01';

__PACKAGE__->config(
    name => 'MyFirstGrid',
    # Disable deprecated behavior needed by old applications
    disable_component_resolution_regex_fallback => 1,
    enable_catalyst_header => 1, # Send X-Catalyst header
);

# Specify which stash keys are exposed as a JSON response. 
__PACKAGE__->config({
  'View::JSON' => {
    expose_stash => qw(json_data)
  }
});

# Start the application
__PACKAGE__->setup();

1;

Run it:

[hack@hackpad MyFirstGrid]$ ./script/myfirstgrid_server.pl -d -r -k

Go here:

http://0.0.0.0:3000/autocrud

There is no need for me to tell you more about Catalyst::Plugin::AutoCRUD because it has already been said. Here is a good place to start:

AutoCRUD supports multiple DBIC Schemas, and it will automatically provide you with a list to let you pick which one to work with. After that, you choose a Result class, and you have access to an extensive AJAX-enhanched database admin tool. You can search and browse data, as well as edit it and add new rows easily. AutoCRUD also understands your DBIC relationships, so you can easily see data related to the current rows.

Like it’s predecessors, I do not recommend trying to make this tool into a generic 'allweb'-application. However, if you use it for what it is, you can save countless development hours making trivial admin tools. Since it uses your DBIC Schema, you’ll also get the advantage of keeping your business logic in the data model. Things like DBIC timestamps will Just Work.

A more in-depth yet easy to follow discussion:

Some helper scripts which accompany Catalyst modules will generate CRUD code, subroutine stubs, package templates, and so on. You can use these as a starting point from which to grow your application, and the term for this is scaffolding.

In the Python world there's a plugin for their Django web framework which is a bit like scaffold helper scripts on steroids. It generates a fully working web interface for manipulating back-end data.

Catalyst's AutoCRUD plugin does something similar. After installing the plugin you have a new URL path at /autocrud from which you can access and edit any data accessible through the app Models. However unlike Django's plugin, this is all done on the fly, there are no scaffolding files written to disk. To give you an idea of what's generated, head over to the demo site at http://demo.autocrud.pl.

That doesn't mean you can't control what AutoCRUD produces. This article will look at a few ways to tailor the appearance of the Catalyst::Plugin::AutoCRUD products.

We started with a simple idea - producing a simple user interface allowing Create, Search, Update and Delete on your back-end data. You can start with scaffolding files produced by helper scripts, or with a plugin like AutoCRUD very quickly have an app which does the same, all on the fly.

Which way you go is your choice. There's a trade-off between effort, and flexibility and power. AutoCRUD requires little effort and delivers a lot, and is even configurable to some degree. But maybe there comes a point when your app grows and you need more.

Documentation:

This module contains an application which will automatically construct a web interface for a database on the fly. The web interface supports Create, Retrieve, Update, Delete and Search operations.

The interface is not written to static files on your system, and uses AJAX to act upon the database without reloading your web page (much like other Web 2.0 applications, for example Google Mail).

Almost all the information required by the plugin is retrieved from the DBIx::Class ORM frontend to your database, which it is expected that you have already set up. This means that any change in database schema ought to be reflected immediately in the web interface after a page refresh.

Scenario 1: Plugin to an existing Catalyst App

This mode is for when you have written your Catalyst application, but the Views are catering for the users and as an admin you'd like a more direct, secondary web interface to the database.

Scenario 2: Frontend for an existing DBIx::Class::Schema based class

In this mode, Catalyst::Plugin::AutoCRUD is running standalone, in a sense as the Catalyst application itself.

Scenario 3: Lazy loading a DBIx::Class schema

If you're in such a hurry that you can't create the DBIx::Class schema, then Catalyst::Plugin::AutoCRUD is able to do this on the fly, but it will slow the application's startup just a little.

SEE ALSO:

CatalystX::CRUD and CatalystX::CRUD::YUI are two distributions which allow you to create something similar but with full customization, and the ability to add more features. So, you trade effort for flexibility and power.

What Next?

Suggestions?

Leave a comment

About j0e

user-pic I am a Perl Catalyst Newbie who is documenting the creation and deployment of yardbirdfanclub.org.