smally
v0.2.14
Published
Small nodejs server-side framework based on event system.
Downloads
10
Readme
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.