# KO Service with Dependencies

Many of the functions developed for KO Services are simple and require no external libraries or files. However the use of these libraries and files can simplify design, increase maintainability and create a better solution.

The 10-Year Risk of Cardiovascular Disease KO is an object the could be developed using a function the has not dependencies ( score-calc v0.1.0). However the introduction of external dependencies in the form of libraries and files creates a more testable, adaptable and maintainable function (score-calc v0.3.0).

Currently the KGrid Activator uses the Nashorn JavaScript engine for execution of JavaScript Objects. Nashorn implements ECMAScript 5.1 specification which doesn't support external dependencies without bundling your function using Webpack and Babel. The use of Webpack and Babel allows the function to be transpiled to Nashorn compliant ECMAScript 5.1 specification artifact.

# Create bundled KO Service

To create from the template of bundled KO, run the KGrid CLI command and select the template type of bundled:

 kgrid create myko

Comparing with the project created as a simple KO in the developer's guide, you will notice an additional file webpack.config.js in the created KO folder. This file will configure the webpack tool to build the source files into a JAVASCRIPT bundle, ready for KGrid activator.

Looking in to the folder of /src, you will find a few more files in addition to index.js.

cvdscore.js is a Javascript file containing the core function of SCORE computation.

beta.json and coefficients.json are the data files containing coefficients for the SCORE models. They are referenced in cvdscore.js using require().

When we build the bundled KO, index.js will be the main entry for webpack.

# Build bundled KO Service

Similar with the simple KO, the bundled KO under development is basically a Javascript project which follows the development pattern of node.js/npm.

In our example template, webpack is used to build the bundled script. We use default values for the webpack configuration wherever possible to keep it simple.

This shows the configuration file from the template.

let path = require('path');
module.exports = {

  entry: './src/index.js',

  output: {
    library: "score",
    path: path.resolve(__dirname, 'dist')
  },

  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
    ]
  },
};

By default, the output.libraryTarget is set as 'var'. For bundling the KO for Nashorn Script Engine, only 'var', 'assign' and 'this' are valid options. By default, the output file is set as 'main.js' in the folder defined in path. In the example template, it will be /dist.

WARNING

Note library name in the output element library: "score" must match the x-kgrid-activation element entry: score found in the KGrid extensions of the OpenAPI specification.

For details on customization of the webpack config, please refer to Webpack Documentation

You need to change to the ko directory and run

npm install

Once npm installs all dependencies, run

npm run build

The build process will create a file main.js in /dist directory. service.yaml is referring this file as the artifact for the service.

# Activate bundled KO Service

Now, the bundled KO is ready to be activated. The activation process is the same as described in the developer's guide.

Go back to the project directory, start the local grid by running

kgrid start

Once activated, you can use Swagger Editor to try out the KO service. To get the url link, run

kgrid play ark:/<username>/myko
Last Updated: 11/25/2019, 6:20:00 PM