There is no question: our goal is to build the the best OData v4 JavaScript tool, ever. There are a lot things to improve, but we release our next Community Technology Preview to show our latest results.
How to try the new OData v4 features
We published an OData v4 service endpoint backed by WebAPI OData, horray.
Thanks to JaySvcUtil, you can pre-generate your data model in node.js command prompt.
1 2 |
npm install -g jaysvcutil jaysvcutil -m <a href="http://odatav4-demo.jaystack.com:9000/odata/">http://odatav4-demo.jaystack.com:9000/odata/</a>$metadata -o context.js |
Arrow function support
JayData has improved predicates in Queryables. You can use arrow functions for example in filter conditions.
Old style
1 |
ctx.Articles.filter(function (it) { return it.Id > 5 }) |
New style – using arrow function
1 |
ctx.Articles.filter(it => it.Id > 5) |
If arrow function isn’t supported by your targeted browsers (older IE) then you can use it in string
1 |
ctx.Articles.filter('it => it.Id > 5') |
Advanced retrieval of related entities through $expand
You can use additional queryable methods on collections to filter/project navigation properties with arrays
Filtering array-valued navigation properties
1 |
ctx.Categories.include('c => c.Articles.filter(a => a.Id > 5)') |
Projecting array-valued navigation properties
1 |
ctx.Categories.include('c => c.Articles.map(a => a.Title)') |
Projecting and filtering array-valued navigation properties
1 |
ctx.Categories.include('c => c.Articles.filter(a => a.Id > 5).map(a => a.Title)') |
Expanding navigation properties of navigation properties
Basic retrieval:
1 2 |
ctx.Categories .include('c => c.Articles.Tags') |
Retrieval of multiple navigation properties at the same level
1 2 3 |
ctx.Categories .include('c => c.Articles.Tags') .include('c => c.Articles.Reviewer') |
Expanding pre-filtered navigation properties – result: you will get a list of categories that has a prefiltered article list that match the filter criteria; all article record will be loaded with Tags and Reviewer properties and its values:
1 2 3 4 |
ctx.Categories .include('c => c.Articles.filter(a => a.Id > 5)') .include('c => c.Articles.Tags') .include('c => c.Articles.Reviewer') |
Available nested queryable methods:
- filter
- map
- orderBy
- orderByDescending
- take
- skip
Deep insert
You can enable deep insert behavior through the following global configuration property in JayData
$data.defaults.OData.enableDeepSave
If true (default: false) then the provider will discover the deeply insertable entities to reduce the created request numbers
1 2 3 4 |
var category = new ctx.Categories.elementType({ Title: 'Javascript' }) var deepInsertArticle = new ctx.Articles.elementType({ Title: 'Deep insert', Lead: 'Lead', Body: 'body', Category: category }) ctx.Articles.add(deepInsertArticle) ctx.saveChanges(); |
The result will be only one POST request.
Technical depth: computed fields (such as auto-incremented IDs) of deep-inserted entities aren’t returned by current version of ASP.NET WebAPI OData
Function parameter resolution (isn’t enabled by default yet)
There is an other usage of parameters in Queryable predicates
Old style
1 |
ctx.Articles.filter(function(it){ return it.Title == this.p1 && it.Lead == this.p2 }, {p1: 'Title', p2: 'Lead'}) |
New style
1 |
ctx.Articles.filter(function(it, p1, p2){ return it.Title == p1 && it.Lead == p2 }, {p1: 'Title', p2: 'Lead'}) |
You can use function parameters in arrow functions, so it is equivalent with:
ctx.Articles.filter((it, p1, p2) => it.Title == p1 && it.Lead == p2 }, {p1: ‘Title’, p2: ‘Lead’})
To enable this feature with the following global setting (default: true) :
$data.defaults.parameterResolutionCompatibility = false;
Managing entity relationships
Creating a relationship between entities in .NET
How to set entity references in JayData
There is a global configuration property: $data.defaults.OData.withReferenceMethods
If true (true by default) then OData provider provider will create an additional POST request what handled by this CreateRef action to set navigation property while calling saveChanges() method
1 2 3 4 |
var product = products[0]; ctx.Products.attach(product); product.Supplier = suppliers[0]; ctx.saveChanges(); |
Removing a relationship between entities
How to remove relationships between entities in JayData
There is a configuration property
$data.defaults.OData.withReferenceMethods
If true (default: true) then the provider will create an additional DELETE request what handled by this DeleteRef action to clear navigation property while calling saveChanges() method
1 2 3 4 |
var product = products[0]; ctx.Products.attach(product); product.Supplier = null; ctx.saveChanges(); |
Download JayData 1.5.1 CTP and try all these features!