UNPKG

@kitmi/data

Version:

Jacaranda Framework Data Access Model

115 lines (114 loc) 4.09 kB
/** * Enable dataSource feature * @module Feature_DataSource */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return _default; } }); const _jacaranda = require("@kitmi/jacaranda"); const _utils = require("@kitmi/utils"); const _types = require("@kitmi/types"); const _drivers = /*#__PURE__*/ _interop_require_wildcard(require("../drivers")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const _default = { /** * This feature is loaded at service stage * @member {string} */ stage: _jacaranda.Feature.SERVICE, /** * Load the feature * @param {ServiceContainer} app - The app module object * @param {object} dataSources - Datasource settings * @returns {Promise.<*>} */ load_: async (app, dataSources, name)=>{ dataSources = app.featureConfig(dataSources, { type: 'object', valueSchema: { type: 'object', valueSchema: { type: 'object', schema: { connection: { type: 'text' }, logStatement: { type: 'boolean', optional: true }, logConnection: { type: 'boolean', optional: true } }, keepUnsanitized: true } } }, name); _utils._.forOwn(dataSources, (dataSource, dbms)=>{ _utils._.forOwn(dataSource, (config, connectorName)=>{ let serviceName = dbms + '.' + connectorName; if (!config.connection) { throw new _types.InvalidConfiguration(`Missing connection config for data source "${serviceName}".`, app, `dataSource.${dbms}.${connectorName}`); } let { connection: connectionString, ...other } = config; if (!(dbms in _drivers)) { throw new Error(`Unsupported connector driver: "${dbms}"!`); } const Connector = _drivers[dbms].Connector; other.connectorName = connectorName; const connectorService = new Connector(app, connectionString, other); app.registerService(serviceName, connectorService); app.on('stopping', async ()=>{ await connectorService.end_(); }); }); }); } }; //# sourceMappingURL=dataSource.js.map