From the perspective of a consumer, is there any value in abstracting resource attributes to make the fields self-describing? Or should the documentation handle it.
The idea is that each attribute will be wrapped in a more complex object which will provide fieldId, fieldType, and the value. Making each field more descriptive.
In addition, the web service would include another endpoint to further describe each field.
So, instead of the following:
{
"id":123,
"type":"person",
"attributes":{
"name":"John Smith",
"dateOfBirth":"2000-01-01",
"ssn":123456789
}
}
The json would look like this:
{
"id":123,
"type":"person",
"attributes":[
{
"fieldId":"name",
"dataType":"string",
"value":"John Smith"
},
{
"fieldId":"dateOfBirth",
"dataType":"date",
"value":"2000-01-01"
},
{
"fieldId":"ssn",
"dataType":"integer",
"value":123456789
}
],
"relationships":{
"dataType":{
"links":{
"related":{
"href":"http://acme.com/ws/dataTypes/"
}
},
"data":[
{
"id":"string",
"type":"dataType"
},
{
"id":"date",
"type":"dataType"
},
{
"id":"integer",
"type":"dataType"
}
]
},
"field":{
"links":{
"related":{
"href":"http://acme.com/ws/fields/"
}
},
"data":[
{
"id":"name",
"type":"field"
},
{
"id":"dateOfBirth",
"type":"field"
},
{
"id":"ssn",
"type":"field"
}
]
}
}
}
And then a dataType resource linked to would give some description and/or format:
{
"id":"ssn",
"type":"field",
"attributes":{
"valueType":"string",
"description":"Social security in the xxx-xx-xxxx format."
},
"links":{
"self":{
"href":"http://acme.com/ws/fields/ssn",
"meta":{
"httpMethod":"GET"
}
}
}
}
{
"id":"date",
"type":"dataType",
"attributes":{
"valueType":"string",
"description":"yyyy-MM-dd"
},
"links":{
"self":{
"href":"http://acme.com/ws/dataTypes/date",
"meta":{
"httpMethod":"GET"
}
}
}
}
To answer this From the perspective of a consumer, is there any value in abstracting resource attributes to make the fields self-describing? Or should the documentation handle it.
Based on experience and evaluating multiple api's the api should only send required data. There is no point handling description in response that needs to be taken care by documentation.
Plus consider the extra amount of data you are sending just to describe the fields
In addition frontend (say javascript) would need to parse the object, save time by sending only the required data
consider the bandwidth taken by this
{
"id":123,
"type":"person",
"attributes":{
"name":"John Smith",
"dateOfBirth":"2000-01-01",
"ssn":123456789
}
}
as compared to this huge data
{
"id":123,
"type":"person",
"attributes":[
{
"fieldId":"name",
"dataType":"string",
"value":"John Smith"
},
{
"fieldId":"dateOfBirth",
"dataType":"date",
"value":"2000-01-01"
},
{
"fieldId":"ssn",
"dataType":"integer",
"value":123456789
}
],
"relationships":{
"dataType":{
"links":{
"related":{
"href":"http://acme.com/ws/dataTypes/"
}
},
"data":[
{
"id":"string",
"type":"dataType"
},
{
"id":"date",
"type":"dataType"
},
{
"id":"integer",
"type":"dataType"
}
]
},
"field":{
"links":{
"related":{
"href":"http://acme.com/ws/fields/"
}
},
"data":[
{
"id":"name",
"type":"field"
},
{
"id":"dateOfBirth",
"type":"field"
},
{
"id":"ssn",
"type":"field"
}
]
}
}
}
From consumer perspective provide them only the required data in response and description in documentation.
And don't make separate call for providing more details, it will be very hard to maintain if you ever change version
Related
Excuse my English, I'm from Russia.
I asked this question in the Russian version SO, but they still haven't answered it.
There is a record collection that stores archival files. Here is its simplified structure (I omitted most of the attributes):
{
"_id": 1,
"tomes": [
{
"number":1,
"archive_number":1
},
{
"number":2,
"archive_number":1
}
]
}
{
"_id": 2,
"tomes": [
{
"number":1,
"archive_number":1
},
{
"number":2,
"archive_number":1
},
{
"number":3,
"archive_number":1
}
]
}
I need to remove the archive_number attribute from each of the nested documents of the tomes array for all documents in the record collection.
After deletion, the structure should look like this:
{
"_id": 1,
"tomes": [
{
"number":1,
},
{
"number":2,
}
]
}
{
"_id": 2,
"tomes": [
{
"number":1,
},
{
"number":2,
},
{
"number":3,
}
]
}
I was able to write a query like this:
db.record.update(
{
"tomes": {
$elemMatch:{
"archive_number":{$exists:true}
}
}
},
{
$unset: {
"tomes.$.archive_number":1
}
},
false, true
)
But this query only removes the archive_number attribute on one volume per archive case. I.e., after launch, we will see the following picture:
{
"_id": 1,
"tomes": [
{
"number":1,
},
{
"number":2,
"archive_number":1
}
]
}
{
"_id": 2,
"tomes": [
{
"number":1,
},
{
"number":2,
"archive_number":1
},
{
"number":3,
"archive_number":1
}
]
}
Can you please tell me how to delete all volumes? I don’t know how to correct the request, but my head doesn’t understand anymore.
Solution 1
With $[<indentifier>] (filtered positional operator) and arrayFilters to update the document(s) in the array.
db.collection.update({
"tomes": {
$elemMatch: {
"archive_number": {
$exists: true
}
}
}
},
{
$unset: {
"tomes.$[tome].archive_number": 1
}
},
{
arrayFilters: [
{
"tome.archive_number": {
$exists: true
}
}
],
multi: true
})
Sample Mongo Playground (Solution 1)
Solution 2
With $[] (all positional operator).
The all positional operator $[] indicates that the update operator should modify all elements in the specified array field.
db.collection.update({
"tomes": {
$elemMatch: {
"archive_number": {
$exists: true
}
}
}
},
{
$unset: {
"tomes.$[].archive_number": 1
}
},
{
multi: true
})
Sample Mongo Playground (Solution 2)
References
How the arrayFilters Parameter Works in MongoDB
I'm trying to create multiple documents at once in a Firestore collection with configuration purposes. I can create documents one by one sending from Postman:
{
"fields": {
"category":{"stringValue": "Configs"},
"order":{"integerValue":"3"}
}
}
I was wondering if there is some way to create multiple documents sending something like this:
{
"batch": [
{
"fields": {
"categoria": {
"stringValue": "Configurações"
},
"ordem": {
"integerValue": "3"
}
}
},
{
"fields": {
"categoria": {
"stringValue": "Configurações"
},
"ordem": {
"integerValue": "3"
}
}
}
]
}
Can anyone say if is it possible? If so, how can I do that?
Something like the below should work.
Although it is an "update" action is creates new documents as well if they don't exist. The document ID must be provided, it cannot be auto generated using the commit endpoint.
I was able to get my use case to work after viewing this response detailing use of commit.
API Endpoint: https://firestore.googleapis.com/v1/projects/projectID/databases/(default)/documents:commit
{
"writes": [
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/ExistingDocumentID1",
"fields": {
"comment": {
"stringValue": "Hello World!"
}
}
}
},
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/NewManualDocumentID2",
"fields": {
"comment": {
"stringValue": "Happy Birthday!"
}
}
}
}
]
}
Here is the sample JSON
Sample JSON:
[
{
"_id": "123456789",
"YEAR": "2019",
"VERSION": "2019.Version",
"QUESTION_GROUPS": [
{
"QUESTIONS": [
{
"QUESTION_NAME": "STATE_CODE",
"QUESTION_VALUE": "MH"
},
{
"QUESTION_NAME": "COUNTY_NAME",
"QUESTION_VALUE": "IN"
}
]
},
{
"QUESTIONS": [
{
"QUESTION_NAME": "STATE_CODE",
"QUESTION_VALUE": "UP"
},
{
"QUESTION_NAME": "COUNTY_NAME",
"QUESTION_VALUE": "IN"
}
]
}
]
}
]
Query that am using :
db.collection.find({},
{
"QUESTION_GROUPS.QUESTIONS.QUESTION_NAME": "STATE_CODE"
})
My requirement is retrive all QUESTION_VALUE whose QUESTION_NAME is equals to STATE_CODE.
Thanks in Advance.
If I get you well, What you are trying to do is something like:
db.collection.find(
{
"QUESTION_GROUPS.QUESTIONS.QUESTION_NAME": "STATE_CODE"
},
{
"QUESTION_GROUPS.QUESTIONS.QUESTION_VALUE": 1
})
Attention: you will get ALL the "QUESTION_VALUE" for ANY document which has a QUESTION_GROUPS.QUESTIONS.QUESTION_NAME with that value.
Attention 2: You will get also the _Id. It is by default.
In case you would like to skip those issues, you may need to use Aggregations, and unwind the "QUESTION_GROUPS"-> "QUESTIONS". This way you can skip both the irrelevant results, and the _id field.
It sounds like you want to unwind the arrays and grab only the question values back
Try this
db.collection.aggregate([
{
$unwind: "$QUESTION_GROUPS"
},
{
$unwind: "$QUESTION_GROUPS.QUESTIONS"
},
{
$match: {
"QUESTION_GROUPS.QUESTIONS.QUESTION_NAME": "STATE_CODE"
}
},
{
$project: {
"QUESTION_GROUPS.QUESTIONS.QUESTION_VALUE": 1
}
}
])
Why is the json code wrong? I know I can have multi key in XML, but it seem that json doesn't allow.
{
"BackupSettings": {
"Setting":
{
"id": "34345"
},
"Setting": {
"id": "16454"
}
}
}
Indeed, keys within an object are required to be unique in JSON. The canonical way of expressing your data in JSON would be to use an array. It could look something like the following:
{
"BackupSettings": {
"Settings": [
{
"id": "34345"
},
{
"id": "16454"
}
]
}
}
Or even:
{
"BackupSettings": [
{
"id": "34345"
},
{
"id": "16454"
}
]
}
The API endpoint I'm working with is returning data that has multiple nested relationships inside it, and I am using normalizeResponse() within DS.JSONAPISerializer to massage it into something that is fully JSON-API compliant.
The ember inspector shows that all data gets placed within its respective container correctly. The link between the top-level model and its hasMany child does work, but the link between the nested models does not work. I verify this by navigating within the inspector to the nested model's child model, clicking on it, and observing that its 'content' property is null.
First, take a look at how my models are set up:
// models/search.js
// i am able to browse from the search model to children with success
export default DS.Model.extend({
articles: DS.hasMany('article'),
});
// models/article.js
// i CANNOT browse from an article down to its digest in ember inspector
export default DS.Model.extend({
search: DS.belongsTo('search'),
type: DS.attr(),
created: DS.attr(),
updated: DS.attr(),
digest: DS.belongsTo('digest'),
});
// models/digest.js
export default DS.Model.extend({
title: DS.attr(),
desc: DS.attr(),
date: DS.attr(),
article: DS.belongsTo('article'),
});
Now, here's my modified JSON after my functions inside normalizeResponse complete. AFTER returning this data from normalizeResponse, the "digest" object under the parent "relationships" object disappears. Is there something wrong with my JSON? I've tried so many permutations of this with no success, and I am pretty sure this matches the JSON-API spec for Compound Documents.
{"data":{
"type":"searches",
"id":"17482738723",
"attributes":{
},
"relationships":{
"articles":{
"data":[
{
"type":"articles",
"id":"19988"
},
{
"type":"articles",
"id":"19989"
},
]
},
"digest":{
"data":[
{
"type":"digest",
"id":"19988_digest"
},
{
"type":"digest",
"id":"19989_digest"
},
]
}
}
},
"included":[
{
"id":"19988",
"type":"articles",
"attributes":{
"type": "internal",
"created":"2016-09-27T00:13:11.000Z",
"updated":"2016-09-27T00:13:11.000Z",
}
},
{
"id":"19988_digest",
"type":"digest",
"attributes":{
"title":null,
"desc":"four five six",
}
},
{
"id":"19989",
"type":"articles",
"attributes":{
"type": "internal",
"created":"2016-09-27T00:13:11.000Z",
"updated":"2016-09-27T00:13:11.000Z",
}
},
{
"id":"19989_digest",
"type":"digest",
"attributes":{
"title":"one two three",
"desc":null,
}
},
]
}
Your response indicates the following relationship model:
// models/search.js
export default DS.Model.extend({
articles: DS.hasMany('article'),
dignists: DS.hasMany('digest'),
});
// models/article.js
export default DS.Model.extend({
search: DS.belongsTo('search'),
});
// models/digest.js
export default DS.Model.extend({
search: DS.belongsTo('search'),
});
So you have to fix your response:
remove the digest relationship on the search
add a digest relationship to every article
So you will end with something like this:
{
"data":{
"type":"searches",
"id":"17482738723",
"attributes":{
},
"relationships":{
"articles":{
"data":[
{
"type":"articles",
"id":"19988"
},
{
"type":"articles",
"id":"19989"
},
]
}
}
},
"included":[
{
"id":"19988",
"type":"articles",
"attributes":{
"type": "internal",
"created":"2016-09-27T00:13:11.000Z",
"updated":"2016-09-27T00:13:11.000Z",
},
"relationships":{
"digest": {
"data": {
"type":"digest",
"id":"19988_digest"
}
}
}
},
{
"id":"19988_digest",
"type":"digest",
"attributes":{
"title":null,
"desc":"four five six",
},
"relationships":{
"digest":{
"data": {
"type":"digest",
"id":"19989_digest"
}
}
}
},
{
"id":"19989",
"type":"articles",
"attributes":{
"type": "internal",
"created":"2016-09-27T00:13:11.000Z",
"updated":"2016-09-27T00:13:11.000Z",
}
},
{
"id":"19989_digest",
"type":"digest",
"attributes":{
"title":"one two three",
"desc":null,
}
},
]
}
Know that you can also do it the other way around and specify the article on the digest. ember-data will automatically keep everything in sync. I personally prefer to specify both sides of the relationship for clarity.