UNPKG

@citrineos/data

Version:

The OCPP data module which includes all persistence layer implementation.

153 lines 7.82 kB
"use strict"; // Copyright (c) 2023 S44, LLC // Copyright Contributors to the CitrineOS Project // // SPDX-License-Identifier: Apache 2.0 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SequelizeRepository = void 0; const base_1 = require("@citrineos/base"); const util_1 = require("../util"); const tslog_1 = require("tslog"); const sequelize_1 = require("sequelize"); class SequelizeRepository extends base_1.CrudRepository { constructor(config, namespace, logger, sequelizeInstance) { super(); this.s = sequelizeInstance !== null && sequelizeInstance !== void 0 ? sequelizeInstance : util_1.DefaultSequelizeInstance.getInstance(config, logger); this.namespace = namespace; this.logger = logger ? logger.getSubLogger({ name: this.constructor.name }) : new tslog_1.Logger({ name: this.constructor.name }); } readByKey(tenantId_1, key_1) { return __awaiter(this, arguments, void 0, function* (tenantId, key, namespace = this.namespace) { return yield this.s.models[namespace].findByPk(key).then((row) => row); }); } readAllByQuery(tenantId_1, query_1) { return __awaiter(this, arguments, void 0, function* (tenantId, query, namespace = this.namespace) { return yield this.s.models[namespace] .findAll(query) .then((row) => row); }); } readAllBySqlString(tenantId_1, sqlString_1) { return __awaiter(this, arguments, void 0, function* (tenantId, sqlString, namespace = this.namespace) { return yield this.s.query(`${sqlString}`, { type: sequelize_1.QueryTypes.SELECT }); }); } readNextValue(tenantId_1, columnName_1, query_1, startValue_1) { return __awaiter(this, arguments, void 0, function* (tenantId, columnName, query, startValue, namespace = this.namespace) { const options = query ? query : undefined; const maxValue = yield this.s.models[namespace].max(columnName, options); if (maxValue === null || maxValue === undefined) { // maxValue can be 0, so we need to specifically check for null or undefined return startValue !== null && startValue !== void 0 ? startValue : 1; } if (typeof maxValue !== 'number' || isNaN(maxValue)) { throw new Error(`Max value ${maxValue} on ${columnName} is invalid.`); } return maxValue + 1; }); } existsByKey(tenantId_1, key_1) { return __awaiter(this, arguments, void 0, function* (tenantId, key, namespace = this.namespace) { return yield this.s.models[namespace].findByPk(key).then((row) => row !== null); }); } existByQuery(tenantId_1, query_1) { return __awaiter(this, arguments, void 0, function* (tenantId, query, namespace = this.namespace) { return yield this.s.models[namespace].findAll(query).then((row) => row.length); }); } findAndCount(tenantId_1, options_1) { return __awaiter(this, arguments, void 0, function* (tenantId, options, namespace = this.namespace) { return this.s.models[namespace].findAndCountAll(options); }); } _create(tenantId_1, value_1) { return __awaiter(this, arguments, void 0, function* (tenantId, value, namespace = this.namespace) { return yield value.save(); }); } _bulkCreate(tenantId_1, values_1) { return __awaiter(this, arguments, void 0, function* (tenantId, values, namespace = this.namespace) { return yield this.s.models[namespace].bulkCreate(values); }); } _createByKey(tenantId_1, value_1, key_1) { return __awaiter(this, arguments, void 0, function* (tenantId, value, key, namespace = this.namespace) { const primaryKey = this.s.models[namespace].primaryKeyAttribute; value.setDataValue(primaryKey, key); return (yield this.s.models[namespace].create(value.toJSON())); }); } _readOrCreateByQuery(tenantId_1, query_1) { return __awaiter(this, arguments, void 0, function* (tenantId, query, namespace = this.namespace) { return yield this.s.models[namespace] .findOrCreate(query) .then((result) => [result[0], result[1]]); }); } _updateByKey(tenantId_1, value_1, key_1) { return __awaiter(this, arguments, void 0, function* (tenantId, value, key, namespace = this.namespace) { const primaryKey = this.s.models[namespace].primaryKeyAttribute; return yield this._updateAllByQuery(tenantId, value, { where: { [primaryKey]: key } }, namespace).then((rows) => (rows && rows.length === 1 ? rows[0] : undefined)); }); } _updateAllByQuery(tenantId_1, value_1, query_1) { return __awaiter(this, arguments, void 0, function* (tenantId, value, query, namespace = this.namespace) { const updateOptions = query; updateOptions.returning = true; // Lengthy type conversion to satisfy sequelize-typescript return yield this.s.models[namespace] .update(value, updateOptions) .then((result) => result[1]); }); } _deleteByKey(tenantId_1, key_1) { return __awaiter(this, arguments, void 0, function* (tenantId, key, namespace = this.namespace) { return this.s.transaction((transaction) => __awaiter(this, void 0, void 0, function* () { const entryToDelete = yield this.s.models[namespace] .findByPk(key, { transaction }) .then((row) => row); if (entryToDelete) { yield this.s.models[namespace].destroy({ where: { [this.s.models[namespace].primaryKeyAttribute]: key }, transaction, }); return entryToDelete; } else { return undefined; } })); }); } _deleteAllByQuery(tenantId_1, query_1) { return __awaiter(this, arguments, void 0, function* (tenantId, query, namespace = this.namespace) { return this.s.transaction((transaction) => __awaiter(this, void 0, void 0, function* () { const entriesToDelete = yield this.s.models[namespace] .findAll(Object.assign(Object.assign({}, query), { transaction })) .then((rows) => rows); const deletedCount = yield this.s.models[namespace].destroy(Object.assign(Object.assign({}, query), { transaction })); if (entriesToDelete.length === deletedCount) { return entriesToDelete; } else { throw new Error(`Deleted ${deletedCount} entries, expected ${entriesToDelete.length}`); } })); }); } } exports.SequelizeRepository = SequelizeRepository; //# sourceMappingURL=Base.js.map