0.2.14 • Published 2 years ago

smally v0.2.14

Weekly downloads
2
License
ISC
Repository
github
Last release
2 years ago

smally

Small server framework.

Small nodejs server-side framework based on event system.

Install

npm install smally@latest

Conception

The Common concept is application consists of several level services that provides API's transport and business logic. Services interact with each other through registered methods and events.

Simple application

import { of, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import * as express from 'express';
import { App, Service, Method, Request } from 'smally';

@Service({ alias: 'HttpServer' })
class HttpServerService {
  constructor(private req: Request) {
    const ex = express();
    ex.post('/rest/users', (req: express.Request, res: express.Response, next: express.NextFunction) => {
      this.req.request({
        method: 'getUsers',
        service: 'Admin',
      }).subscribe(users => res.json(users), err => next(err));
    });
  }
}

@Service({ alias: 'Admin' })
class AdminService {
  @Method({ alias: 'getUsers' })
  private getUsers(): Observable<any[]> {
    return of([
     { id: '1', fio: 'Ivanov' },
     { id: '2', fio: 'Petrov' },
    ]);
  }
}

@Service({ alias: 'Miracle' })
class MiracleService {
  constructor(private req: Request) {
  }
  
  @Method('getFirstUserId')
  private getUserByName(fio: string): Observable<string> {
    return this.req.request<any[]>({
      method: 'getUsers',
      service: 'Admin',
    }).pipe(map(users => users.find(user => user.fio === fio)?.id));
  }
}

const app = new App({ name: 'awesome2', services: [AdminService, MiracleService] });

Services

Services is the high level elements of application. The ones should be marked with a class decorator @Service.

@Service()
class AdminService {
...
}

By default, service name equal his constructor's name: (new AdminService).constructor.name. If you want set certain service name you should define alias:

@Service({ alias: 'Admin' })
class AdminService {
...
}

The name uses to request methods from one service to another.

Services have own API's methods that can be called from other services:

@Service({ alias: 'Admin' })
class AdminService {
  @Method()
  private getUsers(): Observable<any[]> {
    return of([
      { id: '1', fio: 'Ivanov' },
      { id: '2', fio: 'Petrov' },
    ]);
  }
}

You can also pass alias for the method @Method({ alias: 'getAllUsers' }) .

Systems Providers

System providers ensure common interaction between the core and application's elements.

ServiceManager

All services in one place and ready to use. This system provider registers services and its methods into own repo.

import { ServiceManager, Service } from 'smally';
import { of } from 'rxjs';

@Service({ alias: 'Admin' })
class AdminService {
  constructor(private sm: ServiceManager) {
    const w = this.sm.getService('Admin'); // internal wrapper instance of AdminService 
    // register custom method1
    w.addMethod('method1', () => of(this.multiMethod(1)));
    // register custom method2
    w.addMethod('method2', () => of(this.multiMethod(2)));
  }
  
  private multiMethod(n: number): number {
    return n * n;
  }
}

Request

Request provider uses ServiceManager repo in order to call service's methods. It aims to determinate existsing service and method and to call it.

import { Service, Method } from 'smally';
import { of, Observable } from 'rxjs';

@Service({ alias: 'HttpServer' })
class HttpServerService {
  constructor(private req: Request) {
    const ex = express();
    ex.post('/rest/users', (req: express.Request, res: express.Response, next: express.NextFunction) => {
      // call method getUsers from Admin
      this.req.request({
        method: 'getUsers',
        service: 'Admin',
      }).subscribe(users => res.json(users), err => next(err));
    });
  }
}

@Service({ alias: 'Admin' })
class AdminService {
  @Method({ alias: 'getUsers' })
  private getUsers(): Observable<any[]> {
    return of([
     { id: '1', fio: 'Ivanov' },
     { id: '2', fio: 'Petrov' },
    ]);
  }
}

Events

Important part of services interaction is event system. All elements could emit its own events that either can be processed inside of service or outside of it.

import { Service, Method, Events } from 'smally';
import { of, Observable } from 'rxjs';

@Service({ 
  alias: 'HttpServer',
  providers: [{ provide: Events, useFactory: root => new Events('HttpServer', root) }],
 })
class HttpServerService {
  constructor(private events: Events) {
    this.events.emit('start');
  }
}

@Service({ 
  alias: 'Admin',
  providers: [{ provide: Events, useFactory: root => new Events('Admin', root) }],
 })
class AdminService {
  constructor(private events: Events) {
    this.events.on('HttpServer', 'start', () => {
      // I know that server was started
    });
  }
}

DataCenter

Data Center allows to share any data between services safely.

Injector

The injection systems based on @Service and @Injectable decorators. Injector provider builds tree of dependencies and instantiate services and providers.

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.9-beta.1

2 years ago

0.2.9-beta.0

2 years ago

0.2.9-beta.2

2 years ago

0.2.9

2 years ago

0.2.8

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.27

4 years ago

0.1.26-beta.0

4 years ago

0.1.26

4 years ago

0.1.25

4 years ago

0.1.23

4 years ago

0.1.24

4 years ago

0.1.22

4 years ago

0.1.22-sw.0

4 years ago

0.1.21

4 years ago

0.1.20

4 years ago

0.1.19

4 years ago

0.1.17

4 years ago

0.1.16

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.13

4 years ago

0.1.12

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.4

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.12

4 years ago

0.0.13

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago