1.0.5 • Published 5 years ago

@zetwerk/zetwerk-inter-server-virtuals v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

Zetwerk Inter Server Virtuals

npm version

This plugin will run at mongoose model level and create virtuals using the data from external servers based on the configurations passed to it.

Installation

Requires mongoose to run.

$ npm i @zetwerk/zetwerk-inter-server-virtuals --save

Once installed, import the plugin in the required model.

const interServerPlugin = require('@zetwerk/zetwerk-inter-server-virtuals');

Enable the plugin.

schema.plugin(interServerPlugin, config);

Configurations

Following configuration options are available

OptionDescription
keyName of the virtual key to be appended
localFieldName of the field to be mapped in local (support dot notation access. eg: 'a.b.c')
foreignFieldName of the field to be mapped in remote (support dot notation access. eg: 'a.b.c')
responseKeyOptional, if not passsed, it will assume the response sent by remote API is an array of objects (support dot notation access. eg: 'a.b.c')
bulk.queryNameOptional, only needed if your remote API support bulk operation
optionsNative NodeJS http request options

Example

Below given is a sample configuration object.

const config = {
    key: 'customerDetails',
    localField: 'customerId',
    foreignField: '_id',
    responseKey: 'data',
    bulk: {
        queryName: 'customerIds'
    },
    options: {
        protocol: 'http:',
        hostname: 'localhost',
        port: 6000,
        path: '/v1/customer',
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
            'authorizationtoken': '<AUTH_TOKEN>'
        }
    }
};

*options is the native NodeJS http options, so you can pass all the options supported by NodeJS http module.

Please see following link for all the available options: https://nodejs.org/api/http.html#http_http_request_options_callback