UNPKG

@flexiblepersistence/service

Version:

A Service implementation for Flexible Persistence's PersistenceAdapter

115 lines 3.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ServiceHandler = void 0; /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/no-explicit-any */ // file deepcode ignore no-any: any needed const flexiblepersistence_1 = require("flexiblepersistence"); class ServiceHandler { constructor(persistenceInfoOrJournaly, element, persistence) { this.element = {}; if (persistenceInfoOrJournaly instanceof flexiblepersistence_1.PersistenceInfo) { this.persistenceInfo = persistenceInfoOrJournaly; this.journaly = this.persistenceInfo?.journaly; } else { this.journaly = persistenceInfoOrJournaly; } if (element) this.setElement(element); if (persistence) this.setPersistence(persistence); } async transaction( // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars options, // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars callback) { // It must get all CRUD methods with input and output then // put intro an array and then call them in a transaction // If one fails, all must be rolled back using the array // from back to front // each method must call it's opposite method in the transaction // create - call delete using the output of create // delete - call create using a read before delete // update - call update using a read before update // read - no action throw new Error('Method not implemented.'); } clear() { if (this.persistence) return this.persistence.clear(); throw new Error('Persistence nonexistent.'); } initElement() { const initDefault = { journaly: this.journaly, }; for (const key in this.element) { if (Object.prototype.hasOwnProperty.call(this.element, key)) { const element = this.element[key]; element.init(initDefault); } } } setElement(element) { this.element = element; this.initElement(); } initPersistence() { if (this.persistence) for (const key in this.element) { if (Object.prototype.hasOwnProperty.call(this.element, key)) { const element = this.element[key]; element.setPersistence(this.persistence); } } } setPersistence(persistence) { this.persistence = persistence; this.initPersistence(); } close() { if (this.persistence) return this.persistence.close(); throw new Error('Persistence nonexistent.'); } getFormattedScheme(scheme) { return scheme.charAt(0).toUpperCase() + scheme.slice(1); } PersistencePromise(input, method, resolve, reject) { this.journaly ?.publish(this.getFormattedScheme(input.scheme) + 'Service.' + method, input) .then((output) => { resolve(output); }) .catch((error) => { reject(error); }); } makePromise(input, method) { return new Promise((resolve, reject) => { this.PersistencePromise(input, method, resolve, reject); }); } other(input) { return this.makePromise(input, 'other'); } create(input) { return this.makePromise(input, 'create'); } update(input) { return this.makePromise(input, 'update'); } read(input) { return this.makePromise(input, 'read'); } delete(input) { return this.makePromise(input, 'delete'); } getPersistenceInfo() { return this.persistenceInfo; } } exports.ServiceHandler = ServiceHandler; //# sourceMappingURL=serviceHandler.js.map