UNPKG

@fabric-es/fabric-cqrs

Version:

Hyperledger Fabric middleware for event sourcing and cqrs pattern

57 lines 2.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.commandGetById = void 0; const values_1 = __importDefault(require("lodash/values")); const command_1 = require("../store/command"); const types_1 = require("../types"); const _1 = require("."); const commandGetById = (entityName, reducer, isPrivateData, { store, logger, wallet, connectionProfile, channelName }) => async ({ enrollmentId, id }) => { const { data } = await _1.dispatcher(({ tx_id, args: { id, entityName } }) => command_1.action.queryByEntityId({ tx_id, args: { id, entityName, isPrivateData } }), { name: 'queryByEntityId', store, slice: 'write', SuccessAction: command_1.action.QUERY_SUCCESS, ErrorAction: command_1.action.QUERY_ERROR, logger, })({ id, entityName }); const currentState = ((data) => { if (data) { const { state } = types_1.computeEntity(values_1.default(data), reducer); return state; } else { return null; } })(data); const save = !data ? null : _1.dispatcher(({ tx_id, args: { events, signedRequest } }) => command_1.action.create({ channelName, connectionProfile, wallet, tx_id, enrollmentId, args: { entityName, id, version: Object.keys(data).length, isPrivateData, events: _1.replaceTag(_1.addTimestamp(events)), signedRequest, }, }), { name: 'create', store, slice: 'write', SuccessAction: command_1.action.CREATE_SUCCESS, ErrorAction: command_1.action.CREATE_ERROR, logger, typeGuard: _1.isCommitRecord, }, (result) => Object.values(result)[0]); return { currentState, save }; }; exports.commandGetById = commandGetById; //# sourceMappingURL=commandGetById.js.map