jaydata icon indicating copy to clipboard operation
jaydata copied to clipboard

Create OData service where the Entities are different from the MongoDB documents

Open milespoindexter opened this issue 10 years ago • 1 comments

Hi, here is what I am trying to do but having no luck. I would like to create an OData service that exposes data from a MongoDB collection. But the Entities of the OData service do not exactly match the Documents in the collection. How can I do this??

I had thought the process would be something like:

  1. Create the Context.
  2. Link the context the the MongoDB collection.
  3. Create the webservice
  4. In the method handling each HTTP call, generate a list of Entities with a db query, and then add them to the Context.
  5. Send the Context with full list on its way so the Jaydata framework will parse the URL query and respond with the appropriate subset of that list of entities. (or the metadata, if the query is asking for that)

Can anyone provide a sample code snippet of how to do this?

milespoindexter avatar Nov 03 '14 17:11 milespoindexter

Here's example code of what I'm trying to do . . .

require('jaydata'); var http = require('http'), qs = require('querystring'), url = require('url');

window.DOMParser=require('xmldom').DOMParser; require('q');

var MongoClient = require('mongodb').MongoClient;

$data.Class.define("test.Person", $data.Entity, null, { Id: { type: 'integer'}, FirstName: { type: 'string' }, LastName: { type: 'string' } }, null);

$data.Class.define("test.PersonContext", $data.EntityContext, null, { persons: { type: $data.EntitySet, elementType: test.Person } });

exports = test.PersonContext;

test.context = new test.PersonContext({ name: "mongoDB", databaseName:"test", address: "localhost", port: 27017 });

test.context.onReady(function(db){ var server = http.createServer(function(req, res) {

    //do mongo query
    MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
        if(!err) {
            var collection = db.collection('panelists');

            collection.find().toArray(function(err, items) {
                for (var i in items){
                    var panelist = items[i];
                    var p = new test.Person();
                    p['Id'] = panelist.PanelistID;
                    p['FirstName'] = panelist.FirstName;
                    p['LastName'] = panelist.LastName;
                    test.context.persons.add(p);

                }

                //Here's where I am stuck . . .
                //I would like to just hand this context off to Jaydata now.
                handOffToJaydata(function(result){
                    res.writeHead(200, {
                        'Content-Type': 'application/json'
                    });
                    res.write(JSON.stringify(result));
                    res.end();
                });

            });
        }

    });

});

server.listen(8080);

});

milespoindexter avatar Nov 03 '14 19:11 milespoindexter