ODataAngularResources is a fork of Angular's $resource that allows to make OData queries in a fluent way. It does everything Angular Resources does but add some features:
bower install angular-odata-resources --save
or
npm install angular-odata-resources --save
var myModule = angular.module('myModule',['ODataResources']);
myModule.controller('MyController', ['$scope','$odataresource',function($scope,$odataresource){}]);
var User = $odataresource('/user/:userId', {userId:'@id'});
var myUsers = User.odata()
.filter("Name","John")
.query();
//Queries /user?$filter=Name eq 'John'
The first parameter of the filter method is assumed to be the property and the second to be the value. But that behavior can be overriden by passing special parameters. See advanced queries for more informations.
Returned objects from the query() method are regular Resource object which can be used for saving, updating etc...
myUsers[0].$save();
var myUsers = User.odata()
.query(function(){
console.log("Everything went ok!")
},function(){
console.log("Oops, something wrong happened!")
});
var userId = 10;
var myUsers = User.odata().get(userId);
//Queries /user(10)
var userId = 10;
var myUsers = User.odata().get(userId,
function(){
console.log("Everything went ok!")
},function(){
console.log("Oops, something wrong happened!")
});
If you want to retrieve a single element after a query you can use the single() method which will take the first elements of the response. This method will throw if the reponse returns an empty array.
var User = $odataresource('/user/:userId', {userId:'@id'});
var myUser = User.odata()
.filter("Name","John")
.single();
//Queries /user?$filter=Name eq 'John' and put the first element into myUser
var User = $odataresource('/user/:userId', {userId:'@id'});
var myUsers = User.odata()
.filter("Name", "John")
.filter("Age",">",20)
.skip(10)
.take(20)
.orderBy("Name","desc")
.query();
//Queries /user?$filter=(Name eq 'John') and (Age gt 20)&$orderby=Name desc&$top=20&$skip=10
var data = User.odata().filter('name','bob').count();
//Queries /user/$count/?$filter=name eq 'bob'
// data.result == 25
var users = User.odata().withInlineCount().query();
//Queries /user?$inlinecount=allpages
// users is an array but also contains the count property
// The server may reply by
// {
// "@odata.context": "http://host/service/$metadata#Collection(Edm.String)",
// "value": [{
// name: 'Test',
// id: 1,
// }, {
// name: 'Foo',
// id: 2,
// }],
// 'count': 10
// }
// And then, the count will be defined as followed
// users.count == 10
var User = $odataresource('/user/:userId', {userId:'@id'});
var myUsers = User.odata()
.expand("City")
.query();
//Queries /user?$expand=City
var User = $odataresource('/user/:userId', {userId:'@id'});
var myUsers = User.odata()
.expand("City","Country")
.query();
//Queries /user?$expand=City/Country
var myUsers = User.odata()
.expand("City")
.expand("Orders")
.query();
//Queries /user?$expand=City,Orders
var User = $odataresource('/user/:userId', {
userId: '@id'
});
var users = User.odata().select(['name','userId']).query();
//OR
var users = User.odata().select('name','userId').query();
//Queries /user?$select=userId,name
User = $odataresource('/user/:userId',
{ userId: '@id'},
{
odata: {
method: 'POST',
url: '/myCustomUrl'
}
}
);
var myUsers = User.odata()
.format("json")
.expand("City")
.expand("Orders")
.query();
//Queries /user?$format=json&$expand=City,Orders
myModule.controller('MyController', ['$scope','$odataresource','$odata',function($scope,$odataresource,$odata){}]);
var predicate1 = new $odata.Predicate("FirstName", "John");
var predicate2 = new $odata.Predicate("LastName", '!=', "Doe");
//
combination = $odata.Predicate.or([predicate1,predicate2]);
User.odata().filter(combination).query();
//Queries /user?$filter=(FirstName eq 'John') or (LastName ne 'Doe');
var predicate1 = new $odata.Predicate("FirstName", "John");
var predicate2 = new $odata.Predicate("LastName", '!=', "Doe");
var predicate3 = new $odata.Predicate("Age", '>', 10);
//
combination1 = $odata.Predicate.or([predicate1,predicate2]);
combination2 = $odata.Predicate.and([combination1,predicate2]);
User.odata().filter(combination).query();
//Queries /user?$filter=((FirstName eq 'John') or (LastName ne 'Doe')) and Age gt 10
var predicate = new $odata.Predicate("FirstName", "John")
.or(new $odata.Predicate("LastName", '!=', "Doe"))
.and(new $odata.Predicate("Age", '>', 10));
//
User.odata().filter(predicate).query();
//Queries /user?$filter=((FirstName eq 'John') or (LastName ne 'Doe')) and Age gt 10
It is sometime necessary to compare two properties or two values in a query. To do so, you can use the $odata.Value or $odata.Property classes
var predicate = new $odata.Predicate(
new $odata.Value('Foo'),
new $odata.Value('Bar')
);
//
User.odata().filter(predicate).query();
//Queries /user?$filter='Foo' eq 'Bar'
Or with two properties :
User.odata().filter(
new $odata.Property('Name'),
new $odata.Property('Surname')
).query();
//Queries /user?$filter=Name eq Surname
This library is clever enough to figure out the types from the data passed and format them accordingly. But sometimes you may need to have a specific output type. In this case you can pass a second argument to the $odata.Value() constructor :
User.odata().filter(
'Latitude',
new $odata.Value(40.765150,"Decimal")
).query();
//Queries /user?$filter=Latitude eq 40.765150M
User.odata().filter(
'Latitude',
new $odata.Value("75.42","Int32")
).query();
//Queries /user?$filter=Latitude eq 75
User.odata().filter(
'Latitude',
new $odata.Value("true","Boolean")
).query();
//Queries /user?$filter=Latitude eq true
User.odata().filter(
'Latitude',
new $odata.Value(10,"Boolean")
).query();
//Queries /user?$filter=Latitude eq true
Here is the complete list of supported types :
| Type Name | Output example |
|---|---|
| Boolean | true |
| Byte | FE |
| DateTime | datetime'2000-12-12T12:00' |
| Decimal | 2.345M |
| Double | 2.0d |
| Single | 2.0f |
| Guid | guid'12345678-aaaa-bbbb-cccc-ddddeeeeffff' |
| Int32 | 51358 |
| String | 'Hello OData' |
var users = User.odata()
.filter(new $odata.Func("endswith","FullName","Doe"), true)
.query();
//Queries /user?$filter=endswith(FullName, 'Doe') eq true
new $odata.Func(MethodName, PropertyName, Value1, Value2,...)
The parameters are assumed to be first, a property and then a value. This behavior can be overriden by specifying explicit values or properties :
new $odata.Func('substringof',
new $odata.Value('Alfreds'),
new $odata.Property('CompanyName'));
| Function | Example | Example value |
|---|---|---|
| String Functions | ||
| bool substringof(string po, string p1) | new $odata.Func('substringof',new $odata.Value('Alfreds'), new $odata.Property(CompanyName)) |
$ claude mcp add ODataAngularResources \
-- python -m otcore.mcp_server <graph>