Giter VIP home page Giter VIP logo

norma's Introduction

Norma - easy, limited, Moose-based ORM for the unafraid

INTRODUCTION

Norma provides a "Mappable" role to compose into your Moose-based modules.  
When you compose the role into a class, you map that class to a table in the
database.  You can also specify relationships.  With the role composed, columns
become attributes, and you have methods available to read and write to that 
table.  

Schema management is left to you.  You create your tables on your own, and 
Norma will discover what you've done and adapt accordingly.

COMPOSING THE ROLE

Here's an example where we compose the role into a class to represent recipes.  
The role takes a table name and a database handle at a minimum.

  # create a "recipes" table with an id, title, etc
  mysql> CREATE TABLE recipes (id int primary key auto_increment, title ...)

  # create a class to represent a recipe
  package MyApp::Recipe;

  use Moose;
  with 'Norma::ORM::Mappable' => {
      table_name => 'recipes',
      dbh => $dbh,
  };
  
READING AND WRITING 

Now we have access to CRUD operations on the table.  Here we insert a row with 
a call to save() after instantiating a new object:

  my $recipe = MyApp::Recipe->new(
      title => "Scrambled Eggs",
      instructions => "Break two eggs into a bowl...",
      ...
  );
  $recipe->save;

To instantiate an object from an existing row, use load():

  my $recipe = MyApp::Recipe->load( id => 1 );

Update a row by setting attributes and calling save():

  $recipe->title("Delicious Scrambled Eggs");
  $recipe->save;

Delete a row by calling delete() on an instantiated object

  $recipe->delete

QUERYING FOR COLLECTIONS 

To retrieve a set of objects, use collect() as a class method:

  my $recipes = MyApp::Recipe->collect;

Now we have a Norma::ORM::Collection, which gives us access to instantiated 
recipe objects, and other metadata about the result set.

  $recipes->items        # => list of instantiated recipe objects
  $recipes->total_count  # => count of matching items

See Norma::ORM::Collection for more about specifying criteria with joins and 
"where" conditions, etc.

RELATIONSHIPS

When we compose the role we can also specify relationships.  This is up 
to us -- there's no magic to try and discover this.  

Assuming we have an "authors" table with an id, name, and email, and assuming 
we have an author_id column in the "recipes" table, we can specify the 
relationship:

  package MyApp::Author;

  with 'Norma::ORM::Mappable' => {
      table_name => 'authors',
      dbh => MyApp::DB->new,
  }

  package MyApp::Recipe;

  with 'Norma::ORM::Mappable' => {
      table_name => 'recipes',
      dbh => MyApp::DB->new,
      relationships => [
          {
              name => 'author' 
              class => 'MyApp::Author',
              nature => 'belongs_to'
          }
      ]
  };
  
With the relationship defined, now we can access the author through the recipe:

  my $recipe = MyApp::Recipe->load(...);
  $recipe->author; # => MyApp::Author object

This related data is loaded lazily, only when we ask for it.  For has_many 
relationships, we get back a Collection instead of an instantiated object.

For more see documentation for Norma::ORM::Mappable and Norma::ORM::Collection.

AUTHOR

David Chester <[email protected]>

COPYRIGHT AND LICENSE

This software is copyright (c) 2010-2011 by David Chester.

This is free software; you can redistribute it and/or modify it under the same
terms as the Perl 5 programming language system itself.

norma's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.