Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

My question is about how to create a definition for an object that is handled by different web services.

A brief description of my current scenario and why I think it's a problem:

A PHP web application provides AJAX endpoints for populating various parts of my web app. The endpoint provides access to User and Project entities. A node.js application uses sockjs to deliver User and Project events in real time to my web app.

Here's the problem: The schema of the JSON objects delivered by the node.js service is slightly different to the same objects provided by the PHP endpoints. This is bad because it makes much my client size code hard to re-use, and there's lots of "if it came from node, access this property; if it came from AJAX, access this other property" type logic. Ugh!

The code base is too big to do a one time refactor and make sure all devs adhere to the new design.

I want a way to share information between my node.js app and my php app so they both know how they should define certain objects.

What is the most common way of solving this kind of problem?

share|improve this question
    
You could use a single wrapper for both php and node.js. But again this requires big refactoring. –  superM Jun 21 '13 at 12:18
add comment

1 Answer

Normalize any outgoing JSON from your node.js webservice.

For example, for each schema you have, create a wrapper function that modifies your JSON data to match the format of your PHP data. Even if you just define a simple translation like this:

function convertUser(user) {
    return {
        id: user._id,
        anotherField: user.another_field,
        // ...
    };
}

My bigger concern is the reason for this. Are you just dumping data directly from the database? This sort of normalization should be part of the dump process rather than blindly giving back whatever is requested. As database fields (or schemas, in your case) change, your API should not.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.