@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
109 lines • 6.17 kB
JavaScript
;
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.DataSourceConfiguration = void 0;
const core_1 = require("@nodeboot/core");
const PersistenceContext_1 = require("../PersistenceContext");
const PersistenceLogger_1 = require("../adapter/PersistenceLogger");
const types_1 = require("../types");
const QueryCacheConfiguration_1 = require("./QueryCacheConfiguration");
/**
* Configuration class responsible for creating and providing the
* TypeORM DataSourceOptions bean configured with persistence settings.
*
* Retrieves persistence configuration properties, initializes the
* persistence logger, sets up subscribers, migrations, naming strategy,
* and query cache configuration for the data source.
*
* Throws errors if required configurations are missing or inconsistent.
*
* @author Manuel Santos <https://github.com/manusant>
*/
let DataSourceConfiguration = class DataSourceConfiguration {
/**
* Provides the DataSourceOptions bean named "datasource-config".
*
* @param {BeansContext} context - The beans context containing config, iocContainer, and logger.
* @returns {DataSourceOptions} The configured TypeORM DataSourceOptions.
*
* @throws {Error} If the persistence configuration node is missing.
* @throws {Error} If no database-specific configuration found for the persistence type.
* @throws {Error} If database type mismatches between configuration and overrides.
* @throws {Error} If both synchronize and migrationsRun options are enabled.
*
* @author Manuel Santos <https://github.com/manusant>
*/
dataSourceConfig({ config, iocContainer, logger }) {
const persistenceProperties = config.get(types_1.PERSISTENCE_CONFIG_PATH);
if (!persistenceProperties) {
throw new Error(`'${types_1.PERSISTENCE_CONFIG_PATH}' configuration node is required when persistence is enabled.
Please add the persistence configuration depending on the data source you are using or remove
the @EnableRepositories from your application.`);
}
const persistenceLogger = new PersistenceLogger_1.PersistenceLogger(logger, persistenceProperties);
const { databaseConnectionOverrides, eventSubscribers, migrations, namingStrategy } = PersistenceContext_1.PersistenceContext.get();
const strategy = namingStrategy ? new namingStrategy() : undefined;
let cacheConfig;
if (iocContainer.has(QueryCacheConfiguration_1.QUERY_CACHE_CONFIG)) {
cacheConfig = iocContainer.get(QueryCacheConfiguration_1.QUERY_CACHE_CONFIG);
}
else {
logger.warn("No query cache configuration found while building datasource configuration");
}
let databaseConfigs = persistenceProperties[persistenceProperties.type];
if (!databaseConfigs) {
throw new Error(`Invalid persistence configuration. No database specific configuration found for ${persistenceProperties.type} database under ${types_1.PERSISTENCE_CONFIG_PATH}' configuration node.`);
}
// Set the type from configurations to the driver/connection type
databaseConfigs.type = persistenceProperties.type;
logger.info(`${eventSubscribers.length} subscribers found and ready to be registered`);
logger.info(`${migrations.length} migrations found and ready to be registered`);
if (databaseConnectionOverrides) {
if (databaseConnectionOverrides.type !== persistenceProperties.type) {
throw new Error(`Database type mismatch between configuration properties (${persistenceProperties.type})
and @DatasourceConfiguration(...) (${databaseConnectionOverrides.type})`);
}
databaseConfigs = {
...databaseConfigs,
...databaseConnectionOverrides,
};
}
if (databaseConfigs.synchronize && databaseConfigs.migrationsRun) {
throw new Error(`Only one of "synchronize" or "migrationsRun" config property can be enabled. Please set one of them to false`);
}
// Save the synchronization and migration state
PersistenceContext_1.PersistenceContext.get().synchronizeDatabase = databaseConfigs.synchronize;
PersistenceContext_1.PersistenceContext.get().migrationsRun = databaseConfigs.migrationsRun;
return {
...databaseConfigs,
namingStrategy: strategy,
subscribers: eventSubscribers,
migrations: migrations,
logger: persistenceLogger,
cache: cacheConfig,
// IMPORTANT: Disable synchronization and migrations run during datasource initialization. If enabled it will be synced afterward
// If enabled here it will cause injection issues
synchronize: false,
migrationsRun: false,
};
}
};
exports.DataSourceConfiguration = DataSourceConfiguration;
__decorate([
(0, core_1.Bean)("datasource-config"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Object)
], DataSourceConfiguration.prototype, "dataSourceConfig", null);
exports.DataSourceConfiguration = DataSourceConfiguration = __decorate([
(0, core_1.Configuration)()
], DataSourceConfiguration);
//# sourceMappingURL=DataSourceConfiguration.js.map