Breeze is a JavaScript library that helps you manage data in rich client applications. If you store data in a database, query and save those data as complex object graphs, and share these graphs across multiple screens of your JavaScript client, Breeze is for you.
2. What is ?
JS library aimed to manage data in rich client applications
- It’s client side LINQ to SQL
- Mirror server-side model to client side dynamically (eg :- Entity Framework Objects)
- No need of creating client side Models for every server side entity.
Implements the Open Data Protocol (OData) query standard
- If a query can be expressed in OData syntax, it can be expressed in Breeze
- http://www.example.com/api/Northwind/Customers?$filter=startswith(CompanyName,'C') eq true&$orderby=CompanyName
Ships with adapters for the ASP.NET WebAPI for Odata
4. Why ?
It’s free
Simplify data query tasks (LINQ style)
Manage client side data caching
Support batch operations (saveChanges())
Support all modern browsers on desktop and mobile devices (ECMAScript 5)
Out-of-box support for Knockout, Angular, and Backbone
Well documented
Intellisense support
5. Key features of
Query like LINQ
/* Query like LINQ */
// Define query for customers
// starting with 'A', sorted by name,
var query = breeze.EntityQuery
.from("Customers")
.where("CompanyName", "startsWith", "A")
.orderBy("CompanyName");
//... execute it later ...
6. Key features of
Query like LINQ
Async with Promises /* Async query with promises */
// Execute query asynchronously on remote server
// returns a promise ... with success/fail callbacks
var promise = manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
/* Async save with promises */
// Batch asynchronous save of all entities w/ pending
changes
// returns a promise ... with success/fail callbacks
var promise = manager.saveChanges()
.then(saveSucceeded)
.fail(saveFailed);
7. Key features of
Query like LINQ
Async with Promises
Change tracking
/* Change tracking */
// save all changes (if there are any)
if (manager.hasChanges()) {
manager.saveChanges()
.then(saveSucceeded)
.fail(saveFailed);
}
// listen for any change to a customer
customer.entityAspect.propertyChanged
.subscribe(somethingHappened);
// listen for validation errors
customer.entityAspect.validationErrorsChanged
.subscribe(updateValidationUI);
8. Key features of
Query like LINQ
Async with Promises
Change tracking
Knockout/Angular data binding
<!-- Knockout template -->
<ul data-bind="foreach: results">
<li>
<span data-bind="text:FirstName"></span>
<span data-bind="text:LastName"></span>
</li>
</ul>
// bound to employees from query
manager.executeQuery(breeze.EntityQuery
.from("Employees"))
.then(function(data){
ko.applyBindings(data);
});
9. Key features of
Query like LINQ
Async with Promises
Change tracking
Knockout/Angular data binding
Query with related entities
/* Query with related entities using expand */
// query for orders of customers whose name begins
"Alfreds"
// include their customers & child details & their
detail products
breeze.EntityQuery.from("Orders")
.where("Customer.CompanyName", "startsWith", "Alfre
ds")
.expand("Customer, OrderDetails.Product")
.using(manager)
.execute().then(querySucceeded).fail(queryFailed);
10. Key features of
Query like LINQ
Async with Promises
Change tracking
Knockout/Angular data binding
Query with related entities
Navigate to related entities
/* Navigate to related entities in cache */
// Query success callback
function querySucceeded (data) {
var order1 = data.results[0];
// Parent customer of the order
var customer = order1.Customer();
// Product of the first OrderDetail of the order
var product1 = order1.OrderDetails()[0].Product();
}
11. Key features of
Query like LINQ
Async with Promises
Change tracking
Knockout/Angular data binding
Query with related entities
Navigate to related entities
Flatten entity graphs
/* Flatten entity graphs */
// Projection query: "select" flattens the Orders to 4
fields.
// Order id, ship date, & customer name of Orders w/
freight > $500
breeze.EntityQuery.from("Orders")
.where("Freight", ">", 500)
.select("OrderId, Customer.CompanyName,
ShippedDate")
.orderBy("Customer.CompanyName, ShippedDate");
12. Key features of
Query like LINQ
Async with Promises
Change tracking
Knockout/Angular data binding
Query with related entities
Navigate to related entities
Flatten entity graphs
Query server or cache
/* Query the server, cache, or both */
var query = breeze.EntityQuery("Customers");
// execute query asynchronously on the server
manager.executeQuery(query).then(querySuccess).fail(qu
eryFail);
// execute query synchronously on local cache
var customers = manager.executeQueryLocally(query)
// fetch customer by id from cache (if found) or
remotely
var checkCacheFirst = true;
manager.fetchEntityByKey("CompanyName", 42,
checkCacheFirst)
.then(fetchSuccess).fail(fetchFail);
13. Key features of
Query like LINQ
Async with Promises
Change tracking
Knockout/Angular data binding
Query with related entities
Navigate to related entities
Flatten entity graphs
Query server or cache
Save changes offline
/* Save changes offline; restore later */
var changes = manager.getChanges();
var exportData = manager.exportEntities(changes);
window.localStorage.setItem("changes", exportData);
// ... later ...
var importData =
window.localStorage.getItem("changes");
manager.importEntities(importData);