LogoLogo
ChangelogGitHubTwitterGitter
v4.x
v4.x
  • Marble.js
  • Getting started
    • Installation
    • Quick setup
  • HTTP
    • Effects
    • Middlewares
    • Routing
    • Errors
    • Output
    • Context
    • Advanced
      • Logging
      • Validation
      • Server Events
      • Streaming
      • Continuous mode
  • Messaging
    • Core concepts
      • Events
      • Effects
    • Microservices
      • AMQP (RabbitMQ)
      • Redis Pub/Sub
    • CQRS
    • WebSockets
  • Testing
    • HTTP routes testing
  • Other
    • How does it glue together?
    • Migration guides
      • Migration from version 3.x
      • Migration from version 2.x
      • Migration from version 1.x
    • API reference
      • @marblejs/core
        • bindTo
        • bindEagerlyTo
        • createEvent
        • createContextToken
        • operator: matchEvent
        • operator: use
        • operator: act
      • @marblejs/http
        • httpListener
        • r.pipe
        • combineRoutes
        • createServer
      • @marblejs/messaging
        • eventBus
        • messagingClient
        • createMicroservice
        • reply
      • @marblejs/websockets
        • webSocketListener
        • operator: broadcast
        • operator: mapToServer
      • @marblejs/middleware-multipart
      • @marblejs/middleware-cors
      • @marblejs/middleware-io
      • @marblejs/middleware-logger
      • @marblejs/middleware-body
      • @marblejs-contrib/middleware-jwt
        • Token signing
      • @marblejs-contrib/middleware-joi
    • Style Guide
    • FAQ
Powered by GitBook
On this page
  • Project organization
  • Keep server and connected listeners in separate files
  • Keep HTTP route with its corresponding HttpEffect
  • Use index files for combining related effects
  • Context
  • Token naming and creation
  • Eager vs Lazy binding
  • Injection
  • Messaging
  • Event encoding/decoding
  • Event matching and validation
  • Effect output
  • Error handling
  1. Other

Style Guide

Previous@marblejs-contrib/middleware-joiNextFAQ

Last updated 3 years ago

The style guide is mostly based on author experience in using Marble.js on large production-ready projects following microservice/event-driven architecture style.

Project organization

Marble.js framework doesn't define any strict file structures and conventions from the organization-level perspective that each developer should enforce. Below you can find some useful hints that you can follow when organizing your Marble.js app.

Keep server and connected listeners in separate files

From the framework perspective, listeners and servers are separate layers that are responsible for different things. and similar factory functions are responsible for handling transport-layer-related processes, like: bootstrapping server and bounded context, listening for incoming messages or reacting for transport-layer events, where for the contrast, listeners are responsible for processing I/O messages that go through underlying transport layer in order to fulfill business needs. Basically, it is just about the single responsibility principle.

By convention Marble.js follows suffixed file naming which results in:

Server

Listener

http.server.ts

http.listener.ts

microservice.server.ts

microservice.listener.ts

websocket.server.ts

websocket.listener.ts

NONE

eventbus.listener.ts

Keep HTTP route with its corresponding HttpEffect

In Marble.js HTTP effects are tightly connected to the route on which they operate. Having them in a separation makes the code less readable and less understandable. Always try to keep them as a one unit. Every HttpEffect, that comes through r.pipe route builder, is accessible via .effect property of the returned RouteEffect object. It if you want to unit test only the effect function, that's the way of accessing it.

❌ Bad

import { r } from '@marblejs/http';
import { getFooEffect } from './getFoo.effect';

const getFoo = r.pipe(
  r.matchPath('/foo'),
  r.matchType('GET'),
  r.useEffect(getFooEffect));
import { HttpEffect } from '@marblejs/http';

export const getFooEffect: HttpEffect = (req$, ctx) => req$.pipe(
  ...
);

✅ Good

import { r } from '@marblejs/http';

const getFoo$ = r.pipe(
  r.matchPath('/foo'),
  r.matchType('GET'),
  r.useEffect((req$, ctx) => req$.pipe(
    ...
  )));

Use index files for combining related effects

Index files are good for combining and re-exporting related files as a one module.

/user
  + ── /getUserList
  │      + ── getUserList.effect.spec.ts
  │      └─── getUserList.effect.ts
  │
  + ── /getUser
  │      + ── getUser.effect.spec.ts
  │      └─── getUser.effect.ts
  │
  + ── /postUser
  │      + ── getUser.effect.spec.ts
  │      └─── getUser.effect.ts
  │
  └─── index.ts
  
import { combineRoutes } from '@marblejs/http';

export const user$ = combineRoutes('/user', {
  middlewares: [
    authorize$,
  ],
  effects: [
    getUserList$,
    getUser$,
    postUser$,
  ],
});
import { httpListener } from '@marblejs/http';
import { user$ } from './user';

export const listener = httpListener({
  effects: [
    user$,
  ],
});

Or in case of messaging/websockets effects:

import { combineEffects } from '@marblejs/core';

export const user$ = combineEffects(
  userCreated$,
  userRemoved$,
  ...
);
import { messagingListener } from '@marblejs/messaging';
import { user$ } from './user';

export const listener = messagingListener({
  effects: [
    user$,
  ],
});

Context

Token naming and creation

  • Use PascalCase naming convention with Token suffix for token definitions.

  • Always remember to define a context token name identifier. It will help you quickly recognize what dependency is missing when asking for it via useContext hook function.

  • Place token next to reader definition. It is easier to navigate to reader implementation via popular "Go to Implementation" mechanism.

❌ Bad

import { createContextToken } from '@marblejs/core';

export const userRepository = createContextToken<UserRepository>();
import { createReader } from '@marblejs/core';

export const userRepository = createReader(() => ...);

✅ Good

import { createContextToken, createReader } from '@marblejs/core';

export type UserRepository = ReturnType<typeof UserRepository>;

export const UserRepository = createReader(() => ...);

export const UserRepositoryToken = createContextToken<UserRepository>('UserRepository');

Eager vs Lazy binding

Identify which dependencies should be bound to the app context eagerly (before app startup) and which lazily.

Given:

import { createContextToken, createReader } from '@marblejs/core';

export type DatabaseConnection = Connection;

export const DatabaseConnection = createReader(async _ => ...);

export const DatabaseConnectionToken = createContextToken<DatabaseConnection>('DatabaseConnection');

❌ Bad

import { bindTo, bindLazilyTo, useContext } from '@marblejs/core';

const dependencies = [
  bindTo(DatabaseConnectionToken)(DatabaseConnection),
  // or 
  bindLazilyTo(DatabaseConnectionToken)(DatabaseConnection),  
];

...

const connection = useContext(DatabaseConnectionToken)(ask);

// typeof connection === Promise<Connection>;

✅ Good

import { bindEagerlyTo, useContext } from '@marblejs/core';

const dependencies = [
  bindEagerlyTo(DatabaseConnectionToken)(DatabaseConnection),
];

...

const connection = useContext(DatabaseConnectionToken)(ask);

// typeof connection === Connection;

Injection

Always try to inject bound dependencies at the top level of your effects (before returned Observable stream). All effects are evaluated eagerly, so in case of missing context dependency the framework will be able to spot issues during initial bootstrap.

❌ Bad

const postUser$ = r.pipe(
  r.matchPath('/'),
  r.matchType('POST'),
  r.useEffect((req$, ask) => req$.pipe(
    validateRequest,
    mergeMap(req => {
      const userRepository = useContext(UserRepositoryToken)(ask);
      const { body } = req;
      
      return userRepository
        .persist(body)
        .pipe(
          mergeMap(userRepository.getById),
          map(user => ({ body: user })),
        );
    }),
  ));

✅ Good

const postUser$ = r.pipe(
  r.matchPath('/'),
  r.matchType('POST'),
  r.useEffect((req$, ask) => {
    const userRepository = useContext(UserRepositoryToken)(ask);

    return req$.pipe(
      validateRequest,
      map(req => req.body),
      mergeMap(userRepository.persist),
      mergeMap(userRepository.getById),
      map(user => ({ body: user })),
    );
  }));

Messaging

Event encoding/decoding

  • Use UPPER_SNAKE_CASE event type naming

  • Use enumerable string literal type or plain const record for gathering a map of all possible event types for given context

❌ Bad

import { Event } from '@marblejs/core';
import * as t from 'io-ts';

export const UserCreated = (id: string): Event => ({
  type: 'UserCreated',
  payload: { id },
});

export const UserUpdated = (id: string): Event => ({
  type: 'UserUpdated',
  payload: { id },
});

export const UserCreatedCodec = t.type({
  type: t.literal('UserCreated'),
  payload: t.type({ id: t.string }),
});

export const UserUpdatedCodec = t.type({
  type: t.literal('UserUpdated'),
  payload: t.type({ id: t.string }),
});

✅ Good

import { event } from '@marblejs/core';
import * as t from 'io-ts';

export enum UserEventType {
  USER_CREATED = 'USER_CREATED',
  USER_UPDATED = 'USER_UPDATED',
}

export const UserCreatedEvent =
  event(UserEventType.USER_CREATED)(t.type({
    id: t.string,
  }));
  
export const UserUpdatedEvent =
  event(UserEventType.USER_UPDATED)(t.type({
    id: t.string,
  }));

Event matching and validation

  • Always try match events by event I/O codec - avoid raw literals since they don't carry the actual event payload type

  • Event-based communication follows the same laws as request-based communication - each incoming event should be validated before usage (eg. using previously mentioned event codec).

❌ Bad

import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';

export const userCreated$: MsgEffect = event$ =>
  event$.pipe(
    // event payload is unknown, no type is inferred... 
    matchEvent('USER_CREATED'),
    act(event => ...),
  );

❌ / ✅ Better

import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { UserCreatedEvent } from './user.event.ts';

export const userCreated$: MsgEffect = event$ =>
  event$.pipe(
    // type is inferred but event still requires validation...
    matchEvent(UserCreatedEvent),
    act(event => ...),
  );

✅ Good

import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { UserCreatedEvent } from './user.event.ts';

export const userCreated$: MsgEffect = event$ =>
  event$.pipe(
    matchEvent(UserCreatedEvent),
    act(eventValidator$(UserCreatedEvent)),
    act(event => ...),
  );

Effect output

  • Each processed event should be mapped to a different event type to avoid infinite-loops.

  • In case you don't want to emit anything in the effect stream you can skip emitted values, eg. by ignoreElements operator.

❌ Bad

import { matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';

const foo$: MsgEffect = event$ =>
  event$.pipe(
    matchEvent('FOO'),
    tap(doSomeWork),
  );

✅ Good

import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { pipe } from 'fp-ts/lib/function';

const foo$: MsgEffect = event$ =>
  event$.pipe(
    matchEvent('FOO'),
    act(event => pipe(
      doSomeWork(event),
      map(payload => ({ type: 'FOO_RESULT', payload })),
    )),
  );

Error handling

  • Each messaging effect should handle errors in a disposable streams either via mergeMap/switchMap/... operators with combination of catchError or viaact operator.

❌ Bad

import { matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';

const foo$: MsgEffect = event$ =>
  event$.pipe(
    matchEvent('FOO'),
    mergeMap(doSomeWork),
    map(payload => ({ type: 'FOO_RESULT', payload }),
  );

✅ Good

import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { pipe } from 'fp-ts/lib/function';
import { catchError } from 'rxjs/operators';

const foo$: MsgEffect = event$ =>
  event$.pipe(
    matchEvent('FOO'),
    mergeMap(event => pipe(
      doSomeWork(event),
      map(payload => ({ type: 'FOO_RESULT', payload })),
      catchError(error => ({ type: 'FOO_ERROR', error })),
    )),
  );

✅ Even better

import { act, matchEvent } from '@marblejs/core';
import { MsgEffect } from '@marblejs/messaging';
import { pipe } from 'fp-ts/lib/function';

const foo$: MsgEffect = event$ =>
  event$.pipe(
    matchEvent('FOO'),
    act(event => pipe(
      doSomeWork(event),
      map(payload => ({ type: 'FOO_RESULT', payload })),
    )),
  );

Note that in case of the type of created reader will be: Reader<Context, Promise<Connection>>

Use builder for I/O encoding/decoding

Group your messages into Events, Commands and Queries (see: chapter)

#createServer
CQRS
event
async readers