Mesoderm - Schema class scaffold generator for DBIx::Class
version 0.131260
use Mesoderm; use SQL::Translator; use DBI; my $dbh = DBI->connect($dsn, $user, $pass); my $sqlt = SQL::Translator->new(dbh => $dbh, from => 'DBI'); $sqlt->parse(undef); my $scaffold = Mesoderm->new( schema => $sqlt->schema, schema_class => 'My::Schema', ); $scaffold->produce(\*STDOUT);
Mesoderm creates a scaffold of code for DBIx::Class using a schema object from SQL::Translator. At time of writing the version of SQL::Translator required is not available on CPAN and must be fetched directly from github.
Mesoderm
The result is a hierarchy of packages describes below. Moose is used so that any custom methods needed to be added to the result or resultset classes can be done by writing Moose::Role classes. This allows separation between generated code and written code.
Mesoderm defines methods to map table names to class names, relationships and columns to accessor methods. It is also possible to have any table, relationship or column excluded from the generated model. If the defaults do not meet your needs, then it is trvial to subclass Mesoderm and provide overrides.
Given a schema_class name of Schema and a schema containing a single table foo_bars the following packages would be created or searched for with the default settings.
schema_class
Schema
foo_bars
Top level schema class. The user needs to provide this themselves. See "Example Schema Class".
The main generated package that will be a Moose::Role to be consumed into the top level schema class. See "The _scaffold Role"
Although the model generated is a hierarchy of packages, it is expected that all generated code be in one file loaded as Schema::_scaffold. This file contains all the generated code and should never be modified.
A subclass of DBIx::Class::Schema that will be used to register the generated classes.
Schema::FooBar will be the result class for the table foo_bars
During scaffolding Module::Pluggable will be used to search for Schema::Role::FooBar, which should be a Moose::Role class. If it exists then it will be consumed into Schema::FooBar.
Schema::ResultSet::FooBar is the resultset class for the table foo_bars.
During scaffolding Module::Pluggable will be used to search for Schema::ResultSet::Role::FooBar, which should be a Moose::Role class. If it exists then it will be consumed into Schema::ResultSet::FooBar.
The _scaffold will define methods for each resultset. In our example above it will define a method foo_bar.
foo_bar
It also has a method dbic which will return the DBIx::Class::Schema object.
dbic
The minimum requirement for a schema class is that it providers a method connect_args. The result of calling this method will be passed to the connect method of DBIx::Class::Schema.
connect_args
package Schema; use Moose; with 'Schema::_scaffold'; sub connect_args { return @args_for_dbix_class_connect; } 1;
Some other useful additions
# delegate txn_* methods to the DBIx::Class object itself has '+dbic' => (handles => [qw(txn_do txn_scope_guard txn_begin txn_commit txn_rollback)]); # Fetch a DBI handle sub dbh { shift->dbic->storage->dbh; }
With our example schema, searching of the foo_bars table would be done with
my $schema = Schema->new; $schema->foo_bar->search({id => 27});
Required. A SQL::Translator::Object::Schema object that the scaffolding will be generated from.
Required. Package name that the scaffold will be generated for. The actual package created will be a Moose::Role with the named schema_class plus ::_scaffold
::_scaffold
Name of method to generate that when called on any result row or result set will return the parent Mesoderm schema object. Defaults to schema
schema
Optional. Namespace used by default to prefix package names generated for DBIx::Class result classes. Defaults to schema_class
Optional. Namespace used by default to prefix package names generated for DBIx::Class result set classes. Defaults to result_class_namespace plus ::ResultSet
result_class_namespace
::ResultSet
Optional. Namespace that will be searched for, during scaffolding, for roles to add to result classes. The generated code will include with statements for any role that is found during scaffolding. Defaults to result_class_namespace plus ::Role
with
::Role
Optional. Namespace that will be searched for, during scaffolding, for roles to add to result set classes. The generated code will include with statements for any role that is found during scaffolding. Defaults to resultset_class_namespace plus ::Role
resultset_class_namespace
Returns a list of DBIx::Class components to be loaded by the resultset class
Returns a list of DBIx::Class components to be loaded by the result class
Returns a list of Moose::Role classes to be comsumed into the result class Default is to join result_role_namespace with table_class_element, if the module can be found by Module::Pluggable
Returns a list of Moose::Role classes to be comsumed into the result class. Default is to join resultset_role_namespace with table_class_element, if the module can be found by Module::Pluggable
Returns a hash reference which will be serialized as the arguments passed to add_column
add_column
Provides a hook to allow inserting objects to have default values set on columns if no value has been specified. It should return valid perl code that will be inserted into the generated code and will be evaluated in a scalar context
Return a boolean to determine if the passed object should be excluded from the generated model. Default: 0
Returns name for a relationship. Default is to call the method based on the relationship type.
Return relationship accessor name. Default is to call to_singlular or to_plural with the name for the foreign table. Which is called depends on the arity of the relationship
Return the accessor name for the column. Default it to return the column name.
Return name for the result class. Default is to join result_class_namespace with table_class_element
Return name for the resultset class. Default is to join resultset_class_namespace with table_class_element
Return moniker used to register result class with DBIx::Class::Schema. Default is to call to_singular with the lowercase table name
Return package name element that will be prefixed with result_class_namespace, resultset_class_namespace, result_role_namespace and resultset_role_namespace to generate class names. Default takes the table_moniker and title-cases based on _ as a word separator
_
Utility method to return singular form of $word. Default implementation uses "to_S" in Lingua::EN::Inflect::Number
$word
Utility method to return plural form of $word. Default implementation uses "to_PL" in Lingua::EN::Inflect::Number
Create a relatonship which is the opposite of the given relationship.
Return boolean to indicate if the table is a mapping table and many to many mapping relationships need to be created
Generate code and write to filehandle
Build a Mesoderm::Relationship object given a constraint
Build a Mesoderm::Mapping given relationship for a mant to many mapping
DBIx::Class, Moose, Moose::Role, SQL::Translator
At time of writing the version required is not available on CPAN and needs to be fetched from github. http://github.com/arcanez/SQL-Translator
Graham Barr <gbarr@cpan.org>
This software is copyright (c) 2010 by Graham Barr.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Mesoderm, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mesoderm
CPAN shell
perl -MCPAN -e shell install Mesoderm
For more information on module installation, please visit the detailed CPAN module installation guide.