breeze appears to load metadata every query - breeze

Consider the code snippet below ... the WebApi controller Metadata method is called both times for executeQuery below ... Why?
Thanks,
Travis
var manager = (typeof options.breezeController !== 'undefined') ? options.breezeController : Mosaic.Global.defaultBreezeManager();
var query = breeze.EntityQuery
.from("GetColonies")
//.select("VIVLINE_GUID, VIVLINE_NAME")
.orderBy("VIVLINE_NAME");
manager.executeQuery(query);
manager.executeQuery(query)
.then((data) => {
this.viewModel.items.removeAll;
this.prepData(data.results);
this.viewModel.setSelectedValue(selectedModel);
});

Breeze checks if the metadata exists on the client for a given service before each query. If the metadata is not present then it will ask for it before executing the query.
What I am guessing is happening in your case is that both queries start before either returns metadata. This will cause metadata to be fetched more than once. However, once it does make it down you shouldn't see any further requests.
One suggestion would be to force the loading of metadata before any query, i.e.
manager.fetchMetadata().then(function() {
manager.executeQuery(query1);
manager.executeQuery(query2);
}

Related

SAPUI5 ODataModel metadata $format=xml

i am trying to connect sapui5/openui5 ODataModel to an odata-server. I want to use a nodejs server with package simple-odata-server. Unfortunately this odata server provides metadata only in xml-format. But sapui5 tries to load metadata in json-format.
Before i switch to another odata server, i want to check, wether sapui5 can load metadata in xml-format. I tried to create the model with several parameters, but ODataModel still tries to load metadata as json.
var oModel = new ODataModel("/odata", {
"metadataUrlParams": "$format=xml",
"json": false
});
Does anybody know, wether i can switch to $format=xml
Thanks in advance,
Torsten
As far as I know the OData protocol metadata is always provided as XML, never seen metadata in JSON format. Also my n-odata-server Qualiture mentioned in the comment above does it. But I never had problems with SAPUI5. It requests the metadata, gets the xml stream and works with it.
Since the metadataUrlParams parameter is of type map I'd suppose it would at least do what you intend like this:
var oModel = new ODataModel("/odata", {
"metadataUrlParams": {
"$format": "xml"
}
});
https://sapui5.hana.ondemand.com/sdk/#docs/api/symbols/sap.ui.model.odata.ODataModel.html#constructor

get 'undefined' saveResult.entities after saveChanges [breezejs]

I tried to update one entity in my angularjs client using breezejs library. After calling saveChanges(), it can actually save back in the server and fetched on the client. However, the server did not return the response back. The saveResult.entities is undefined and pop up an error for me. When I took a look at the docs, it mentions 'Some service APIs do not return information about every saved entity. If your server doesn't return such information, you should add the pre-save, cached entity to saveResult.entities yourself'. Could anyone provide an example of how to do this?
This is the code when i am trying to do an update.
manager.saveChanges(entitiesToSave, null, (saveResult) => {
const savedRes = saveResult;
savedRes.entities = entitiesToSave;
return savedRes;
}).then(saveSucceeded);
On the server, you would need to construct the response for an update similar to the way it is for a create:
response.setContent(...); // entities
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());

Mongo and Node.js: unable to look up document by _id

I'm using the Express framework and Mongodb for my application.
When I insert objects into the database, I use a custom ObjectID. It's generated using mongo's objectid function, but toString()ed (for reasons i think are irrelevant, so i won't go into them). Inserts look like this:
collection.insert({ _id: oid, ... })
The data is in mongo - i can db.collection.find().pretty() from mongo's cli and see it exactly as it's supposed to be. But when I run db.collection.find({_id: oid}) from my application, I get nothing ([] to be exact). Even when I manually hardcode the objectID that I'm looking for (that I know is in the database) into my query, I still can't get an actual result.
As an experiment, I tried collection.find({title: "test title"}) from the app, and got exactly the result I wanted. The database connection is fine, the data structure is fine, the data itself is fine - the problem is clearly with the _id field.
Query code:
var sid = req.param("sid");
var db = req.db;
var collection = db.get("stories");
collection.find({'_id': sid }, function(err, document) {
console.log(document.title);
});
Any ideas on how I can get my document by searching _id?
update: per JohnnyHK's answer, I am now using findOne() as in the example below. However, it now returns null (instead of the [] that i was getting). I'll update if I find a solution on my own.
collection.findOne({'_id': sid }, function(err, document) {
console.log(document);
});
find provides a cursor as the second parameter to its callback (document in your example). To find a single doc, use findOne instead:
collection.findOne({'_id': sid }, function(err, document) {
console.log(document.title);
});

The Breezejs ZUMO example returns the inlineCount as undefined

I am trying to get the inlinecount from a breeze query. The results will come back, but the inlineCount property will be undefined. I captured the breeze query and results in Fiddler and they seem to be correct. The server returns the values in Fiddler with a count property added the the json results. I am getting the same results with my own app.
// Get all TodoItems from the server and cache combined
function getAllTodoItems() {
// Create the query
var query = breeze.EntityQuery.from('TodoItem')
.inlineCount();
// Execute the query
return manager.executeQuery(query)
.then(success).catch(queryFailed);
function success(data){
// Interested in what server has then we are done.
var fetched = data.results;
$log.log('breeze query succeeded. fetched: '+ fetched.length);
// Blended results.
// This gets me all local changes and what the server game me.
return manager.getEntities(todoItemType);
// Normally would re-query the cache to combine cached and fetched
// but need the deleted entities too for this UI.
// For demo, we returned every cached Todo
// Warning: the cache will accumulate entities that
// have been deleted by other users until it is entirely rebuilt via 'refresh'
}
}
The inlineCount property will be added by Breeze if you decorate your WebApi Get method with the BreezeQueryableAttribute or if you decorate the WebApi controller with the BreezeControllerAttribute. Otherwise, you can get the count by calling data.httpResponse.data.count.
You can prove this in the todo sample by launching the WebApi server and setting the uzeZumo variable to false in the config.js file. This will fetch the records from the WebApi. By default it'll fetch the data from Zumo.
Hope this helps.

Breeze: how to tell if the query returned is from local cache or remote server?

I know you can call this function to query locally, if possible, or remotely. Is there a way to tell if the result is from local or remote? I remember I read something about it, but couldn't find it anymore.
manager.fetchEntityByKey('Employee', employeeID, true)
.then(function(data) {
employee(data.entity);
});
Use the 'fromCache' boolean property on the promise result.
manager.fetchEntityByKey('Employee', employeeID, true).then(function(data) {
var employee = data.entity
var wasFoundInCache = data.fromCache;
});
See: http://www.breezejs.com/sites/all/apidocs/classes/EntityManager.html#method_fetchEntityByKey

Resources