@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.
192 lines • 8.59 kB
JavaScript
"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.CrudRepository = void 0;
const events_1 = require("events");
/**
* Represents a generic CRUD repository.
*
* @template T - The type of the values stored in the repository.
*/
class CrudRepository extends events_1.EventEmitter {
constructor() {
super();
}
/**
* On method overridden to handle events from {@link CrudEvent}.
* @param event The name of the event. Must be a key in {@link CrudEvent}.
* @param listener The callback for the event. Argument types correspond to the contents of the event key in {@link CrudEvent}.
*
* @see {@link EventEmitter#on} for the original method.
*/
on(event, listener) {
return super.on(event, listener);
}
/**
* Emit method overridden to emit events from {@link CrudEvent}.
* @param event The name of the event. Must be a key in {@link CrudEvent}.
* @param args The arguments to pass with the event. Allowed types correspond to the contents of the event key in {@link CrudEvent}.
*
* @see {@link EventEmitter#emit} for the original method.
*/
emit(event, ...args) {
return super.emit(event, ...args);
}
/**
* Creates a new entry in the database with the specified value.
*
* @param tenantId - The tenant ID for which to create the entry.
* @param value - The value of the entry.
* @param namespace - The optional namespace to create the entry in.
* @returns A Promise that resolves to the created entry.
*/
create(tenantId, value, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._create(tenantId, value, namespace);
this.emit('created', [result]);
return result;
});
}
/**
* Creates multiple entries in the database.
*
* @param tenantId - The tenant ID for which to create the entries.
* @param values - The values of the entries.
* @param clazz - The class of the model.
* @param namespace - The optional namespace to create the entries in.
* @returns A Promise that resolves to the created entries.
*/
bulkCreate(tenantId, values, clazz, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._bulkCreate(tenantId, values, namespace);
this.emit('created', result);
return result;
});
}
/**
* Creates a new entry in the database with the specified value and key.
*
* @param tenantId - The tenant ID for which to create the entry.
* @param value - The value of the entry.
* @param key - The key of the entry.
* @param namespace - The optional namespace to create the entry in.
* @returns A Promise that resolves to the created entry.
*/
createByKey(tenantId, value, key, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._createByKey(tenantId, value, key, namespace);
this.emit('created', [result]);
return result;
});
}
/**
* Attempts to read a value from storage based on the given query, or throws an exception if more than one value is found.
*
* @param tenantId - The tenant ID for which to read the entry.
* @param query - The query to use.
* @param namespace - Optional namespace for the query.
* @returns A promise that resolves to the value associated with the query if it exists. An exception is thrown if more than one value is found.
*/
readOnlyOneByQuery(tenantId, query, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const results = yield 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 the first matching value from storage based on the given query, or creates a matching value if none exists.
*
* @param tenantId - The tenant ID for which to read or create the entry.
* @param query - The query to use.
* @param namespace - Optional namespace for the query.
* @returns A promise that resolves to an array where the first element is the value associated with the query, either an existing value or the newly created value, and the second element is a boolean indicating whether the entry was created.
*/
readOrCreateByQuery(tenantId, query, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._readOrCreateByQuery(tenantId, query, namespace);
if (result[1]) {
this.emit('created', [result[0]]);
}
return result;
});
}
/**
* Updates the value associated with the given key.
*
* @param tenantId - The tenant ID for which to update the entry.
* @param value - The new value to associate with the key.
* @param key - The key to update.
* @param namespace - The namespace in which to update the key.
* @returns A promise that resolves to the updated value, or undefined if the key does not exist.
*/
updateByKey(tenantId, value, key, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._updateByKey(tenantId, value, key, namespace);
this.emit('updated', result ? [result] : []);
return result;
});
}
/**
* Updates the values associated with the given query.
*
* @param tenantId - The tenant ID for which to update the entries.
* @param value - The new value to associate with the query.
* @param query - The query to use.
* @param namespace - Optional namespace for the query.
* @returns A promise that resolves to the updated values associated with the query.
*/
updateAllByQuery(tenantId, value, query, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._updateAllByQuery(tenantId, value, query, namespace);
this.emit('updated', result);
return result;
});
}
/**
* Deletes a key from the specified namespace.
*
* @param tenantId - The tenant ID for which to delete the entry.
* @param key - The key to delete.
* @param namespace - Optional. The namespace from which to delete the key.
* @returns A Promise that resolves to the deleted entry, or undefined there was no matching entry.
*/
deleteByKey(tenantId, key, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._deleteByKey(tenantId, key, namespace);
this.emit('deleted', result ? [result] : []);
return result;
});
}
/**
* Deletes all values associated with a query from the specified namespace.
*
* @param tenantId - The tenant ID for which to delete the entries.
* @param query - The query to use.
* @param namespace - Optional. The namespace from which to delete the values.
* @returns A Promise that resolves to the deleted entries.
*/
deleteAllByQuery(tenantId, query, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._deleteAllByQuery(tenantId, query, namespace);
this.emit('deleted', result);
return result;
});
}
}
exports.CrudRepository = CrudRepository;
//# sourceMappingURL=repository.js.map