odata client library for node
Перейти к файлу
Denny 8c2ca239da Merge pull request #1 from telerik/dedimitr/date-fix
Right side of expression is not escaped if value is Date #330820
2017-04-06 15:51:39 +03:00
spec use strict typo fix 2017-02-14 19:49:55 +00:00
.gitignore housekeeping 2016-08-07 19:54:06 +01:00
LICENSE.md Add license file 2016-08-06 13:47:45 +01:00
README.md README tidyup 2016-08-15 08:16:21 +01:00
batch.js Fix changeset mime boundaries 2016-08-13 12:19:31 +01:00
escape.js add identifier keyword 2016-08-04 19:58:37 +01:00
expression.js Right side of expression is not escaped if value is Date #330820 2017-04-06 14:48:04 +03:00
identifier.js add identifier keyword 2016-08-04 19:58:37 +01:00
index.js change package entry point to index.js 2016-08-08 10:10:14 +01:00
lambda.js Add lambda functions any and all 2016-08-06 17:13:52 +01:00
literal.js Add literal method 2016-08-05 21:54:35 +01:00
odata.js Add option to include count 2017-03-01 14:44:57 +02:00
package.json 0.5.4 2016-08-13 12:20:04 +01:00
request.js Add get request tests 2016-08-06 13:42:41 +01:00
url.js abstract out URL handling 2016-08-07 20:49:55 +01:00

README.md

odata-client

A client library for accessing odata resources using node. HTTP queries return a promise.

Installation

npm install odata-client

Usage

const odata = require('odata-client');
var q = odata({service: 'https://example.com', resources: 'Customers'});
q.top(5).skip(10).filter('Balance gt 5000').and('CreditLimit', '<', 10000).get()
.then(function(response) {
  ...
});

odata object

  • odata(config)

The odata(config) function produces a query object for the construction of queries. config is an object with the following options:

  1. service - the base URL of the service

  2. resources - the resource part of the URL for the query, e.g. Customers or Customers('ACME01')/Orders. You can also add resource parts using the resource method of the query function

  3. custom - optional object containing addition query parameters, e.g. {access_token:'123456'} will append ?access_token=123456 to the query URL.

  4. version - set OData-Version HTTP header

  5. maxVersion - set OData-MaxVersion HTTP header

  6. format - specify response format (e.g. json)

  • expression(left, op, right)

Used to produce subexpressions in a filter. For example, q.filter('CreditBalance', '>', odata.expression('OrderValue', '+', 100)) produces $filter=CreditBalance gt (OrderValue add 100). The arguments are the same as for q.filter, see below.

Expressions can be chained, eg

expression('Balance', '>', 500').and('CreditLimit', '=', 0) // (Balance gt 500) and (CreditLimit eq 0)
expression('Balance', '+', 1000).lt('CreditLimit') // (Balance add 1000) lt (CreditLimit)
  • identifier(string)
  • literal(string)

In a filter expression part, the left argument is normally treated as an identifier (i.e., if it's a string it isn't surrounded by quotes) whereas the right argument is assumed to be a literal (strings are surrounded by quotes). These methods allow you to override this. e.g.

q.filter(odata.literal('Customer'), '=', odata.identifer('Type')) // $filter='Customer' eq Type

query object

The query object has the following methods:

  • top(n)

Adds a $top=n query parameter.

  • skip(n)

Adds a $skip=n query parameter.

  • filter(left, op, right)

Used for constructing $filter requests. There are several ways to call this method:

  1. If called with a string as the sole argument, the string is used as a literal filter, e.g. q.filter("Account eq 'ACME01'")

  2. If all three arguments are specified, op should be one of the usual odata operations such as eq or add, or the symbolic equivalents such as = or +. e.g. q.filter('Account', '=', 'ACME01'). The left and right arguments can be odata.expressions for building nested queries.

  3. If called with two arguments, the operator is assumed to be eq, e.g. q.filter('Account', 'ACME01')

The left argument is assumed to be an identifier while right is assumed to be a literal, which affects the quoting of strings. You can override this behaviour with the odata.literal and odata.identifier functions, see above.

If two or more filters are chained, they are anded together. q.filter('Balance gt 1000').filter('Status', 'stop') profduces $filter=(Balance gt 1000) and (Status eq 'stop').

  • and(left, op, right)

Synonym for filter.

  • or(left, op, right)

Adds an or clause to the filter being built.

  • not(left, op, right)

Adds a not clause to the filter

  • all(field, property, op, value)

Adds an all filter, e.g.

q.all('Orders', 'Value', '<', 50) // ?$filter=Orders/all(p0:p0/Value lt 50)
  • any(field, property, op, value)

Adds an any filter, e.g.

q.any('Orders', 'Lines/$count', '>=', 10) // ?$filter=Orders/any(p0:p0/Lines/$count ge 10)
  • resource(resource, value)

Adds a new part to the resource section. e.g.

odata({service: 'https://example.com'}).resource('Customers').resource('Orders'); // https://example.com/Customers/Orders
odata({service: 'https://example.com'}).resource('Customers', 'ACME01').resource('Orders'); // https://example.com/Customers('ACME01')/Orders
odata({service: 'https://example.com'}).resource('Customers', {account:'ACME01'}).resource('Orders'); // https://example.com/Customers(account='ACME01')/Orders
  • select(items)

Adds a $select clause to the filter, e.g. q.select('Account', 'Status') produces $select=Account,Status.

  • expand(item)

Adds an item to $expand

  • search(term)

Sets the term for $search.

  • count

Adds a $count clause to the query

  • orderby(item, dir)

Adds an $orderby clause to the query. There are several ways to call this function:

  1. q.orderby('Account') produces $orderby=Account

  2. q.orderby('Account', 'desc') produces $orderby=Account desc

  3. q.orderby(['Status', 'desc'], ['Account']) produces $orderby=Status desc,Account

  • custom(name, value)

Adds custom query prameters to the query using either a pair of parameters or an object, e.q.

q.custom('access_token', '123456') // ?access_token=123456
q.custom({access_token: '123456', version: '1.2'}) // ?access_token=123456&version=1.2
  • query

Produces the query string, e.g.

odata({service: 'https://example.com/Customers'}).top(5).query() // 'https://example.com/Customers?$top=5'
  • get(options)
  • post(body, options)
  • put(body, options)
  • patch(body, options)
  • delete(options)

Perform an HTTP operation. For non-batched queries, these will return a promise which resolves to an HTTP response. The options argument is passed to the underlying request library.

For batched queries, requests are accumulated into a single document which is sent with the send function.

As a convenience when using batch functions, the content_id property of options is copied to the Content-ID header, e.g.

q.batch()...get({content_id: 1})
  • batch

Sets up batch processing. When batch processing is enabled and a HTTP request function is called, instead of being sent immediately the request is held in a queue. When the send function is called, all the requests are sent in one document.

The code

q.resource('Customers', 'ACME01').batch();
q.resource('Orders', 1).get();
q.resource('Orders', 2).get();
q.send();

will batch the queries /Customers('ACME01')/Orders(1) and /Customers('ACME01')/Orders(2) into one and send them as one document.

  • send

Will send a batched query, returning a promise that resolves to an HTTP response.