@citrineos/base
Version:
The base module for OCPP v2.0.1 including all interfaces. This module is not intended to be used directly, but rather as a dependency for other modules.
75 lines • 2.6 kB
JavaScript
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache-2.0
import { EventEmitter } from 'events';
/**
* Represents a generic CRUD repository.
*
* @template T - The type of the values stored in the repository.
*/
export class CrudRepository extends EventEmitter {
constructor() {
super();
}
/**
* Creates a new entry in the database with the specified value.
*/
async create(tenantId, value, namespace) {
const result = await this._create(tenantId, value, namespace);
this.emit('created', [result]);
return result;
}
/**
* Attempts to read a value from storage based on the given query, or throws if more than one value is found.
*/
async readOnlyOneByQuery(tenantId, query, namespace) {
const results = await this.readAllByQuery(tenantId, query, namespace);
if (results.length > 1) {
throw new Error(`More than one value found for query: ${JSON.stringify(query)}`);
}
return results[0];
}
/**
* Reads a value from storage or creates it if none exists.
*/
async readOrCreateByQuery(tenantId, query, namespace) {
const result = await this._readOrCreateByQuery(tenantId, query, namespace);
if (result[1]) {
this.emit('created', [result[0]]);
}
return result;
}
/**
* Updates the value associated with the given key.
*/
async updateByKey(tenantId, value, key, namespace) {
const result = await this._updateByKey(tenantId, value, key, namespace);
this.emit('updated', result ? [result] : []);
return result;
}
/**
* Updates all values matching the given query.
*/
async updateAllByQuery(tenantId, value, query, namespace) {
const result = await this._updateAllByQuery(tenantId, value, query, namespace);
this.emit('updated', result);
return result;
}
/**
* Deletes the entry with the given key.
*/
async deleteByKey(tenantId, key, namespace) {
const result = await this._deleteByKey(tenantId, key, namespace);
this.emit('deleted', result ? [result] : []);
return result;
}
/**
* Deletes all values associated with a query from the specified namespace.
*/
async deleteAllByQuery(tenantId, query, namespace) {
const result = await this._deleteAllByQuery(tenantId, query, namespace);
this.emit('deleted', result);
return result;
}
}
//# sourceMappingURL=repository.js.map