PUT to an array in existing collection - postman

The following is the schema of my API end-point. I want to test a PUT using PostMan, where I add a "skill".
How would you enter the information in Postman so that you are just passing a new "skill" to the model?
{
"_id": "579a6fa26a0b6484172ae284",
"firstname": "Max",
"lastname": "Headron",
"skills": [
{"skill":"Can Type", "level":"Great"},
{"skill":"Can Run", "level":"Good"},
]
}
Would you use dot notation in a form field. So instead of adding "firstname", which is at the top level of the object, you might enter "skills.skill" and the "value".
Hopefully this is a clearer description.

Just change the body of the request.
See https://www.getpostman.com/docs/requests.

Related

How to add map to map array in AWS DynamoDB only when id is not existed?

Here is my DynamoDB structure.
{"books": [
{
"name": "Hello World 1",
"id": "1234"
},
{
"name": "Hello World 2",
"id": "5678"
}
]}
I want to set ConditionExpression to check whether id existed before adding new items to books array. Here is my ConditionExpression. I am using API gateway to access DynamoDB.
"ConditionExpression": "NOT contains(#lu.books.id,:id)",
"ExpressionAttributeValues": {":id": {
"S": "$input.path('$.id')"
}
}
Result when I test the API: no matter id existed or not, success to add items to array.
Any suggestion on how to do it? Thanks!
Unfortunately, you can't. However, there is a workaround.
Store the books in separate rows. For example
PK SK
BOOK_LU#<ID> BOOK_NAME#<book name>#BOOK_ID#<BOOK_ID>
Now you can use the 'if_not_exists' conditional expression
"ConditionExpression": "if_not_exists(id, :id)'",
"ExpressionAttributeValues": {":id": {
"S": "$input.path('$.id')"
}
}
The con is if you were previously fetching the list as part of another object you will have to change that.
The pro is that now you can easily work with the books + you won't hit the max row size limits if the books became too many.

What is a correct way to handle writable M2M relationship in djangorestframework?

I have a many to many relationship between a Contact and a ContactGroup. One contact can belong to many groups, one group can contain multiple contacts.
I want to be able to display data like this, so I don't need to do multiple queries when showing names of groups where an user belongs.
GET
{
"id": 1,
"name": "Gandalf",
"groups": [
{
"id": 3,
"name": "Lord of the rings"
}
]
}
But if I update, I want to be able to update using ids or urls e.g.
POST
{
"id": 1,
"name": "Gandalf",
"groups": [
[2]
]
}
That would remove it from group 3 and put it to group 2 instead. I know I should write a Writable nested serializer, but I have two questions:
1) I want to do this properly, what is supposed to be a good practice when I want to do this. Shall I send ids or the whole objects like
POST
{
"id": 1,
"name": "Gandalf",
"groups": [
{
"id": 2,
"name": "Wizards"
}
]
}
This one seems a bit weird to me as I need to send information that is not needed (name in this case).
2) If I can go with id/url principle, how shall I do this? In a custom create/update method, I can't have id validated, because a serializer points to a GroupSerializer and doesn't accept int type, it expects GroupSerializer, so accessing validated_data.get('groups') wouldn't get me ids, it would tell me [{"non_field_errors":["Invalid data. Expected a dictionary, but got int."]}]}
I can write 2 serializers - one for create/update and one from displaying data. Do you think it's a correct way of doing this? Am I doing a right think in a first place? What do you think of this approach?
For #1, you can leave the name as read only field in which case you'll have it for read and it'll be discarded for write operations.
For #2 as you want to remain consistent, your best take it to send:
POST
{
"id": 1,
"name": "Gandalf",
"groups": [
{"id": 2}
]
}
You really don't want to have a different style for read and write operations, really.

Populating search results with meta data in Amazon CloudSearch

Unfortunately, Amazon CloudSearch does not support nested JSON, meaning that the below document structure is not valid.
[{
"type": "add",
"id": 1,
"fields": {
"company_name": "My Company",
"services": [
{
"id": 123,
"name": "Construction",
"logo": "logo1.png"
},
{
"id": 456,
"name": "Programming",
"logo": "logo2.png"
}
]
}
}]
Basically I cannot nest an array of objects under the services key. In this particular scenario, only the nested name field has to be searchable, so what I could do is the following:
[{
"type": "add",
"id": 1,
"fields": {
"company_name": "My Company",
"services": [ "Construction", "Programming" ]
}
}]
The above JSON is valid, and I can still search for the service names. However, now I have lost some meta data about my services that I need when displaying the search results. Is there any way in which I can add the meta data to the document in Amazon CloudSearch and have it returned with my search results, such that I can use it when displaying the results?
Or do I have to fetch this additional meta data from my database afterwards to populate the search results with the additional data required to display the results? This does not seem feasible because it complicates my code much more than if I could fetch this data straight from CloudSearch. This would also impact the performance of the search, even though I could use caching - but I kind of want to avoid that if possible, because I don't need it for anything else right now.
So my questions are:
Can I somehow add the meta data for services to the CloudSearch documents and have it returned with my search results?
If not, should I then extract this data from my data store upon receiving the search results from CloudSearch?
Do you have any other solutions or ideas? Are there any best practices with this?
Thank you in advance!

How to write CouchDb views?

I have list of such documents in my database of couchDB.
{
"_id": "9",
"_rev": "1-f5a9a0b76c6ae1fe5e20f1a1f9e6f8ba",
"Project": "Vaibhava",
"Type": "activity",
"Name": "Civil_Clearence",
"PercentComplete": "",
"DateAndTime": "",
"SourcePMSId": "1049",
"ProgressUpdatedToPMSFlag": "NO",
"UserId": "Kundan",
"ParentId": "5"
}
How to write a view function so that when i pass a doc._id as a key then i must get all siblings of that doc._id(docs with ParentId same as the key which I have sent)??
As said in another answer, it is not possible to do that with a single request.
However, you can do the following instead:
Define a map (with no reduce) view indexed on ParentID:
function(o) {
if (o.ParentID) {
emit(o.ParentID);
}
}
Send a first request to your object to know the ID of its parent:
GET /myDatabase/myObject
Then send a request to your view
GET /myDatabase/_design/myApp/_view/myView/?key="itsParent"&include_docs=true
Having several requests should not cause much harm here, since their number (2) is constant.
Moreover you can hide them behind a single request handled by NodeJS.
Unfortunately, you would need to chain together two map-reduce functions to achieve this result and that functionality is not available in CouchDB. See this question for further information.

real-time update page feed

here I am for real update "page", add fields to status to receive the "status" of
pages, I try to add as "link" fields to receive the subscription works but I do
not receive notification when I publish a link, it's really difficult to have
correct information
{
"object": "user",
"callback_url": "http://*/fbcallback.php",
"fields": [
"feed",
"link",
"status"
],
"active": true
}
http://bugs.developers.facebook.net/show_bug.cgi?id=18048#c40
As per the documentation at https://developers.facebook.com/docs/reference/api/realtime/ :
To setup a subscription, send a POST with the following parameters
It seems like you're posting a JSON object while you should do is a normal post with these variables (as if you're doing a with action="post").
Note, by the way, that the fields parameter needs to have a CSV value, so that would be "feed,link,status", and that there's no 'active' attribute.