1.1.5 • Published 4 years ago

@pratiko-framework/pratiko v1.1.5

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Pratiko Framework

Pratiko is a microservices framework derived from Byron Framework, from where it inherited the event-driven microservices framework. Pratiko is also tangible by adopting an interface inspired by Google's Flutter framework for mobile development.

Installation

Pratiko is hosted at NPM and can easily be install with your package manager:

# using YARN
yarn add @pratiko-framework/pratiko

# using NPM
npm install --save @pratiko-framework/pratiko

Hello, outter World

"It's a pleasure to meet you!" you hear from the Diplomat, Pratiko's component responsible for handling with communication with external world. Let's dive into the code:

import { Diplomat, Resolver } from '@pratiko-framework/pratiko';

import { CreateUser } from 'path/to/CreateUser.ts';

export const app: Diplomat = new Diplomat({
  resolvers: [
    new Resolver({
      route: 'POST /users',
      callback: CreateUser,
    }),
  ],
});

app.run();

The Diplomat handles communication with external world by defining a set of Resolvers. Each resolver implements an exposed action thru Diplomat's API. That means, Diplomat exposes an RESTful API and each endpoint is implemented with a Resolver.

Resolvers are a simple set of two or three informations: a route and a callback, which are both mandatory, and a name, which is opcional. The route is defined by a HTTP verb and a URI. The callback is the implementation for that endpoint and it follows this structure:

// CreateUser.ts
export const CreateUser = async (ctx: any): Promise<void> => {
  const { db }: any = ctx;
  const { email, password, confirmation }: any = ctx.args;

  // validate the data given, using rules and database, for instance

  if (isValid) {
    ctx.body = { user };
  } else {
    ctx.status = 400;
    ctx.body = { error: validationError };
  }
};

Notice some design decisions that might be unusual:

  1. the successful case -- when isValid === true -- doesn't change the database directly, instead it publishes an event
  2. the response is sent as soon as possible, without further due -- like updating the DB or receiving an ack back

Hello, inner World

Since the external communication is well handled by the Diplomat, meet now the component responsible for watching every internal event and dispatching actions accordingly: the Meerkat. Let's dive into the code:

import { Meerkat, Broker, Handler } from '@pratiko-framework/pratiko';

import { CreateUser } from '/path/to/CreateUser.ts';

export const app: Meerkat = new Meerkat({
  broker: new Broker({
    clientID: 'module-meerkat',
  }),
  handlers: [
    new Handler({
      topic: 'new.user',
      callback: CreateUser,
    }),
  ],
});

app.run();

You might noticed that the Meerkat requires a broker. The Broker is an object that wraps a connection to the NATS Streaming broker. That makes sense because Meerkat is responsible for the internal communication, i.e., it listens to events published all across the system and react accordingly.

Each event the Meerkat listens is declared in a Handler object, along-side with the reaction implementation, which is presented following:

export const CreateUser = async (msg: any, ctx: any): Promise<void> => {
  const { db }: any = ctx;
  const user = JSON.parse(msg.getData());

  await db.collection('users').insertOne(user);
};

The tipical behavior a Handler has is to extract data from messages -- representation of an event in the broker -- and update the database with that. But nothing stops you from doing other validations, transformations and even emitting events back to the broker as part of a choreography, a Saga for instance.

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.9

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago