-
Notifications
You must be signed in to change notification settings - Fork 8
Home
The JavaScript library DevExtreme by DevExpress includes a highly advanced data layer. Many of the complex data-bound UI widgets in the library utilize the data layer infrastructure to load server-provided data efficiently.
When data is loaded by a data source attached to a UI widget (or by code interaction with the data source), the underlying data store receives a call to its load
function, and a parameter object is passed that I will refer to as loadOptions. If you implement a custom store to load data from your own server through a service interface, the server will (or should!) receive the loadOptions and query data accordingly.
The library devextreme-query-mongodb implements the required logic to query data from a MongoDB collection, parametrized by a DevExtreme loadOptions object.
In v2, the library is published with babel-compiled files (in the dist) folder, which are used by default. This provides broader compatibility, but it introduces a requirement for babel-polyfill
. To satisfy this, you should add a dependency to babel-polyfill
to your project (npm install --save babel-polyfill
) and initialize the polyfill before you load devextreme-query-mongodb:
require('babel-polyfill');
const query = require('devextreme-query-mongodb');
devextreme-query-mongodb requires at least version 7.3 of Node.js, and you need to pass the --harmony
flag when running node
(unless you're using the latest 8.x nightly builds, where --harmony
is not required anymore). The reason for this requirement is that devextreme-query-mongodb uses async
and await
.
The library is available through npm:
npm install devextreme-query-mongodb
The Query API is simple, it only supplies one call. The following block of code runs a query, assuming the MongoDB connection parameters are correct for your setup, and the collection values
exists with items that have a intval
field:
const MongoClient = require("mongodb").MongoClient;
const query = require("devextreme-query-mongodb");
async function queryData() {
MongoClient.connect("mongodb://localhost:27017/testdatabase", (err, db) => {
const results = await query(db.collection("values"), {
// This is the loadOptions object - pass in any valid parameters
take: 10,
filter: [ "intval", ">", 47 ],
sort: [ { selector: "intval", desc: true }]
});
// Now "results" contains an array of ten or fewer documents from the
// "values" collection that have intval > 47, sorted descendingly by intval.
});
}
Please see the Query API page for more details.
Since version 2, devextreme-query-mongodb makes it very easy to run a data server that can be used by DevExtreme clients. The main component that has been added to the library for the purpose is the OptionsAPI. Please check out the DemoDataServer page for sample code to run a data server.
Please see BreakingChanges.