UNPKG

@vulcan-sql/core

Version:
56 lines 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dataSourceModule = void 0; const tslib_1 = require("tslib"); const inversify_1 = require("inversify"); const types_1 = require("../types"); const extensions_1 = require("../../models/extensions"); const utils_1 = require("../../lib/utils/index"); const models_1 = require("../../models/index"); require("reflect-metadata"); const dataSourceModule = (profiles, options) => new inversify_1.AsyncContainerModule((bind) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { // Set the cache layer profile if user set cache loader in options // The cache layer is a special data source which is used to cache the data from other data sources. // We don't check the schemas has cache config or not in artifact, because it need to load artifact first to get. // However if the container loading data source module when artifact not generated, it will throw error. if (options && options.loader) { profiles.set(models_1.cacheProfileName, { name: models_1.cacheProfileName, type: options.loader.toLocaleLowerCase(), // allow '*' to make every user request could use the cache-layer data source. allow: '*', connection: { ['persistent-path']: models_1.cacheLayerPersistentFileName }, }); } // Data source // Relation: Executor --- Builder --- Data Source --- Profiles // This factory return the data source which has the target profile name // For example: we have three profiles p1, p2, and p3. Two data source ds1 and d2s. // ds1 has two profiles p1 and p2, ds2 has only one profile p3. // factory('p1') -> ds1 / factory('p2') -> ds2 / factory('p3') -> ds2 bind(types_1.TYPES.Factory_DataSource).toFactory((context) => (profileName) => { const profile = profiles.get(profileName); if (!profile) throw new utils_1.ConfigurationError(`Profile ${profileName} not found`); return context.container.getNamed(types_1.TYPES.Extension_DataSource, profile.type); }); // Bind profiles to their data source for (const profile of profiles.values()) { bind(types_1.TYPES.Profile) .toConstantValue(profile) .when((request) => { var _a; // Using index 0 because constraints are applied to one binding at a time (see Planner class) // See https://github.com/inversify/InversifyJS/blob/master/src/syntax/constraint_helpers.ts#L32 const constructor = (_a = request.parentRequest) === null || _a === void 0 ? void 0 : _a.bindings[0].implementationType; const parentType = Reflect.getMetadata(extensions_1.EXTENSION_TYPE_METADATA_KEY, constructor); // Always fulfill the request while the injector isn't a data source if (parentType !== types_1.TYPES.Extension_DataSource) return true; const dataSourceId = Reflect.getMetadata(extensions_1.EXTENSION_IDENTIFIER_METADATA_KEY, constructor); return dataSourceId === profile.type; }); } })); exports.dataSourceModule = dataSourceModule; //# sourceMappingURL=dataSource.js.map