Skip to content

I. Components

Jonathan Casarrubias edited this page May 3, 2018 · 1 revision

alt text

Description

In this section, you will learn how to create and configure an OnixJS Component.

An OnixJS Component can also be considered as a Controller and is the place where you expose your APIs, either through RPC, Streams, REST or Views.

Component Example

import { IComponent, Inject, Component, RPC, Stream } from '@onixjs/core';
import { MyService } from './my.service';
import { MyModel } from './my.model';

@Component({
  // Optional component level lifecycle
  // will execute on every RPC Call, do your magic here. :)
  lifecycle: async (app, metadata, method): Promise<any> => {
    // before call
    const result = await method();
    // after call
    console.log('Custom Logger: ', result);
    return result;
  }
})
export class MyComponent implements IComponent {

  @Inject.Service(MyService) private service: MyService;
  /**
   * @method init
   * @description This method will be executed by the framework
   * when everything has been configured.
   */
  init() {}
  /**
   * @method myEndpoint
   * @param payload
   * @returns Promise<SubscriptionModel>
   * @description Example method of how to expose through
   * RPC methods that internally might add business logic
   * or database/services calls.
   */
  @RPC()
  async subscribe(payload: MyModel): Promise<MyModel> {
    const result = await this.service.create(todo);
    // Do something here?...
    return result;
  }
  /**
   * @method myStream
   * @param todo
   * @returns Promise<MyModel>
   * @description Example of a stream endpoint for real-time
   * features.
   */
  @Stream()
  async listTodos(stream) {
    setInterval(() => stream(true), 1000); // Will stream true every 1 second 
  }
  /**
   * @method destroy
   * @param todo
   * @description Destroy method will be executed before terminating
   * an application process.
   */
  destroy() {}
}

The example above explains how to create a simple Component Class, which uses the @Component decorator in order to provide some configurations, as for now, we are only considering a lifecycle hook. But since a component is a more complex artifact in an OnixJS Project, different features will be documented in different sections.

A Component configuration can include

  • acl: An array of ACL Rules.
  • lifecycle A system hook that will be executed on every RPC or Stream.

A Component Class can include

  • @Router: Used to expose either REST Endpoints, Views or Middleware features compatible with any Express.js middleware.
  • @Inject: Used to inject either Models, Service Providers, Renderers or System Notifier.
  • @RPC: Used to expose a remote procedure call endpoint, mainly called from an OnixJS SDK Client.
  • @Stream: Used to register real-time endpoints, which can stream a set of data over time.

Init Method

The init method will be executed once the framework is ready, it must be used instead of a constructor as a best practice in order to make sure everythin has been correctly setup.

Destroy Method

The destroy method will be executed by the framework once this is going to be shut down.

Clone this wiki locally