UNPKG

@themost/data

Version:

MOST Web Framework Codename Blueshift - Data module

86 lines (83 loc) 2.92 kB
// MOST Web Framework 2.0 Codename Blueshift BSD-3-Clause license Copyright (c) 2017-2022, THEMOST LP All rights reserved var {Args, PathUtils, SequentialEventEmitter} = require('@themost/common'); var {DataConfiguration} = require('./data-configuration'); var {DefaultDataContext} = require('./data-context'); var isObjectLike = require('lodash/isObjectLike'); /** * @class * @param {string} cwdOrConfig */ class DataApplication extends SequentialEventEmitter { constructor(cwdOrConfig) { super(); Object.defineProperty(this, '_services', { configurable: true, enumerable: false, writable: false, value: {} }); if (isObjectLike(cwdOrConfig)) { Object.defineProperty(this, 'configuration', { configurable: true, enumerable: false, writable: false, value: new DataConfiguration(cwdOrConfig) }); return; } Object.defineProperty(this, 'configuration', { configurable: true, enumerable: false, writable: false, value: new DataConfiguration(PathUtils.join(cwdOrConfig, 'config')) }); } hasService(serviceCtor) { if (serviceCtor == null) { return false; } Args.check(typeof serviceCtor === 'function', new Error('Strategy constructor is invalid.')); return Object.prototype.hasOwnProperty.call(this._services, serviceCtor.name); } getService(serviceCtor) { if (serviceCtor == null) { return false; } Args.check(typeof serviceCtor === 'function', new Error('Strategy constructor is invalid.')); if (Object.prototype.hasOwnProperty.call(this._services, serviceCtor.name) === false) { return; } return this._services[serviceCtor.name]; } useStrategy(serviceCtor, strategyCtor) { if (strategyCtor == null) { return false; } Args.check(typeof serviceCtor === 'function', new Error('Service constructor is invalid.')); Args.check(typeof strategyCtor === 'function', new Error('Strategy constructor is invalid.')); Object.defineProperty(this._services, serviceCtor.name, { configurable: true, enumerable: true, writable: true, value: new strategyCtor(this) }); return this; } useService(serviceCtor) { return this.useStrategy(serviceCtor, serviceCtor); } getConfiguration() { return this.configuration; } createContext() { const context = new DefaultDataContext(); // override configuration context.getConfiguration = () => { return this.configuration; }; return context; } } module.exports = { DataApplication };