UNPKG

@nodeboot/starter-persistence

Version:

Nodeboot starter package for persistence. Supports data access layer auto-configuration providing features like database initialization, consistency check, entity mapping, repository pattern, transactions, paging, migrations, persistence listeners, persis

94 lines 5.01 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryCacheConfiguration = exports.QUERY_CACHE_CONFIG = void 0; const core_1 = require("@nodeboot/core"); const PersistenceContext_1 = require("../PersistenceContext"); const types_1 = require("../types"); exports.QUERY_CACHE_CONFIG = "query-cache-config"; /** * QueryCacheConfiguration class responsible for setting up * query cache configurations based on application properties. * It integrates with PersistenceContext to optionally use a custom query cache provider. * * Supports enabling/disabling cache, using complex configuration options, * or providing a custom cache provider factory. * * @author Manuel Santos <https://github.com/manusant> */ let QueryCacheConfiguration = class QueryCacheConfiguration { /** * Configures the query cache settings bean. * * - Reads persistence configuration from application config. * - Checks if caching is enabled or custom cache options are set. * - If a custom QueryCache class is provided in PersistenceContext, uses it as a provider. * - Sets up the IoC container binding with the cache config or provider. * - Logs relevant information and warnings during configuration. * * @param {BeansContext} context - The context containing iocContainer, logger, and config. * @returns {void} * * @author Manuel Santos <https://github.com/manusant> */ queryCacheConfig({ iocContainer, logger, config }) { logger.info("Preparing cache configurations"); const persistenceProperties = config.getOptionalConfig(types_1.PERSISTENCE_CONFIG_PATH); if (persistenceProperties) { // Cache config can be a boolean or a complex config object const cacheConfig = persistenceProperties.getOptional("cache"); const cacheEnabled = persistenceProperties.getOptionalBoolean("cache"); if (cacheConfig || cacheEnabled !== undefined) { let cacheProvider; // Setup cache provider if a custom provider is configured through @PersistenceCache decorator const QueryCache = PersistenceContext_1.PersistenceContext.get().queryCache; if (QueryCache) { cacheProvider = (connection) => new QueryCache(connection); } if (cacheConfig) { logger.info(`Configuring query cache with options from configuration${cacheProvider ? " and custom cache provider" : ""}`); iocContainer.set(exports.QUERY_CACHE_CONFIG, { ...cacheConfig, provider: cacheProvider, }); } else if (cacheProvider) { logger.info(`Configuring query cache with custom cache provider`); iocContainer.set(exports.QUERY_CACHE_CONFIG, { provider: cacheProvider, }); } else if (cacheEnabled) { // If cache is only enabled, falling back to database cache or to a custom provider if specified logger.info(`${cacheProvider ? "Configuring custom query cache provider" : "Enabling database query cache with default configurations"}`); iocContainer.set(exports.QUERY_CACHE_CONFIG, cacheProvider ? { provider: cacheProvider } : true); } else { // Cache is explicitly disabled logger.warn("Persistence query cache is not enabled. Enable it to boost your application performance."); } } } } }; exports.QueryCacheConfiguration = QueryCacheConfiguration; __decorate([ (0, core_1.Bean)(), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], QueryCacheConfiguration.prototype, "queryCacheConfig", null); exports.QueryCacheConfiguration = QueryCacheConfiguration = __decorate([ (0, core_1.Configuration)() ], QueryCacheConfiguration); //# sourceMappingURL=QueryCacheConfiguration.js.map