UNPKG

@citrineos/data

Version:

The OCPP data module which includes all persistence layer implementation.

140 lines 6.08 kB
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project // // SPDX-License-Identifier: Apache-2.0 import {} from '@citrineos/base'; import {} from 'sequelize'; import { Sequelize } from 'sequelize-typescript'; import { Logger } from 'tslog'; import { ComponentVariable } from './model/DeviceModel/ComponentVariable.js'; import { Authorization, Boot, Certificate, ChangeConfiguration, ChargingNeeds, ChargingProfile, ChargingSchedule, ChargingStation, ChargingStationNetworkProfile, ChargingStationSecurityInfo, ChargingStationSequence, Component, CompositeSchedule, Connector, EventData, Evse, EvseType, InstalledCertificate, LatestStatusNotification, LocalListAuthorization, LocalListVersion, LocalListVersionAuthorization, Location, MessageInfo, MeterValue, OCPPMessage, Reservation, SalesTariff, SecurityEvent, SendLocalList, SendLocalListAuthorization, ServerNetworkProfile, SetNetworkProfile, StartTransaction, StatusNotification, StopTransaction, Subscription, Tariff, Tenant, TenantPartner, Transaction, TransactionEvent, Variable, VariableAttribute, VariableCharacteristics, VariableMonitoring, VariableMonitoringStatus, VariableStatus, } from './index.js'; export class DefaultSequelizeInstance { /** * Fields */ static DEFAULT_RETRIES = 5; static DEFAULT_RETRY_DELAY = 5000; static instance = null; static logger; static config; constructor() { } static getInstance(config, logger) { if (!DefaultSequelizeInstance.instance) { DefaultSequelizeInstance.config = config; DefaultSequelizeInstance.logger = logger ? logger.getSubLogger({ name: this.name }) : new Logger({ name: this.name }); DefaultSequelizeInstance.instance = this.createSequelizeInstance(); } return DefaultSequelizeInstance.instance; } static async initializeSequelize(_sync = false) { let retryCount = 0; const maxRetries = this.config.database.maxRetries ?? this.DEFAULT_RETRIES; const retryDelay = this.config.database.retryDelay ?? this.DEFAULT_RETRY_DELAY; while (retryCount < maxRetries) { try { await this.instance.authenticate(); this.logger.info('Database connection has been established successfully'); await this.syncDb(); break; } catch (error) { retryCount++; this.logger.error(`Failed to connect to the database (attempt ${retryCount}/${maxRetries}):`, error); if (retryCount < maxRetries) { this.logger.info(`Retrying in ${retryDelay / 1000} seconds...`); await new Promise((resolve) => setTimeout(resolve, retryDelay)); } else { this.logger.error('Max retries reached. Unable to establish database connection.'); } } } this.logger.info(`Sequelize initialized: ${JSON.stringify(this.instance?.config || {})}`); } static async syncDb() { if (this.config.database.sync) { const alter = this.config.database.alter; const force = this.config.database.force; if (force) { this.logger.info('Database force synchronizing'); await this.instance.sync({ force: true }); this.logger.info('Database force synchronized'); } else if (alter) { this.logger.info('Database altering'); await this.instance.sync({ alter: true }); this.logger.info('Database altered'); } else { this.logger.info('Database synchronizing'); await this.instance.sync(); this.logger.info('Database synchronized'); } } } static createSequelizeInstance() { return new Sequelize({ host: this.config.database.host, port: this.config.database.port, database: this.config.database.database, dialect: this.config.database.dialect, username: this.config.database.username, password: this.config.database.password, models: [ Authorization, Boot, Certificate, InstalledCertificate, ChangeConfiguration, ChargingNeeds, ChargingProfile, ChargingSchedule, ChargingStation, ChargingStationNetworkProfile, ChargingStationSecurityInfo, ChargingStationSequence, Component, ComponentVariable, CompositeSchedule, Connector, Evse, EvseType, EventData, Location, MeterValue, MessageInfo, OCPPMessage, Reservation, SalesTariff, SecurityEvent, SetNetworkProfile, ServerNetworkProfile, Transaction, StartTransaction, StatusNotification, StopTransaction, LatestStatusNotification, Subscription, TransactionEvent, Tariff, VariableAttribute, VariableCharacteristics, VariableMonitoring, VariableMonitoringStatus, VariableStatus, Variable, LocalListAuthorization, LocalListVersion, LocalListVersionAuthorization, SendLocalList, SendLocalListAuthorization, Tenant, TenantPartner, ], pool: this.config.database.pool, logging: (_sql, _timing) => { }, }); } } //# sourceMappingURL=util.js.map