UNPKG

@sap/xsodata

Version:

Expose data from a HANA database as OData V2 service with help of .xsodata files.

154 lines (126 loc) 5.38 kB
'use strict'; /** * Configuration. * @module configuration */ exports.modes = { development: "development", productive: "productive" }; /** * Exposed in index.js and used by xsjs or other application to set options for a hole OData handler * @param init * @constructor */ exports.HandlerOptions = function HandlerOptions(init) { /** * serviceConfiguration may be a directory containing *.xsodata files (the directory name MUST NOT end with ".xsodata") * Usage: When pathPrefix is "/sub1/sub2" and serviceConfiguration is directory "./services" then the request to * url "/sub1/sub2/test.xsodata" will be processed by the odata handler using the file * "test.xsodata" from the directory "./services" and the odata service url will be "/sub1/sub2/test.xsodata" * serviceConfiguration may be a full qualified filename (the file name MUST end with ".xsodata") * Sample: When using pathPrefix "/sub1/sub2/" as serviceConfiguration is "./services/my_service.xsodata" then the request to * url "/sub1/sub2" will be processed by the odata handler using the file * "./services/my_service.xsodata" and the odata service url will be "/sub1/sub2" * @type {string} */ this.serviceConfiguration = init.serviceConfiguration; /** * Default schema which will set on the HDB connection if not null * @type {string|null} */ this.defaultSchema = init.defaultSchema || null; /** * Settings to open a new database connection via HDB. A new connection is only open if the handlers processRequest method * is called WITHOUT an connected database connection. * @type {Object} Passed to HDB createClient */ this.dbConfiguration = init.dbConfiguration || null; /** * Define db driver to be used if dbConfiguration parameters are set. * Possible settings are 'hana-client' or 'hdb' * Default is 'hana-client' */ this.dbDriver = init.dbDriver || '@sap/hana-client'; /** * Logger Object with methods 'trace','debug','info','warn','error','fatal'. Each of them having a string as input parameter * For example a Winston logger instance is suitable * @type {logger|null} */ this.logger = init.logger || null; check(this); }; /** * RequestOptions encapsulate all options witch can be set on a per request basis * Exposed in index.js and used by xsjs or other application when calling "processRequest" * @class * @param init * @param {exports.modes.productive|exports.modes.development} [init.mode=exports.modes.productive] - Sets the operation mode. Must be exports.modes.productive or exports.modes.development * @param [init.uriPrefix] - if any kind of routing strips parts of the url, this part can be added here to * create correct embedded urls * @constructor */ exports.RequestOptions = function RequestOptions(init) { /** * If the xsodata handler is used within frameworks which modify the request url(e.g. express) * is may be required inform the xsodata handler about the removed parts. * The xsodata module adds this prefix before the service root when building the baseurl */ this.uriPrefix = init.uriPrefix; this.dbClient = init.dbClient || null; /** Function to open the db connection lazy * The functions first parameter contains the dbConfiguration passed via HandlerOptions.dbConfiguration, * the second parameter is a callback function with the signature (error, dbClient). * * Sample: * RequestOptions.dbOpenCB = function openConnection(dbConfiguration, cb) { * var client = hdb.createClient(dbConfiguration); * client.connect(function (err) { * return cb(err,client); * }); * } * ... * @type {function} */ this.dbOpenCB = init.dbOpenCB || null; /** Function to close if the db connection has been opened lazy * The functions first parameter contains the dbClient * the second parameter is a callback function without parameters. * * Sample: * RequestOptions.dbCloseDB = function(dbClient, cb) { * dbClient.end(); * return cb(); * } * ... * @type {function} */ this.dbCloseCB = init.dbCloseCB || null; /** * Function used to call a application function obtained from the moduleLoader. * The function used because some environment required special call handling, e.g. to process the * application function synchronous. * @type {null|*} */ this.functionExecutor = init.functionExecutor || null; /** * Function called for loading a module. The module is a bag for functions used at exit points * The function must support an object like {"package" : string, "file": string, "ext": string} as input parameter * @type {function()|null} */ this.moduleLoader = init.moduleLoader; if (init.mode) { check(init); } this.mode = init.mode; /** * Logger for request based logging */ this.logger = init.logger; this.locale = init.locale; }; function check (target) { if ((target.mode !== exports.modes.development ) && (target.mode !== exports.modes.productive )) { throw('Invalid mode. See xsodata.modes for possible values.'); } }