| 1 | package JSONAPI::Document; | ||||||||||||||||||||||||||||
| 2 | 6 | $JSONAPI::Document::VERSION = '2.0'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Turn DBIx results into JSON API documents. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use CHI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use JSONAPI::Document::Builder; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use JSONAPI::Document::Builder::Compound; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Lingua::EN::Segment; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | has kebab_case_attrs => ( | ||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | 6 | ▶ | default => sub { 0 }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | has api_url => ( | ||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | isa => sub { | ||||||||||||||||||||||||||||
| 20 | 7 | ▶ | Carp::croak('api_url should be an absolute url') unless $_[0] =~ m/^http/i; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | }, | ||||||||||||||||||||||||||||
| 22 | 6 | required => 1, | |||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 6 | has data_dir => ( | |||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||
| 27 | required => 1, | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 6 | has chi => (is => 'lazy',); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 6 | has segmenter => (is => 'lazy',); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub _build_chi { | ||||||||||||||||||||||||||||
| 35 | 7 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_chi: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 7 | return CHI->new(driver => 'File', root_dir => $self->data_dir); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub _build_segmenter { | ||||||||||||||||||||||||||||
| 40 | 6 | ▶ | return Lingua::EN::Segment->new; | ||||||||||||||||||||||||||
|
Subroutine
_build_segmenter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub compound_resource_document { | ||||||||||||||||||||||||||||
| 44 | 5 | ▶ | my ($self, $row, $options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
compound_resource_document: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 5 | ▶ | $options //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options //= {}
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||
| 46 | 5 | ▶ | my $fields = [grep { $_ } @{ $options->{fields} // [] }]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options->{"fields"} // []
MC/DC0%0/1
$options->{"fields"} // []
$options->{"fields"}
Truth table50%1/2
A
A= $options->{"fields"}
| |||||||||||||||||||||||||||||
| 47 | 5 | ▶ | my $related_fields = $options->{related_fields} //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options->{"related_fields"} //= {}
MC/DC0%0/1
$options->{"related_fields"} //= {}
$options->{"related_fields"}
Truth table50%1/2
A
A= $options->{"related_fields"}
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 5 | my @relationships = $row->result_source->relationships(); | |||||||||||||||||||||||||||
| 50 | 5 | ▶ | if ($options->{includes}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 4 | @relationships = @{ $options->{includes} }; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | my $builder = JSONAPI::Document::Builder::Compound->new( | |||||||||||||||||||||||||||
| 55 | api_url => $self->api_url, | ||||||||||||||||||||||||||||
| 56 | chi => $self->chi, | ||||||||||||||||||||||||||||
| 57 | fields => $fields, | ||||||||||||||||||||||||||||
| 58 | kebab_case_attrs => $self->kebab_case_attrs, | ||||||||||||||||||||||||||||
| 59 | row => $row, | ||||||||||||||||||||||||||||
| 60 | segmenter => $self->segmenter, | ||||||||||||||||||||||||||||
| 61 | relationships => \@relationships, | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | return { | ||||||||||||||||||||||||||||
| 65 | 5 | data => $builder->build_document(), | |||||||||||||||||||||||||||
| 66 | included => $builder->build_relationships(\@relationships, $related_fields), | ||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub resource_documents { | ||||||||||||||||||||||||||||
| 71 | 2 | ▶ | my ($self, $resultset, $options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
resource_documents: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 2 | ▶ | $options //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options //= {}
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | my @results = $resultset->all(); | |||||||||||||||||||||||||||
| 75 | 2 | return { data => [map { $self->resource_document($_, $options) } @results], }; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub resource_document { | ||||||||||||||||||||||||||||
| 79 | 14 | ▶ | my ($self, $row, $options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
resource_document: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 14 | ▶ | Carp::confess('No row provided') unless $row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 14 | ▶ | $options //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options //= {}
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||
| 83 | 14 | my $with_attributes = $options->{with_attributes}; | |||||||||||||||||||||||||||
| 84 | 14 | ▶ | my $includes = $options->{includes} // []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"includes"} // []
MC/DC100%1/1
$options->{"includes"} // []
$options->{"includes"}
Truth table100%2/2
A
A= $options->{"includes"}
| |||||||||||||||||||||||||||||
| 85 | 14 | ▶ | my $fields = [grep { $_ } @{ $options->{fields} // [] }]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"fields"} // []
MC/DC100%1/1
$options->{"fields"} // []
$options->{"fields"}
Truth table100%2/2
A
A= $options->{"fields"}
| |||||||||||||||||||||||||||||
| 86 | 14 | ▶ | my $related_fields = $options->{related_fields} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"related_fields"} //= {}
MC/DC100%1/1
$options->{"related_fields"} //= {}
$options->{"related_fields"}
Truth table100%2/2
A
A= $options->{"related_fields"}
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 14 | ▶ | if (ref(\$includes) eq 'SCALAR' && $includes eq 'all_related') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref \$includes eq "SCALAR" and $includes eq "all_related"
MC/DC50%1/2
ref \$includes eq "SCALAR" and $includes eq "all_related"
ref \$includes eq "SCALAR"
$includes eq "all_related"
Truth table66%2/3
A and B
A= ref \$includes eq "SCALAR"B= $includes eq "all_related"
| |||||||||||||||||||||||||||||
| 89 | 1 | $includes = [$row->result_source->relationships()]; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 14 | my $builder = JSONAPI::Document::Builder->new( | |||||||||||||||||||||||||||
| 93 | api_url => $self->api_url, | ||||||||||||||||||||||||||||
| 94 | chi => $self->chi, | ||||||||||||||||||||||||||||
| 95 | fields => $fields, | ||||||||||||||||||||||||||||
| 96 | kebab_case_attrs => $self->kebab_case_attrs, | ||||||||||||||||||||||||||||
| 97 | row => $row, | ||||||||||||||||||||||||||||
| 98 | segmenter => $self->segmenter, | ||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 14 | my $document = $builder->build(); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 14 | ▶ | if (@$includes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 5 | my %relationships; | |||||||||||||||||||||||||||
| 105 | 5 | foreach my $relationship (@$includes) { | |||||||||||||||||||||||||||
| 106 | 6 | my $relationship_type = $builder->format_type($relationship); | |||||||||||||||||||||||||||
| 107 | $relationships{$relationship_type} = $builder->build_relationship( | ||||||||||||||||||||||||||||
| 108 | $relationship, | ||||||||||||||||||||||||||||
| 109 | 6 | $related_fields->{$relationship}, | |||||||||||||||||||||||||||
| 110 | { with_attributes => $with_attributes }); | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 5 | ▶ | if (values(%relationships)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 5 | $document->{relationships} = \%relationships; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 14 | return $document; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 NAME | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | JSONAPI::Document - Turn DBIx results into JSON API documents. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 VERSION | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | version 2.0 | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | use JSONAPI::Document; | ||||||||||||||||||||||||||||
| 137 | use DBIx::Class::Schema; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | my $jsonapi = JSONAPI::Document->new({ api_url => 'http://example.com/api' }); | ||||||||||||||||||||||||||||
| 140 | my $schema = DBIx::Class::Schema->connect(['dbi:SQLite:dbname=:memory:', '', '']); | ||||||||||||||||||||||||||||
| 141 | my $user = $schema->resultset('User')->find(1); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Builds a simple JSON API document, without any relationships | ||||||||||||||||||||||||||||
| 144 | my $doc = $jsonapi->resource_document($user); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Same but with all relationships | ||||||||||||||||||||||||||||
| 147 | my $doc = $jsonapi->resource_document($user, { includes => 'all_related' }); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # With only the author relationship | ||||||||||||||||||||||||||||
| 150 | my $doc = $jsonapi->resource_document($user, { includes => ['author'] }); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # Fully blown resource document with all relationships and their attributes | ||||||||||||||||||||||||||||
| 153 | my $doc = $jsonapi->compound_resource_document($user); | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Multiple resource documents | ||||||||||||||||||||||||||||
| 156 | my $docs = $jsonapi->resource_documents($schema->resultset('User')); | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # With sparse fieldsets | ||||||||||||||||||||||||||||
| 159 | my $doc = $jsonapi->resource_document($user, { fields => [qw/name email/] }); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Relationships with sparse fieldsets | ||||||||||||||||||||||||||||
| 162 | my $doc = $jsonapi->resource_document($user, { related_fields => { author => [qw/name expertise/] } }); | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Moo class that builds data structures according to the L<JSON API|http://jsonapi.org/format/> specification. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 NOTES | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | JSON API documents require that you define the type of a document, which this | ||||||||||||||||||||||||||||
| 171 | library does using the L<source_name|https://metacpan.org/pod/DBIx::Class::ResultSource#source_name> | ||||||||||||||||||||||||||||
| 172 | of the result row. The type is also pluralised using L<Linua::EN::Inflexion|https://metacpan.org/pod/Lingua::EN::Inflexion> | ||||||||||||||||||||||||||||
| 173 | while keeping relationship names intact (i.e. an 'author' relationship will still be called 'author', with the type 'authors'). | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | This module supplies an opt-in Moo role that can be consumed by objects that layer over a DBIx::Class::Row, | ||||||||||||||||||||||||||||
| 176 | C<JSONAPI::Document::Role::Attributes>. Consuming objects should implement a method called C<attributes> | ||||||||||||||||||||||||||||
| 177 | which will be used throughout the creation of resource documents for that result type to build the attributes | ||||||||||||||||||||||||||||
| 178 | of the document. This is useful when you have a more complicated set of attribute that cannot be fulfilled | ||||||||||||||||||||||||||||
| 179 | by simply calling C<get_inflated_columns> (the default behaviour). | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 data_dir | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Required; Directory string where this module can store computed document type strings. This should be | ||||||||||||||||||||||||||||
| 186 | a directory that's ignored by your VCS. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head2 api_url | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Required; An absolute URL pointing to your servers JSON API namespace. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head2 kebab_case_attrs | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Boolean attribute; setting this will make the column keys for each document into | ||||||||||||||||||||||||||||
| 195 | kebab-cased-strings instead of snake_cased. Default is false. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 METHODS | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head2 compound_resource_document(I<DBIx::Class::Row|Object> $row, I<HashRef> $options) | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | A compound document is one that includes the resource object | ||||||||||||||||||||||||||||
| 202 | along with the data of all its relationships. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Returns a I<HashRef> with the following structure: | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||||||||
| 207 | data => { | ||||||||||||||||||||||||||||
| 208 | id => 1, | ||||||||||||||||||||||||||||
| 209 | type => 'authors', | ||||||||||||||||||||||||||||
| 210 | attributes => {}, | ||||||||||||||||||||||||||||
| 211 | relationships => {}, | ||||||||||||||||||||||||||||
| 212 | }, | ||||||||||||||||||||||||||||
| 213 | included => [ | ||||||||||||||||||||||||||||
| 214 | { | ||||||||||||||||||||||||||||
| 215 | id => 1, | ||||||||||||||||||||||||||||
| 216 | type => 'posts', | ||||||||||||||||||||||||||||
| 217 | attributes => { ... }, | ||||||||||||||||||||||||||||
| 218 | }, | ||||||||||||||||||||||||||||
| 219 | ... | ||||||||||||||||||||||||||||
| 220 | ] | ||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | The following options can be given: | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =over | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =item C<includes> | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | An array reference specifying inclusion of a subset of relationships. | ||||||||||||||||||||||||||||
| 230 | By default all the relationships will be included. Use this if you | ||||||||||||||||||||||||||||
| 231 | only want a subset of relationships (e.g. when accepting the C<includes> | ||||||||||||||||||||||||||||
| 232 | query parameter in your API requests, where you have to return only what | ||||||||||||||||||||||||||||
| 233 | relationships were requested). | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | This argument should contain strings representing direct relationships to the row, | ||||||||||||||||||||||||||||
| 236 | and can also contain hash refs which specify nested inclusion. Example: | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | $self->compound_resource_document($post, ['author', { comments => ['author'] }]); | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | This will include the post as the primary document, its direct relationships 'author' | ||||||||||||||||||||||||||||
| 241 | and 'comments', and the 'author' of each related comment. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | B<NOTE>: Nested relationships are experimental and come with the following limitations: | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =over 2 | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =item | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | many_to_many relationships are not supported | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =item | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | only one level of depth is supported (so requesting 'include=comments.likes.author' will throw errors) | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =back | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =back | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head2 resource_document(I<DBIx::Class::Row|Object> $row, I<HashRef> $options) | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Builds a single resource document for the given result row. Will optionally | ||||||||||||||||||||||||||||
| 262 | include relationships that contain resource identifiers. | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Returns a I<HashRef> with the following structure: | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | { | ||||||||||||||||||||||||||||
| 267 | id => 1, | ||||||||||||||||||||||||||||
| 268 | type => 'authors', | ||||||||||||||||||||||||||||
| 269 | attributes => {}, | ||||||||||||||||||||||||||||
| 270 | relationships => {}, | ||||||||||||||||||||||||||||
| 271 | }, | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | View the resource document specification L<here|http://jsonapi.org/format/#document-resource-objects>. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | Uses L<Lingua::EN::Segment|metacpan.org/pod/Lingua::EN::Segment> to set the appropriate type of the | ||||||||||||||||||||||||||||
| 276 | document. This is a bit expensive, but it ensures that your schema results source name gets hyphenated | ||||||||||||||||||||||||||||
| 277 | appropriately when converted into its plural form. The resulting type is cached into the C<data_dir> | ||||||||||||||||||||||||||||
| 278 | to minimize the need to re-compute the document type. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | The following options can be given: | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =over | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =item C<includes> I<Str|ArrayRef> | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Optional; Used to specify any relationships of the row to include. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | This argument can contain either the value 'all_related', which will return all the direct | ||||||||||||||||||||||||||||
| 289 | relationships of the row, or an array ref including a subset of direct relationships. | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =item C<with_attributes> I<Bool> | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | If C<includes> is used, for each resulting relationship row, the attributes (columns) of that | ||||||||||||||||||||||||||||
| 294 | relationship will be included. | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | By default, each relationship will contain a L<links object|http://jsonapi.org/format/#document-links>. | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | If this option is true, links object will be replaced with attributes. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =item C<fields> I<ArrayRef> | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | An optional list of attributes to include for the given resource. Implements | ||||||||||||||||||||||||||||
| 303 | L<sparse fieldsets|http://jsonapi.org/format/#fetching-sparse-fieldsets> in the specification. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | Will pass the array reference to the C<attributes_via> method, which should make use | ||||||||||||||||||||||||||||
| 306 | of the reference and return B<only> those attributes that were requested. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =item C<related_fields> I<HashRef> | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | Behaves the same as the C<fields> option but for relationships, returning only those fields | ||||||||||||||||||||||||||||
| 311 | for the related resource that were requested. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | Not specifying sparse fieldsets for a resource implies requesting all attributes for | ||||||||||||||||||||||||||||
| 314 | that relationship. | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =back | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =head2 resource_documents(I<DBIx::Class::Row|Object> $row, I<HashRef> $options) | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Builds the structure for multiple resource documents with a given resultset. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | Returns a I<HashRef> with the following structure: | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | { | ||||||||||||||||||||||||||||
| 325 | data => [ | ||||||||||||||||||||||||||||
| 326 | { | ||||||||||||||||||||||||||||
| 327 | id => 1, | ||||||||||||||||||||||||||||
| 328 | type => 'authors', | ||||||||||||||||||||||||||||
| 329 | attributes => {}, | ||||||||||||||||||||||||||||
| 330 | relationships => {}, | ||||||||||||||||||||||||||||
| 331 | }, | ||||||||||||||||||||||||||||
| 332 | ... | ||||||||||||||||||||||||||||
| 333 | ] | ||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | See C<resource_document> for a list of options. | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | This code is released under the Perl 5 License. | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =cut | ||||||||||||||||||||||||||||