Getting Distinct or FirstOrDefault with Breeze Client-Side - breeze

A query already executed with breeze on the client side and I store the results locally. I was really hoping to either .Distinct() or .FirstOrDefault() on the client side so that I can execute locally instead of going back to the server.
I know that breeze has limited capabilities on the client side. I've looked through the samples and no luck there.
http://www.breezejs.com/documentation/query-examples
Can breeze do this? Is this something that they will be doing in the future?

There's nothing yet built into breeze that will do this. I've run into the same thing, and had to call .Distinct() and .FirstOrDefault() on the server side. There is an outstanding suggestion from last year in the breeze forums (currently at #10 on the list by vote count) to add .Distinct()-like functionality.

I need to see your query to answer properly.
In almost all cases, take(1) is equivalent to firstOrDefault and works fine on both remote and local queries.
var query = breeze.EntityQuery.from('Persons')
.where('FirstName', 'eq', 'Lizzy')
.take(1); // will return one or null
If you want to find an entity by its key, looking first in the cache and then going remote if necessary, then you should consider the fetchEntityByKey method.
Distinct only makes sense for projection queries in which the objects returned (which are NOT entities, btw) have no id. Can't know if that is what you need until you show me your query.
With due respect, I feel that hunch_hunch's answer is a bit misleading. Please reconsider checking MY ANSWER as the best answer.

Related

What's the most efficient way to query Microsoft Graph events by id list?

Currently, in my code, I am taking a list of stored Event ids and fetching them like this:
results = await Task.WhenAll(
criteria.Identifiers
.Skip(offset).Take(pageSize)
.Select(i => userEvents[i].Request(options).GetAsync())
.ToList());
This works fine, but it makes pageSize number of calls to the API, which seems less than ideal. Is there a way to bundle these identifiers into a single API call?
My question proved similar to another:
Getting multiple users/groups by objectids
The answer, there, suggests this feature might be coming, but isn't here, yet. If anyone knows more about the timeline, please add another answer or comment.

Using custom metadata in the result of OData queries

I wonder if it's possible to use custom metadata within the result of OData queries (at the entity level). I think of metadata such as scores since the query executed on an ElasticSearch database and I would like to provide these hints to the end user.
Thanks very much for your help!
Thierry
Sorry I'm not familiar with ElasticSearch. But if I understand your questions correctly, you want some information other than the raw data in the request result ? If so, there is such things called Instance Annotation that may satisfy your need. An explanation from OData spec.
HTH

Cant update table in using isset

I have a table called settings, when I would change or enter data into the form it did not change the data in the table. In addition on form an image upload file is not running, There may be the wrong code below.
(Solved by me)
Maybe someone can help me Related to this.
What you are doing here is tottaly in secure and your data can be hacked / manipulate really fast.
Why dont you use a framework like codeignighter there are about 100 easy frameworks that will help you manage database a lot easyer.
Are you sure that you are updating the wrond ID? where id = 1, seems to be not dynamic.
Please post your error http://www.w3schools.com/php/func_mysql_error.asp
I know it is not so related to your question, but you should see these light frameworks:
http://kohanaframework.org/
https://github.com/ElbertF/Swiftlet
http://ellislab.com/codeigniter
You're not checking the return status of of your query, so if it's not working you wouldn't know. Do this:
mysql_query("UPDATE settings SET site='$name',keywords='$keys',descrp='$desc',email='$email',fbpage='$fbpage',twitter='$twitter',gplus='$gplus',disclaimer='$disclaimer',template='$template' WHERE id=1")
or die(mysql_error());
Note: mysql_*() is deprecated: you shouldn't use it. Use mysqli_*() or PDO instead.
Also: You are susceptible to an SQL Injection attack. You should escape your input variables with mysql_real_escape_string() (or the equivalent if you switch to mysqli), or consider moving to prepared statements.

method timeout instead of waiting

It would appear that the savechanges method on breeze waits indefinitely when calling to or waiting for the server. Is there a way of getting it to time out? I am calling save change with allowConcurrentSaves: false. This now causes users who somehow do not get a response from the server to simply hang in limbo indefinitely say for example with a dropped internet connection.
I do not want to re-call the method with allowConcurrentSaves to false fearing that I might duplicate the data.
Any ideas?
Thanks
Update 16 May 2014
You can set HTTP-level timeout and cancellation with the AJAX Adapter's requestInterceptor as of v.1.4.12. See the documentation, "Controlling AJAX calls".
I'd still be reluctant to use this feature on save as you have no chance of knowing what whether the server persisted the data or not. Of course if your client hangs or crashes you don't know anyway. It's up to you.
Original Answer
Actually, there is a ready-made solution from Q.js. It's called timeout and it's mentioned in the API reference with a simplified example of its implementation and use in the readme.md.
I know you asked about Save but your question is pertinent for promises in general. Here is a query example adapted from the queryTests.js in our DocCode Sample
var timeoutMs = 10000; // 10 second timeout
var em = newEm(); // creates a new EntityManager
var query = new EntityQuery().from("Customers").using(em);
Q.timeout(query.execute, timeoutMs)
.then(queryFinishedBeforeTimeout)
.fail(queryFailedOrTimedout);
function queryFailedOrTimedout(error) {
var expect = /timed out/i;
var emsg = error.message;
if (expect.test(emsg)) {
log("Query timed out w/ message '{0}' " + expectTimeoutMsg)
.format(emsg));
// do something
} else {
handleFail(error);
}
}
Note: I just added this test so you'd have to get if from github or wait for a Breeze release after 1.2.5.
Oops ... maybe not
I gave what I think is a great answer for query. It may not be the right answer for save.
The problem with save is that you do not know on the client if the save succeeded until the server responds. Things could go wrong anywhere along the way. The server might not have heard the request to save. The server may have failed during save. The server may have saved the data but the response never made it back to the client.
Changing the value of allowConcurrentSaves won't get you out of this bind. Neither will having a save timeout.
In fact, adding a timeout to the save is probably deceiving. It is even possible for the save response to arrive after your custom timeout ... in which case Breeze will have tried to update your EntityManager ... and you won't know if Breeze succeeded or failed!
What if we added a Breeze save timeout. What should it do? What if breeze said the save had timedout ... and Breeze ignored a belated response from the server? Then imagine that the save succeeded on the server - it just took "too long" for it to respond to the client. Now you've got a client whose state is unexpectedly out of sync with the server. This is not good.
So I think you want a different solution to this very real problem. It's a user experience problem really. You can indicate to the user that you think the save is still in progress and then set your own timer. If the save isn't done when your timer expires, you can query the server to see if the data have been saved or if there is a connection ... or something along these lines. I can't think of a better way right now honestly.
Note that I'm assuming you need to know that the server succeeded. If you avoid store-generated IDs and always assume saves succeed unless the server tells you otherwise ... well that's a completely different paradigm and programming model that we could talk about someday (see meteorjs).
The net of all of this: I'm pretty darned sure that a save timeout is NOT what you want.
Still useful on a query though :)
Great question, and I wish I had a good answer. But it is definitely worth looking into. Could you please add this as a feature request to the Breeze User Voice. We take these requests very seriously in determining our priorities for Breeze development.

Entity Framework Stored Procedure Mapping HELP!

Seems my appreciation for the Entity Framework is taking a serious hit. The "MS almost got it right, but they just missed it because of something L-A-M-E" thought is coming up. Until today everything has been fine. For some unknown reason, it won't compile anymore with Error 2048. I've read up on this and I've seen how you need to map all three operations. Why is this even necessary? What I if I don't need a delete function and only need insert and update? I tried mapping a dummy SP to my delete function. If that fixes my problem, however cheezy, fine. Only problem is, it just created more problems.
Here's what I have. I'm writing a simple newsletter app in MVC. I have entities for a publication, issue and article all generated from my DB (SQL 08). I set up the the relationships in my DB and they translated fine to my EDMX. I made some SPs to insert and update my issues and article. I added them to the EDMX and mapped them accordingly. I don't need a delete function for any of them and I don't need anything for the publication entity. Why is the compiler forcing me to map all functions? IMO, this a MAJOR, MAJOR PROBLEM with EF4 and I can't believe MS would release it with this kind of crap coming up.
The other strange issue is I've tried mapping sp's to entities in another project and configured with only insert and update and they compile fine. Why is the compiler inconsistent?
I would rather not resort to having to use the Imported Functions. Is that my only option? If that's the case it eliminates the ability to the SaveChanges method. Come On MS!!! Fix this!!!!!!!
After much digging and side by side text comparison, I think I have found the solution.
The SP in question was set up like
CREATE PROCEDURE updateArticle
(
#ArticleID INT,
#Content TEXT
)
where it should have been something like
CREATE PROCEDURE updateArticle
(
#ArticleID INT,
#IssueID INT,
#Content TEXT
)
Now, I still don't know why EF4 even requires this since I'm not updating the issueid and the error message lends little help in diagnosing the problem. My SP doesn't even use the IssueID, but EF needs it regardless. Hopefully this will help someone down the road. MS still could do a better job regarding the need for this.

Resources