We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Currently the library doesn't support removing an BelongsTo relation. You can set the relation to null but this isn't send to the backend
BelongsTo
null
My current fix:
/** * Fix clearing of belongsTo relation * * @param data */ protected getRelationships(data: any): any { let relationships: any; const belongsToMetadata: any[] = Reflect.getMetadata('BelongsTo', data) || []; const hasManyMetadata: any[] = Reflect.getMetadata('HasMany', data) || []; for (const key in data) { if (data.hasOwnProperty(key)) { if (data[key] instanceof JsonApiModel) { relationships = relationships || {}; if (data[key].id) { const entity = belongsToMetadata.find((entity: any) => entity.propertyName === key); const relationshipKey = entity.relationship; relationships[relationshipKey] = { data: this.buildSingleRelationshipData(data[key]) }; } } else if (data[key] instanceof Array) { const entity = hasManyMetadata.find((entity: any) => entity.propertyName === key); if (entity && this.isValidToManyRelation(data[key])) { relationships = relationships || {}; const relationshipKey = entity.relationship; const relationshipData = data[key] .filter((model: JsonApiModel) => model.id) .map((model: JsonApiModel) => this.buildSingleRelationshipData(model)); relationships[relationshipKey] = { data: relationshipData }; } } else if (data[key] === null) { const entity = belongsToMetadata.find((entity: any) => entity.propertyName === key); if (entity) { relationships = relationships || {}; relationships[entity.relationship] = { data: null }; } } } } return relationships; }
The updateRelationships method can not handle relationshipKey: {data: null} response. This is fixed with:
updateRelationships
relationshipKey: {data: null}
/** * @param model * @param relationships */ protected updateRelationships<T extends JsonApiModel>(model: T, relationships: any): T { const modelsTypes: any = Reflect.getMetadata('JsonApiDatastoreConfig', this.constructor).models; for (const relationship in relationships) { if (relationships.hasOwnProperty(relationship) && model.hasOwnProperty(relationship) && model[relationship]) { const relationshipModel: JsonApiModel = model[relationship]; const hasMany: any[] = Reflect.getMetadata('HasMany', relationshipModel); const propertyHasMany: any = find(hasMany, (property) => { return modelsTypes[property.relationship] === model.constructor; }); if (propertyHasMany) { relationshipModel[propertyHasMany.propertyName] = relationshipModel[propertyHasMany.propertyName] || []; const indexOfModel = relationshipModel[propertyHasMany.propertyName].indexOf(model); if (indexOfModel === -1) { relationshipModel[propertyHasMany.propertyName].push(model); } else { relationshipModel[propertyHasMany.propertyName][indexOfModel] = model; } } } } return model; }
The text was updated successfully, but these errors were encountered:
[BUGFIX] ghidoz#244 Fix removing belongsTo relations
20e8b93
Related PR #245
Sorry, something went wrong.
[BUGFIX] #244 Fix removing belongsTo relations (#245)
ead645a
No branches or pull requests
Currently the library doesn't support removing an
BelongsTo
relation. You can set the relation tonull
but this isn't send to the backendMy current fix:
The
updateRelationships
method can not handlerelationshipKey: {data: null}
response.This is fixed with:
The text was updated successfully, but these errors were encountered: