Skip to content

OptionsAPI

Oli Sturm edited this page Oct 7, 2022 · 4 revisions

The Query API published by devextreme-query-mongodb requires a valid loadOptions structure for any call to the query function. If you set up at a data service that queries information from a database on behalf of a client, you need to make sure that parameters passed by the client are valid before passing your loadOptions to query. Of course you could have all sorts of custom validation processes to run, but the basic technical requirements can be standardized.

Starting with version 2, devextreme-query-mongodb provides a helper function called getOptions that performs validations and consistency checks on parameters passed from a client and returns valid loadOptions and processing options to pass to query. For example, if your client passes options in the query string of a request, you can retrieve loadOptions like this:

const getOptions = require('devextreme-query-mongodb/options').getOptions;

...

// request object in req
const loadOptions = getOptions(req.query);

The object passed to getOptions is expected to adhere to the loadOptions structure, with three additions:

  1. A summaryQueryLimit (see QueryAPI) can be included
  2. A field tzOffset can be included, which is translated to the processing option timezoneOffset (again, see QueryAPI)
  3. A field caseInsensitiveRegex (see QueryAPI) can be included

Using a schema

For purposes of comparing data values, it is important that any values used in the filter and searchValue fields have the correct types. When query parameters are received from a client, they may be transferred in string form. By passing a schema to getOptions, named values can be converted automatically. This mechanism is currently implemented for int, float, Date (type name: datetime) and boolean (type name: bool) values.

For instance, if you know that your collection documents have an intNum field of type int and a floatNum field of type float, you can call getOptions like this to make sure that any filter and search parameters are converted accordingly:

getOptions(req.query, {
  intNum: 'int',
  floatNum: 'float',
});

Property paths can be used as well:

getOptions(req.query, {
  'customer.registration.date': 'datetime',
  'customer.emailConfirmed': 'bool'
});

Note that since version 2.0.11 the schema mechanism is the only way of converting types for data passed with filter and search criteria. Auto-conversion happened in previous versions, but this has been removed. Also see BreakingChanges.

Example

See DemoDataServer

Clone this wiki locally