I have 2 model Task and Hearts, Task embedsMany hearts.
task.json
{
"name": "Task",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"taskId": {
"type": "string"
}
},
"validations": [],
"relations": {
"hearts": {
"type": "embedsMany",
"model": "hearts",
"property": "heartsList",
"options": {
"validate": false,
"forceId": false
}
}
}},
"acls": [],
"methods": {}
}
hearts.json
{
"name": "hearts",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"hid": {
"type": "string"
}
},
"validations": [],
"relations": {
},
"acls": [],
"methods": {}
}
After model creation when i do a POST call.
Request:
POST http://localhost:3000/api/Tasks
Payload:
{
"taskId": "string"
}
Response:
{
"taskId": "string",
"id": 1,
"heartsList": []
}
Expected Result: Since I did not pass heartsList I do not expect it to be added in response, We have a situation where the heartsList property is readonly and should not be passed for POST Call.
Is there a way to omit it from getting added with default empty brackets?
@SowmyaGrama could you fork https://github.com/strongloop/loopback-sandbox and provide your code?
There is an example repo which contains a basic embedsMany relation from model Book to model People: https://github.com/strongloop/loopback-example-relations, I tried with it but by default when I do POST/ Books in explorer, it doesn't return me embedded items:
{
"name": "janny",
"id": 2,
"links": []
// embedded People is not included
}
And actually for GET/ Books you also have to use filter {"include": "embeddedRelationName"} to see your embedded items.
You can run node . for that example repo to test.
Thanks Janny,
Let me try this and get back to u . I am using loopback version 2.X
On Fri, Nov 4, 2016 at 1:07 AM, Janny [email protected] wrote:
@SowmyaGrama https://github.com/SowmyaGrama could you fork
https://github.com/strongloop/loopback-sandbox and provide your code?There is an example repo which contains a basic embedsMany relation from
model Book to model People: https://github.com/strongloop/
loopback-example-relations, I tried with it but by default when I do POST/
Books in explorer, it doesn't return me embedded items:{
"name": "janny",
"id": 2,
"links": []
// embedded People is not included
}And actually for GET/ Books you also have to use filter {"include":
"embeddedRelationName"} to see your embedded items.
You can run node . for that example repo to test.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/strongloop/loopback/issues/2882#issuecomment-258251148,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AOVY5TGknmyRkf8Nx69EKYYW5WTnNECNks5q6jgDgaJpZM4Kc5Sa
.
@jannyHou : Just looked at the repo you shared, from you comments it clear that the people object is indeed returned from POST call to /Books
What is the payload you pass to /Books?
In the response i see
{
"name": "janny",
"id": 2,
"links": [] ---> // embedded People is not included though i don't pass it in create payload
}
How can i avoid it?
@SowmyaGrama I am afraid you misunderstand here:
links is just a property of model Book, embedded people only occur in your book instance when you do GET Books/ with filter {"include": "people"}
sample result:
[
{
"name": "Book 1",
"id": 1,
"links": [
{
"id": 1,
"name": "Author 1",
"notes": "Note 1",
"linkedId": 1,
"linkedType": "Author"
},
{
"id": 2,
"name": "Reader 1",
"notes": "Note 2",
"linkedId": 1,
"linkedType": "Reader"
}
],
"people": [
{
"id": 1,
"name": "Author 1",
"notes": "Note 1",
"linkedId": 1,
"linkedType": "Author",
"linked": {
"name": "Author 1",
"id": 1
}
},
{
"id": 2,
"name": "Reader 1",
"notes": "Note 2",
"linkedId": 1,
"linkedType": "Reader",
"linked": {
"name": "Reader 1",
"id": 1
}
}
]
}
]
Janny, I was thinking that since links is a embedsMany field, the people model is returned within the links field. In the model defined here books has 3 fields, id, name and people
https://github.com/strongloop/loopback-example-relations/blob/master/common/models/book.json
Now if do not want links to be returned as empty array, is there a way to avoid it?
I will do a POST call with { "name":"test"} this should return me { "name":"test","id":3}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
I think this is the line that causes the problem: https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/relation-definition.js#L2450
Let me try if I can omit/change the default value by passing in options
Any update by when we can get fix for this
thanks in advance
FYI I created a PR to omit the default value for the embedded item, not finished, still in progress. Let's track and discuss the technical details there https://github.com/strongloop/loopback-datasource-juggler/pull/1532
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
@jannyHou any update on the PR
hi @matuagarwal Sorry I haven't got time to continue the fix PR, https://github.com/strongloop/loopback-datasource-juggler/pull/1532
cc @dhmlau Is it possible to prioritize fixing this issue in our next planning? Thanks!
I started a PR https://github.com/strongloop/loopback-datasource-juggler/pull/1532 to fix it but didn't get time to continue :(
@jannyHou Any update on the PR? We are facing this problem in multiple connectors
Marking it as bug so it won't be closed by stalebot.
I have heard different opinion on the expected behaviour:
@raymondfeng Do you think it's a valid use case to omit the embedded item when creating its parent instance?
For example:
Request:
POST http://localhost:3000/api/Tasks
Payload:
{
"taskId": "string"
}
Response with embedded item:
{
"taskId": "string",
"id": 1,
"heartsList": []
}
Response omit embedded item:
{
"taskId": "string",
"id": 1
}
And I also appreciate some explanation of the "read only property" mentioned in https://github.com/strongloop/loopback/issues/2882#issue-184407788, @SowmyaGrama or other ones run into the issue, does read only mean if you create a task with heartsList equals to [](or assigned with any value), the database returns error?
If that's the case, how do you generate the embedded item for task?
@raymondfeng @SowmyaGrama @jannyHou
It's my understanding that embedsMany is used to model an object that has an field of type array, with array elements that are themselves of type object.
So I think this boils down to whether the array field is required in the response or not.
Since the field is clearly not mandatory in the overall model (otherwise, you would not be allowed to create an instance of the main object without passing the array), then it seems reasonable, for consistency with Loopback's handling of other optional properties, to think that this field should also be optional on the response.
As a general point, it's perfectly valid for a structure to have a non-required array field.
It's equally valid for that field to be required - but then I would expect that to be explicitly stated in the model.
I will continue with my PR https://github.com/strongloop/loopback-datasource-juggler/pull/1532 to make the default value of embedded item optional.
While there is a question worth clarify, thanks @virkt25, I copy paste your question here:
Does getting an empty array property for the relation break anything?
And my concern:
+1 I am also curious to know, the only thing I can think of is inserting
[]to a read only property results in database error.
While if that’s the case, the embedded item can never be created.
So IMO “Having default value for embedded item” is one topic, figuring out and solving the “read only property” issue mentioned in the original story is another topic.
Even I make “omitting the default value” happen, it doesn’t guarantee solve their issue.
Should be fixed in https://github.com/strongloop/loopback-datasource-juggler/pull/1532, I am going to release a minor version for it.
Released in 3.17.0
In model.json file, add an option omitDefaultEmbeddedItem to the embedsMany relation entry
Set it to true.
E.g.
"relations": {
"emails": {
"type": "embedsMany",
"model": "EmailAddress",
"property": "emailList",
"options": {
"validate": true,
"forceId": false,
"omitDefaultEmbeddedItem": true
}
}
...
}