NAME

JSONAPI::Document - Turn DBIx results into JSON API documents.

VERSION

version 2.4

SYNOPSIS

use JSONAPI::Document;
use DBIx::Class::Schema;

my $jsonapi = JSONAPI::Document->new({ api_url => 'http://example.com/api' });
my $schema = DBIx::Class::Schema->connect(['dbi:SQLite:dbname=:memory:', '', '']);
my $user = $schema->resultset('User')->find(1);

# Builds a simple JSON API document, without any relationships
my $doc = $jsonapi->resource_document($user);

# Same but with all relationships
my $doc = $jsonapi->resource_document($user, { includes => 'all_related' });

# With only the author relationship
my $doc = $jsonapi->resource_document($user, { includes => ['author'] });

# Fully blown resource document with all relationships and their attributes
my $doc = $jsonapi->compound_resource_document($user);

# Multiple resource documents
my $docs = $jsonapi->resource_documents($schema->resultset('User'));

# With sparse fieldsets
my $doc = $jsonapi->resource_document($user, { fields => [qw/name email/] });

# Relationships with sparse fieldsets
my $doc = $jsonapi->resource_document($user, { related_fields => { author => [qw/name expertise/] } });

DESCRIPTION

Moo class that builds data structures according to the JSON API specification.

NOTES

JSON API documents require that you define the type of a document, which this library does using the source_name of the result row. The type is also pluralised using Linua::EN::Inflexion while keeping relationship names intact (i.e. an 'author' relationship will still be called 'author', with the type 'authors').

This module supplies an opt-in Moo role that can be consumed by objects that layer over a DBIx::Class::Row, JSONAPI::Document::Role::Attributes. Consuming objects should implement a method called attributes which will be used throughout the creation of resource documents for that result type to build the attributes of the document. This is useful when you have a more complicated set of attributes that cannot be fulfilled by simply calling get_inflated_columns (the default behaviour).

ATTRIBUTES

api_url

Required; An absolute URL pointing to your servers JSON API namespace.

kebab_case_attrs

Boolean attribute; setting this will make the column keys for each document into kebab-cased-strings instead of snake_cased. Default is false.

METHODS

compound_resource_document(DBIx::Class::Row|Object $row, HashRef $options)

A compound document is one that includes the resource object along with the data of all its relationships.

Returns a HashRef with the following structure:

{
    data => {
        id => 1,
        type => 'authors',
        attributes => {},
        relationships => {},
    },
    included => [
        {
            id => 1,
            type => 'posts',
            attributes => { ... },
        },
        ...
    ]
}

The following options can be given:

resource_document(DBIx::Class::Row|Object $row, HashRef $options)

Builds a single resource document for the given result row. Will optionally include relationships that contain resource identifiers.

Returns a HashRef with the following structure:

{
    id => 1,
    type => 'authors',
    attributes => {},
    relationships => {},
},

View the resource document specification here.

Uses decamelize from Mojo::Util to parse the source_name of the DBIx::Class::Row and set the appropriate type of the document. This is used to ensure that your rows source name gets hyphenated appropriately when converted into its plural form.

The following options can be given:

resource_documents(DBIx::Class::ResultSet|Object|ArrayRef $resultset, HashRef $options)

Builds the structure for multiple resource documents with a given resultset.

$resultset can be either a DBIx::Class::ResultSet object in which case this method will call all on the resultset, an object that extends DBIx::Class::ResultSet, or you can pass in an ArrayRef from your own all call.

Returns a HashRef with the following structure:

{
    data => [
        {
            id => 1,
            type => 'authors',
            attributes => {},
            relationships => {},
        },
        ...
    ]
}

See resource_document for a list of options.

LICENSE

This code is released under the Perl 5 License.