@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.
177 lines • 7.82 kB
JavaScript
// 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());
});
};
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();
}
/**
* 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.
* If a namespace is provided, the entry will be created within that namespace.
*
* @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(value, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._create(value, namespace);
this.emit('created', [result]);
return result;
});
}
bulkCreate(values, clazz) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._bulkCreate(values, clazz);
this.emit('created', result);
return result;
});
}
/**
* Creates a new entry in the database with the specified value and key.
* If a namespace is provided, the entry will be created within that namespace.
*
* @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(value, key, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._createByKey(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 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(query, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const results = yield this.readAllByQuery(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 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(query, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._readOrCreateByQuery(query, namespace);
if (result[1]) {
this.emit('created', [result[0]]);
}
return result;
});
}
/**
* Updates the value associated with the given key in the specified namespace.
* If no namespace is provided, the default namespace is used.
*
* @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(value, key, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._updateByKey(value, key, namespace);
this.emit('updated', result ? [result] : []);
return result;
});
}
/**
* Updates the values associated with the given query in the specified namespace.
* If no namespace is provided, the default namespace is used.
*
* @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(value, query, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._updateAllByQuery(value, query, namespace);
this.emit('updated', result);
return result;
});
}
/**
* Deletes a key from the specified namespace.
* If no namespace is provided, the key is deleted from the default namespace.
*
* @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(key, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._deleteByKey(key, namespace);
this.emit('deleted', result ? [result] : []);
return result;
});
}
/**
* Deletes all values associated with a query from the specified namespace.
* If no namespace is provided, the values are deleted from the default namespace.
*
* @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(query, namespace) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._deleteAllByQuery(query, namespace);
this.emit('deleted', result);
return result;
});
}
}
//# sourceMappingURL=repository.js.map