UNPKG

@citrineos/data

Version:

The OCPP data module which includes all persistence layer implementation.

52 lines 2.17 kB
import { Sequelize } from 'sequelize-typescript'; import { Logger } from 'tslog'; import { ChargingStationSequence } from '../model/index.js'; import { SequelizeRepository } from './Base.js'; export class SequelizeChargingStationSequenceRepository extends SequelizeRepository { static SEQUENCE_START = 1; constructor(config, logger, sequelizeInstance) { super(config, ChargingStationSequence.MODEL_NAME, logger, sequelizeInstance); } /** * Converts a Sequelize bigint value to a JavaScript number. * Sequelize returns PostgreSQL BIGINT columns as strings to avoid precision loss, * but OCPP requires numeric types (e.g., requestId in GetChargingProfilesRequest). * * @param value - The value from Sequelize (may be string or number) * @returns A JavaScript number */ _ensureNumber(value) { if (value === null || value === undefined) { return SequelizeChargingStationSequenceRepository.SEQUENCE_START; } if (typeof value === 'string') { const parsed = parseInt(value, 10); if (isNaN(parsed)) { return SequelizeChargingStationSequenceRepository.SEQUENCE_START; } return parsed; } return Number(value); } async getNextSequenceValue(tenantId, stationId, type) { return await this.s.transaction(async (transaction) => { const [storedSequence, sequenceCreated] = await this.readOrCreateByQuery(tenantId, { where: { tenantId: tenantId, stationId: stationId, type: type, }, defaults: { value: SequelizeChargingStationSequenceRepository.SEQUENCE_START, }, transaction, }); if (!sequenceCreated) { await storedSequence.increment('value', { transaction }); await storedSequence.reload({ transaction }); } return this._ensureNumber(storedSequence.get('value')); }); } } //# sourceMappingURL=ChargingStationSequence.js.map