https://github.com/reg2005/adonis5-nats
Scale your Adonis 5 application to infinity with microservices based on classes (with code completion from client side!) and NATS transport
https://github.com/reg2005/adonis5-nats
Last synced: 5 months ago
JSON representation
Scale your Adonis 5 application to infinity with microservices based on classes (with code completion from client side!) and NATS transport
- Host: GitHub
- URL: https://github.com/reg2005/adonis5-nats
- Owner: reg2005
- License: mit
- Created: 2021-08-11T20:23:40.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-27T17:43:03.000Z (over 2 years ago)
- Last Synced: 2024-09-16T16:22:22.147Z (7 months ago)
- Language: TypeScript
- Homepage:
- Size: 168 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-adonisjs - Adonis NATS - Scale your Adonis 5 application to infinity with microservices based on classes (with code completion from client side!) and NATS transport (Packages)
README
## Table of contents
- [adonis5-nats](#adonis5-nats)
- [Installation](#installation)
- [Example usage](#example-usage)
- [App/Services/UserServiceServer.ts:](#appservicesuserserviceserverts)
- [App/Services/UserServiceClient.ts:](#appservicesuserserviceclientts)
- [commands/UsersService.ts](#commandsusersservicets)
- [start/routes.ts](#startroutests)
- [Add docker-compose.yaml](#add-docker-composeyaml)
- [Run nats](#run-nats)
- [Update manifest](#update-manifest)
- [Run service and http server](#run-service-and-http-server)
- [test](#test)# adonis5-nats
> Adonis, microservices, nats[![npm-image]][npm-url] [![license-image]][license-url] [![typescript-image]][typescript-url]
Need microservices in your Adonis5 application? Just use it.
This package provide wrapper for NATs broker (based on moleculer). For better experience you must implement two classes called `${AnyService}Server` and `${AnyService}client` who extends `${AnyService}Server` (for better autocomplete)
## Installation
```bash
npm i adonis5-nats
node ace configure adonis5-nats
```## Example usage
### App/Services/UserServiceServer.ts:
```typescript
import { ServiceSchema } from 'moleculer'
const sleep = (ms: number = 3000) => new Promise((res) => setTimeout(() => res(true), ms))
export class UserServiceClassServer {
public static serviceName = 'UserServiceClassServer' //required field
public static serviceOptions: ServiceSchema = {} //any optins like metadata and other
/** Your any logic: */
public users: [1, 2, 3]
public async getUsersIDs() {
await sleep()
return this.users
}
public async getTestVars(data: T): Promise {
await sleep()
return data
}
}
```
### App/Services/UserServiceClient.ts:
```typescript
import { UserServiceClassServer } from 'App/Services/UserServiceServer'
import Broker from '@ioc:Adonis/Addons/NATS'
class UserServiceClassClient extends UserServiceClassServer {
public static serviceName = 'UserServiceClassServer'
constructor() {
super()
}
}
export const UserServiceClient = Broker.addClient(
UserServiceClassClient,
UserServiceClassServer
)
```### commands/UsersService.ts
```typescript
import { BaseCommand } from '@adonisjs/core/build/standalone'
import { UserServiceClassServer } from 'App/Services/UserServiceServer'
import Broker from '@ioc:Adonis/Addons/NATS'
export default class UsersService extends BaseCommand {
public static commandName = 'users:service'
public static description = ''
public static settings = {
loadApp: true,
stayAlive: true,
}public async run() {
await Broker.addServer(UserServiceClassServer)
this.logger.info('users service run')
}
}
```### start/routes.ts
```typescript
import Route from '@ioc:Adonis/Core/Route'
import { UserServiceClient } from 'App/Services/UserServiceClient'
Route.get('/get-users-ids', async () => {
return await UserServiceClient.getUsersIDs()
})
```### Add docker-compose.yaml
```yaml
version: '3.7'
services:
nats:
image: 'nats'
ports:
- '4222:4222'
```
### Run nats
```bash
docker-compose up nats
```
### Update manifest
```bash
node ace generate:manifest
```
### Run service and http server
```
node ace users:service
node ace serve --watch
```### test
http://localhost:3333/get-users-ids[npm-image]: https://img.shields.io/npm/v/adonis5-nats.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/adonis5-nats "npm"[license-image]: https://img.shields.io/npm/l/adonis5-nats?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"