Oliver's Blog

MongoDB and DevExtreme News - Data Servers Made Easy

Several months ago, I announced the library devextreme-query-mongodb, which can be used to query MongoDB with parameters in the format supported by our DevExtreme UI widgets. You can find the original announcement here, and the library has its home on github here.

Recently I have released version 2 of this library. I chose to use major version 2 for the release because of some structural changes I made. Existing projects that were using v1 should work with v2 directly, as soon as you take one extra requirement into account. As the project wiki shows:

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');

The options API

To make it easier to create data services on the basis of devextreme-query-mongodb, I have included an API in v2 that validates query parameters. You can find a page of documentation in the wiki, but the gist of it is that you can retrieve a valid loadOptions object like this:

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

...

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

A sample data server

The page DemoDataServer in the wiki has the full source code for a data server. I decided not to publish the server as a github project, because the source needs a few changes depending on your own scenarios and a github project wouldn’t have worked out of the box (or grown more complicated again).

Here are the basic steps implemented in the sample.

1 — Connect to MongoDB. Of course you need to use your own MongoDB connection URI here.

mongodb.MongoClient.connect(mongoUri, function(err, database) {
...

2 — Set up an Express web server. Modify the URL part and collection name as necessary.

const app = express();
...
app.get('/countries', function(req, res) {
  getData(database.collection('countries'), req, res);
});

3 — Load data, taking advantage of the new options API. Adapt the schema for your collection.

function getQueryOptions(req) {
  return getOptions(req.query, {
    areaKM2: 'int',
    population: 'int'
  });
}

async function getData(coll, req, res) {
  try {
    const options = getQueryOptions(req);

    const results = await query(
      coll,
      options.loadOptions,
      options.processingOptions
    );
    res.status(200).jsonp(results);
  } catch (err) {
    handleError(res, err, 'Failed to retrieve data');
  }
}

4 — Run the web server

const httpServer = http.createServer(app);
httpServer.listen(process.env.HTTPPORT || 8080, function() {
  const port = httpServer.address().port;
  console.log('HTTP server running on port', port);
});

Try it!

Please give the library a spin if you can use it in your projects, and report any issues or questions directly on github!

Published Aug 17 2017, 03:23 PM by
Filed under: ,
Bookmark and Share

Comments

Fabio Andrade

Hi, it will be available in dashboard ?

thanks

August 21, 2017 11:11 PM

Oliver Sturm (DevExpress)

Hi Fabio,

I'm not sure I understand your question. Can you elaborate how you think this functionality could be made available in our Dashboard products?

Thanks

Oliver

August 22, 2017 7:51 AM
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners