@@ -13,6 +13,7 @@ import { JsonApiQueryData } from '../models/json-api-query-data';
1313import * as qs from 'qs' ;
1414import { DatastoreConfig } from '../interfaces/datastore-config.interface' ;
1515import { ModelConfig } from '../interfaces/model-config.interface' ;
16+ import { AttributeMetadata } from '../constants/symbols' ;
1617
1718export type ModelType < T extends JsonApiModel > = { new ( datastore : JsonApiDatastore , data : any ) : T ; } ;
1819
@@ -21,7 +22,11 @@ export class JsonApiDatastore {
2122 // tslint:disable:variable-name
2223 private _headers : Headers ;
2324 private _store : { [ type : string ] : { [ id : string ] : JsonApiModel } } = { } ;
24-
25+ // tslint:disable:max-line-length
26+ private getDirtyAttributes : Function = this . datastoreConfig . overrides && this . datastoreConfig . overrides . getDirtyAttributes ? this . datastoreConfig . overrides . getDirtyAttributes : this . _getDirtyAttributes ;
27+ private toQueryString : Function = this . datastoreConfig . overrides && this . datastoreConfig . overrides . toQueryString ? this . datastoreConfig . overrides . toQueryString : this . _toQueryString ;
28+ // tslint:enable:max-line-length
29+
2530 protected config : DatastoreConfig ;
2631
2732 constructor ( private http : Http ) { }
@@ -73,7 +78,7 @@ export class JsonApiDatastore {
7378 return new modelType ( this , { attributes : data } ) ;
7479 }
7580
76- private getDirtyAttributes ( attributesMetadata : any ) : { string : any } {
81+ private _getDirtyAttributes ( attributesMetadata : any ) : { string : any } {
7782 const dirtyData : any = { } ;
7883
7984 for ( const propertyName in attributesMetadata ) {
@@ -189,13 +194,21 @@ export class JsonApiDatastore {
189194 if ( data . hasOwnProperty ( key ) ) {
190195 if ( data [ key ] instanceof JsonApiModel ) {
191196 relationships = relationships || { } ;
192- relationships [ key ] = {
193- data : this . buildSingleRelationshipData ( data [ key ] )
194- } ;
197+
198+ if ( data [ key ] . id ) {
199+ relationships [ key ] = {
200+ data : this . buildSingleRelationshipData ( data [ key ] )
201+ } ;
202+ }
195203 } else if ( data [ key ] instanceof Array && data [ key ] . length > 0 && this . isValidToManyRelation ( data [ key ] ) ) {
196204 relationships = relationships || { } ;
205+
206+ const relationshipData = data [ key ]
207+ . filter ( ( model : JsonApiModel ) => model . id )
208+ . map ( ( model : JsonApiModel ) => this . buildSingleRelationshipData ( model ) ) ;
209+
197210 relationships [ key ] = {
198- data : data [ key ] . map ( ( model : JsonApiModel ) => this . buildSingleRelationshipData ( model ) )
211+ data : relationshipData
199212 } ;
200213 }
201214 }
@@ -330,7 +343,7 @@ export class JsonApiDatastore {
330343 return new RequestOptions ( { headers : requestHeaders } ) ;
331344 }
332345
333- private toQueryString ( params : any ) {
346+ private _toQueryString ( params : any ) : string {
334347 return qs . stringify ( params , { arrayFormat : 'brackets' } ) ;
335348 }
336349
@@ -351,7 +364,7 @@ export class JsonApiDatastore {
351364 private resetMetadataAttributes < T extends JsonApiModel > ( res : T , attributesMetadata : any , modelType : ModelType < T > ) {
352365 // TODO check why is attributesMetadata from the arguments never used
353366 // tslint:disable-next-line:no-param-reassign
354- attributesMetadata = Reflect . getMetadata ( 'Attribute' , res ) ;
367+ attributesMetadata = res [ AttributeMetadata ] ;
355368
356369 for ( const propertyName in attributesMetadata ) {
357370 if ( attributesMetadata . hasOwnProperty ( propertyName ) ) {
@@ -363,7 +376,7 @@ export class JsonApiDatastore {
363376 }
364377 }
365378
366- Reflect . defineMetadata ( 'Attribute' , attributesMetadata , res ) ;
379+ res [ AttributeMetadata ] = attributesMetadata ;
367380 return res ;
368381 }
369382
0 commit comments