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

NAME

Yancy::Model - Model layer for Yancy apps

VERSION

version 1.088

SYNOPSIS

    # XXX: Allow using backend strings
    my $model = Yancy::Model->new( backend => $backend );

    my $schema = $model->schema( 'foo' );

    my $id = $schema->create( $data );
    my $count = $schema->delete( $id );
    my $count = $schema->delete( $where );
    my $count = $schema->set( $id, $data );
    my $count = $schema->set( $where, $data );

    my $item = $schema->get( $id );
    my ( $items, $total ) = $schema->list( $where, $opts );
    for my $item ( @$items ) {
    }

    my $success = $row->set( $data );
    my $success = $row->delete();
    my $data = $row->to_hash;

DESCRIPTION

NOTE: This module is experimental and its API may change before Yancy v2!

Yancy::Model is a framework for your business logic. Yancy::Model contains a number of schemas, Yancy::Model::Schema objects. Each schema contains a number of items, Yancy::Model::Item objects.

For information on how to extend this module to add your own schema and item methods, see Yancy::Guides::Model.

ATTRIBUTES

backend

A Yancy::Backend object.

namespaces

An array of namespaces to find Schema and Item classes. Defaults to [ 'Yancy::Model' ].

log

A Mojo::Log object to log messages to.

METHODS

new

Create a new model with the given backend. In addition to any "ATTRIBUTES", these options may be given:

schema

A JSON schema configuration. By default, the information from "read_schema" will be merged with this information. See "Documenting Your Schema" in Yancy::Guides::Schema for more information.

read_schema

Read the backend database information to build the schema information. Enabled by default. Set to a false value to disable.

find_class

Find a class of the given type for an object of the given name. The name is run through "camelize" in Mojo::Util before lookups.

    unshift @{ $model->namespaces }, 'MyApp';
    # MyApp::Schema::User
    $class = $model->find_class( Schema => 'user' );
    # MyApp::Item::UserProfile
    $class = $model->find_class( Item => 'user_profile' );

If a specific class cannot be found, a generic class for the type is found instead.

    # MyApp::Schema
    $class = $model->find_class( Schema => 'not_found' );
    # MyApp::Item
    $class = $model->find_class( Item => 'not_found' );

read_schema

Read the schema from the "backend" and prepare schema objects using "find_class" to find the correct classes.

schema

Get or set a schema object.

    $model = $model->schema( user => MyApp::Model::User->new );
    $schema = $model->schema( 'user' );

json_schema

Get the JSON Schema for every attached schema.

schema_names

Get a list of all the schema names.

SEE ALSO

Yancy::Guides::Model

AUTHOR

Doug Bell <preaction@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Doug Bell.

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