1.0.1 • Published 5 years ago

@ximerajs/core v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Ximera

Ximera is a lightweight managed component container for Node.JS. It provides a level of abstraction over standard Node.JS modules to support Dependency Injection and Invertion of Control patterns, greatly improving maintenance, testability and reuse of your components.

Ximera is for an enterprise audience where multiple developers, of different skill levels, will work on a module over many years. When you own you code and control everything, using standard require and just installing modules from NPM registry is great. But in an enterprise context, where your code will be maintained or reused for many years, having a way to isolate components and control what modules are injected into a project provide a defensive architecture that will give confidence to junior developers while letting senior ones innovate.

Ximera is very lightweight and does impose only a few useful patterns. Only a few component types are built-in the core container, all other component types are injected through plugins or defined in the container. It is very easy to create new specialized components and this is a key enabler of our enterprise wide code promotion strategy (more on this in the WIKI).

Here's a simple JSON schema component

// example.schema.js
module.exports = function({ log, jsonSchemaFactory }) {
    log.debug("example schema factory");
    return jsonSchemaFactory({
        "$id": "http://example.com/schemas/schema.json",
        "type": "object",
        "properties": {
            "foo": { "$ref": "defs.json#/definitions/int" },
            "bar": { "$ref": "defs.json#/definitions/str" }
        }
    });
}

As you can see, other than the injected schema factory hidding the actual JSON schema implementation used, this is a simple component factory pattern that will be used by other components needing to get access to this schema. The following demonstrate an API endpoint using this schema.

// sample.endpoint.js
module.exports = async function({ proc }) {

    // Ask the Ximera process for dependencies. Wait will block until all deps are resolved. 
    const { httpApi, mySchema } = await proc.select("httpApi", "mySchema").wait();

    return httpAPi.endpoint({
        method: httpEndpointFactory.Method.GET,
        path: '/samples',
        hooks: {
            beforeGet(req) => mySchema.normalize(req.body);
        },
        handler(req, reply) {
            return reply({type: 'json', content: req.body });
        }
    });
}

This dynamic collaborators resolution will help when unit testing this component. Ximera provides a number of testing tools to easily mock/stub/intercept collaborators.

For more information about Ximera concepts, please visit our the WIKI

Getting Started

Just install @ximerajs/core@latest and save it in your package.json to use the container.