1.0.2 • Published 3 years ago

command-query-separation.js v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

command-query-separation.js

About

Command Query Separation implementation for js/ts

Motivation

I'm really tired; I had seen realisation of CQS in TS/JS: "https://www.npmjs.com/package/ts-cqs", "https://www.npmjs.com/package/@timreynolds/cqs";, but I think my would be better, than them, cause - 1) More comfortable interface for working; 2) The principle is preserved; 3) As an entry and exit point here I added the "Store" abstraction for registering command/query and executing them; THIS IS NOT INCLUDED IN CQS, it's just a pre-fab friendly interface for you, Guys!!! 😘

How to use

  npm i cqs.js
//functions.ts
const x = 0;

function summTwoNumbers(a, b) {
  x = a + b;
}

function getResult() {
  return x;
}

export { summTwoNumbers, getResult };
//cqs.ts
import { Command, Query, Store } from 'cqs.js';
import { summTwoNumbers, getResult } from '../functions';

const store = new Store();
const command = new Command(store);
const query = new Query(store);

// Register new commands/query
store.register('Command', summTwoNumbers);
store.register('Query', getResult);

// Run some command/query by title
command.execute('summTwoNumbers', 2, 25);
query.execute('getResult'); // 27

// Unregister of some command/query by her title
store.unregister('Command', 'summTwoNumbers');
store.unregister('Query', 'getResult');
1.0.2

3 years ago