Conch API

Logo

Datacenter build and management service

View the Project on GitHub joyent/conch-api

Conch::Plugin::Database

SOURCE

https://github.com/joyent/conch-api/blob/master/lib/Conch/Plugin/Database.pm

DESCRIPTION

METHODS

register

Sets up the database and provides convenient accessors to it.

HELPERS

These methods are made available on the $c object (the invocant of all controller methods, and therefore other helpers).

schema

Provides read/write access to the database via DBIx::Class. Returns a Conch::DB object that persists for the lifetime of the application.

rw_schema

See “schema”; can be used interchangeably with it.

ro_schema

Provides (guaranteed) read-only access to the database via DBIx::Class. Returns a Conch::DB object that persists for the lifetime of the application.

db_<table>s, db_ro_<table>s

Provides direct read/write and read-only accessors to resultsets. The table name is used in the alias attribute (see “alias” in DBIx::Class::ResultSet). Note that these are NOT available when the (developer-only) no_db feature is enabled.

txn_wrapper

my $result = $c->txn_wrapper(sub ($c) {
    # many update, delete queries etc...
});

## if the result is false, we errored and rolled back the db...
return $c->status(400) if not $result;

Wraps the provided subref in a database transaction, rolling back in case of an exception. Any provided arguments are passed to the sub, along with the invocant controller.

If the exception is not 'rollback' (which signals an intentional premature bailout), the exception will be logged and stored in the stash, of which the first line will be included in the response if no other response is prepared (see “status” in Conch).

You should not render a response in the subref itself, as you will have a difficult time figuring out afterwards whether $c->rendered still needs to be called or not. Instead, use the subref’s return value to signal success.

LICENSING

Copyright Joyent, Inc.

This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://www.mozilla.org/en-US/MPL/2.0/.