The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DustyDB::Model - model classes represent the tables in your database

VERSION

version 0.06

SYNOPSIS

  use DustyDB;
  my $db = DustyDB->new( path => 'foo.db' );
  my $author = $db->model( 'Author' );

  # Create a record
  my $schwartz = $author->create( name => 'Randall Schwartz' );

  # New record that hasn't been saved yet
  my $chromatic = $author->construct( name => 'chromatic' );

  # Load a record from the disk
  my $the_damian = $author->load( 'Damian Conway' );

  # Load all/many of the records
  my @authors = $author->all;
  my @d_authors = $author->all_where( name => qr/^d/i );

  # Or as an iterator
  my $authors = $autor->all;
  while (my $author = $authors->next) {
      print " - ", $author->name, "\n";
  }

  # Delete the record
  $schwartz->delete;

DESCRIPTION

This class is the bridge between the storage database and the records in it. Normally, you won't need to create this object yourself, but use the model method of DustyDB to create it for you.

ATTRIBUTES

db

This is the DustyDB that owns this model instance.

record_meta

This is the meta-class for something that does DustyDB::Record.

METHODS

construct

  my $record = $model->construct( %params );

This constructs an object, but does not save it.

create

  my $record = $model->create( %params );

This is essentially just sugar for:

  my $record = $model->construct( %params );
  $record->save;

This constructs the record and saves it to the database.

load

  my $record = $model->load( $value );
  my $record = $model->load( %key );

Given the names and values of key parameters, this will load an object from the database.

If there is only a single key attribute, you may pass just a value to this method.

load_or_create

  my $record = $model->load_or_create( %params );

Given the record attributes, it uses the key parameters given to load an object if such an object exists. If not, the object will be created using the parameters given.

save

  my $record = $model->save( %params );

  # Or the more verbose synonym
  my $record = $model->load_and_update_or_create( %params );

Given the record attributes, it uses the key parameters given to load an object, if such an object can be found. If found, it will overwrite all the non-key parameters with the values given (and clear those that aren't given) and then save the object. If not found, it will create an object using the record attributes given.

all

all_where

The "all" and "all_where" are synonyms. In list context, they will return a list of zero or more records. In scalar context they will return a DustyDB::Collection object. These methods will accept the same arguments as the "filter" in DustyDB::Collection method of that class.