4
votes

I'm interested in being able to use the Breeze.js EntityManager and query capabilities within a node console service to access a remote Data Service that exposes an BreezeJS/OData compliant RESTful endpoint.

We currently have a Data Service implemented using Node.js, MongoDB and the Breeze.js breeze-mongodb module.

We have web browser hosted clients that access the MondgoDB using the Breeze.js client API (EntityManager) and the Data Service described above.

I need to create another Node.js service that can access the same MongoDB database that the web browser hosted clients do, and for consistency/simplicity I would like to use the same data acceess API as I am using in the web browser.

Has anyone experimented with this configuration?

I experimented with loading Breeze and its dependencies using the Node.js module infrastructure, but am getting errors when Breeze tries to initialize Angular as an ajax handler. Angular is installed and configured as a node module dependency, but I am getting an error thrown:

Error: [$injector:nomod] http://errors.angularjs.org/1.2.2/$injector/nomod?p0=ngLocale

In theory I shouldn't need angular, but I get additional errors if Angular is not present.

I may be able to debug this particular issue, but it will require stepping through Breeze.js code in detail and possibly modifying it to fix. Was curious if anyone else has gotten this working.

2

2 Answers

2
votes

I'm running Breeze in Node at the moment. It used to work just fine without any modification, but a few versions ago they added a check that it's running in the browser... so now I manually remove that check :-)

My use-case is a little bit different: I'm running breeze on the server so that I can use the same business logic as in the client, and just have a really really thin layer between breezejs and the DB.

The only thing I needed to change to get it to run in the browser is add a fake ajax handler that delegates to my skinny DB wrapper - you could equally delegate to anything else, including your existing API.

    var ctor = function () {
        this.name = 'node';
        this.defaultSettings = { };
    };

    ctor.prototype.initialize = function () {
    };
    var query = require('../../../../server/db/query');

    ctor.prototype.ajax = function (config) {
        if (config.url === '/api/all') {
            query.get()
                .then(function (result) {
                    var httpResponse = {
                        data: result,
                        status: '400',
                        getHeaders: undefined,
                        config: config
                    };
                    config.success(httpResponse);
                })
                .otherwise(function (error) {
                    var httpResponse = {
                        data: '',
                        status: '500',
                        getHeaders: undefined,
                        error: error,
                        config: config
                    };
                    config.error(httpResponse);
                });
        } else if (config.url === '/api/SaveChanges') {
            query.save(JSON.parse(config.data))
                .then(function (result) {
                    var httpResponse = {
                        data: result,
                        status: '400',
                        getHeaders: undefined,
                        config: config
                    };
                    config.success(httpResponse);
                })
                .otherwise(function (error) {
                    var httpResponse = {
                        data: '',
                        status: '500',
                        getHeaders: undefined,
                        error: error,
                        config: config
                    };
                    config.error(httpResponse);
                });
        }


    };

    breezejs.config.registerAdapter('ajax', ctor);
    breezejs.config.initializeAdapterInstance('ajax', 'node', true);
0
votes

It's a good question. We haven't actually tried running Breeze within Node but your use case is interesting. This sounds like a perfect item for the Breeze User Voice. We take these suggestions seriously.