Building a react native app using CosmosDB and it's SQL api.
Per their documentation, I can add an object to a container like this:
const CosmosClient = require('#azure/cosmos').CosmosClient;
const client = new CosmosClient({ endpoint, key });
const myNewObject = {foo: "bar"}
await client
.database(databaseId)
.container(containerId)
.items.create(myNewObject);
And I can confirm this works.
What I'm trying to do tho, is place data into that {foo: "bar"} document that already exists.
So far I've tried chaining the .item method, but it does't work.
await client
.database(databaseId)
.container(containerId)
.item(idOfMyNewObject) // The existing object I want to create a child in
.item('myNewChildObject') // new child of the parent
.create(newEntry); // new entry in the new child
Any ideas? the documentation doesn't seem to talk about this.
There's no method on the container that does this for you. You must define the structure of the data yourself in your code. Here is an example of a customer record which has both an array of addresses as well as an embedded object.
const customer = {
"id": "000242A2-BF40-4220-864B-2770CAA38F5D",
"type": "customer",
"customerId": "000242A2-BF40-4220-864B-2770CAA38F5D",
"title": "",
"firstName": "Timothy",
"lastName": "Kelly",
"emailAddress": "timothy4#adventure-works.com",
"phoneNumber": "193-555-0189",
"creationDate": "2014-03-14T00:00:00",
"addresses": [
{
"addressLine1": "9918 Scottsdale Rd.",
"addressLine2": "",
"city": "Novato",
"state": "CA ",
"country": "US",
"zipCode": "94947",
"location": {
"type": "Point",
"coordinates": [
-122.764,
38.0852
]
}
}
],
"password": {
"hash": "LvEbgjonEPU11HEeSXqdzTsmqNeUfuhxBNL82vGlCWA=",
"salt": "61626BAE"
},
"salesOrderCount": 1
}
Related
Backend API's:
url: http://www.sample.com/getAllPersons
[
{
"name": "ABC",
"occupation": "Student",
"address_url": "http://www.sample.com/address/person=hgjgjgyyfhg"
},
{
"name": "ABC1",
"occupation": "Carpenter",
"address_url": "http://www.sample.com/address/person=fsdafdsa"
},
{
"name": "ABC2",
"occupation": "Developer"
},
{
"name": "ABC3",
"occupation": "Tester",
"address_url": "http://www.sample.com/address/person=sgdfsgd"
}
]
url: http://www.sample.com/address/person=hash_value
{
"address": "XYZ",
"city": "Phoenix",
"state": "Arizona",
"pin code": "3243242"
}
Need array of objects:-
{
"name": "ABC",
"occupation": "Student",
"address": "XYZ",
"city": "Phoenix",
"state": "Arizona",
"pin code": "3243242"
}
I need to show in a view the name, occupation and full address of every person with the above two api urls.
Note: Every person's address may not be available.
Please suggest the best method to do this asynchronously using HTTP Service and Promises/Observables in Angular 2.
My Solution:
Create http request promise of getAllPersons.
In then function:
Save the response in a component class variable.
Create array of http promises of every object's address_url (if it is present).
Return Promise.all([Array of address url promises])
In then function:
Iterate responses one by one and add them to the component class
variable containing address_url.
Is there any better way? Also suggest if this was a three way hierarchy.
Use flatMap and forkJoin:
var o = http.get('http://www.sample.com/getAllPersons')
.flatMap(t=> Observable.forkJoin(
t.json().filter(x=>x.address_url != null)
.map(y=> http.get(y.address_url).map(t=>json()))
));
Subscribe like this:
o.subscribe(arr=> {
//arr[0] --> address_url[0]
//arr[1] --> address_url[1]
...
});
I am using $firebaseArray for collecting the data from Firebase. Output is as follows:
[
{
"BankAccount": {
"AccountHolder": "Tom Antony",
"AccountNumber": "56767887"
},
"Info": {
"BillingAddress": {
"City": "XYZ",
"State": "ABC"
},
"FullName": "Tom Antony",
"PhoneNumber": "634762347"
},
"$id": "dGUZX5SWi7aP0SNYLYqEiMdCYAS2",
"$priority": null
},
{
"Campaigns": {
"Settings": {
"Active": true
}
}
},
"Info": {
"BillingAddress": {
"City": "ABC",
"State": "DFG"
},
"FullName": "Mario",
"PhoneNumber": "634762347"
},
"$id": "tBqGZ7g6VwNYOWoVy7C1FHKZKFS2",
"$priority": null
}
]
My js is as follows:
const rootRef = firebase.database().ref().child('Users');
$scope.users = $firebaseArray(rootRef);
Each Array element will have different type of objects, but each will have a similar object called Info, which contains a field for FullName I need to apply ng-repeat on this FullName. My implementation is as shown below:
<div ng-repeat="user in users.Info">
<p ng-bind="user.FullName"></p>
</div>
But its not working. What are the mistakes I made here?
You are trying to iterate on the wrong property. You have two objects (user) which each have an Info property that you want the FullName from. You do not have two Info objects. Therefore, you should be iterating over the users, not iterating over users.Info.
Try this instead:
<div ng-repeat="user in users">
<p ng-bind="user.Info.FullName"></p>
</div>
Also, your object you pasted here wasn't valid JSON, you had an extra }.
Full Working example: http://plnkr.co/edit/ryfjhO0c3egHfKnaL9SQ?p=preview
I'm an Italian PouchDb and AngularJS Developer.
My json document is:
{
"_id": "6",
"_rev": "3-f7283d7683cd6fb15753f494aad1d49f",
"name": "Ivrea",
"owners": [
{
"owner_id": 1,
"name": "asdas",
"address": "asdas",
"gender": "Uomo",
"type": "Assente",
"notes": [
]
},
{
"owner_id": 2,
"name": "balbaba",
"address": "blabla",
"gender": "Uomo",
"type": "Assente",
"notes": [
]
}
]
}
and after an ng-click action, I will delete owner_id: 2 object inside _id: 6 document. In API reference I found only document delete action, but not how to delete object inside document.
Thanks for your reply!!
Alessandro
You just need to put() the main document back in the database after you remove an object from it. :)
db.get('foo').then(function (doc) {
delete doc.whatever;
return db.put(doc);
}).catch(function (err) { /* ... */ });
department {
"_id": "1",
"department": "Computers",
"type": "Department",
"room_no": "102",
"HOD": "Mr. G Rahul",
"floor": "1st Floor"
}
student {
"_id": "fdf370e2f43d4af1b505b8913502a5e4",
"_rev": "1-16df9a4cd45ca69009ab6c9767425a8e",
"student Name": "H Ravi",
"date_of_birth": "March 1, 1993",
"roll_no": "55",
"inter_marks": "820",
"secondary_marks": "420"
"department_id": "1",
"type": "student"
}
Map Function
function(doc) {
var id,department,student,hod,dob;
if(doc.type == 'student') {
id = doc.department_id;
dob = new Date(doc.date_of_birth)
student = doc;
}
}
emit(dob, {'_id': id,"student_doc": student});
}
After writing map function we call view by using URL "//localhost:5984/db_name/_design/design_name/_view/view_name". In that URL we will append ?include_docs=true after "view_name"("//localhost:5984/db_name/_design/design_name/_view/view_name/?include_docs=true") to get the docs of by using _id in emit, example: emit(dob,{"_id": id}) it will return the docs of linked id...My question is how can we access that docs in reduce function.
You can’t, the docs are fetched on query time, not on indexing time, so the reduce function never gets to see that data. Sorry!
First, I should point out that I verified my JSON object with http://jsonlint.com and it is, indeed, valid.
Now that is out of the way, I'm looking at examples of the YUI DataTable, specifically the datasource and the structure of the JSON objects the examples use (see http://developer.yahoo.com/yui/examples/datatable/dt_basic.html).
The Basic Example uses a DataSource composed as follows:
YAHOO.example.Data = {
bookorders: [
{id:"po-0167", date:new Date(1980, 2, 24), quantity:1, amount:4, title:"A Book About Nothing"},
{id:"po-0783", date:new Date("January 3, 1983"), quantity:null, amount:12.12345, title:"The Meaning of Life"},
{id:"po-0297", date:new Date(1978, 11, 12), quantity:12, amount:1.25, title:"This Book Was Meant to Be Read Aloud"},
{id:"po-1482", date:new Date("March 11, 1985"), quantity:6, amount:3.5, title:"Read Me Twice"}
]
}
Whereas my JSON object looks like this:
[
{
"Listing": {
"Name": "Jay",
"Address": "Main Street",
"City": "New York"
}
},
{
"Listing": {
"Name": "Thomas",
"Address": "Union Street",
"City": "New York"
}
},
{
"Listing": {
"Name": "Jason",
"Address": "Square Street",
"City": "Boston"
}
}
]
Here is how Yahoo's example specifies the datasource and a few other lines tied to it:
var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.bookorders);
myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
myDataSource.responseSchema = {
fields: ["id","date","quantity","amount","title"]
};
In my JSON object, each "Listing" would be a row in the YUI DataTable. What do I need to modify in the YUI code to make it work with my JSON object?
Thank you.
Above you have defined an object, with an unnamed array of objects, each object is composed of another object, with members. While this might be valid JSON, I don't think this is compatible with the expectations of the YUI datatable. It is more of a contrived or obfuscated challenge.
I am unable to provide a way, using the existing JSON object. While your JSON is valid, IMHO, I do not believe it to be compatable with the YUI datatable.
I think you need an object containing a named array of objects that have members, not other objects. There are too many layers in the existing data structure, that serve no apparent purpose, to me.
'Change', below, implies changing the basic datatable example, provided by YAHOO.
Simply restructuring your data like so,
YAHOO.example.Data = {
Listing: [
{
"Name": "Jay",
"Address": "Main Street",
"City": "New York"
},
{
"Name": "Thomas",
"Address": "Union Street",
"City": "New York"
},
{
"Name": "Jason",
"Address": "Square Street",
"City": "Boston"
}
]
};
will simplify your data structure and make this work. This is the minimum change, I believe, considering the constraints.
Then change the datasource:
var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.Listing);
and the column defs
var myColumnDefs = [
{key:"Name"},
{key:"Address"},
{key:"City"}
];
and finally the response schema
myDataSource.responseSchema = {
fields: ["Name","Address","City"]
};
Hope that helps.