New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

adonis5-nats

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adonis5-nats

Adonis NATS broker

  • 1.1.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

Table of contents

adonis5-nats

Adonis, microservices, nats

npm-image license-image typescript-image

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

npm i adonis5-nats
node ace configure adonis5-nats

Example usage

App/Services/UserServiceServer.ts:

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<T>(data: T): Promise<T> {
    await sleep()
    return data
  }
}

App/Services/UserServiceClient.ts:

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>(
  UserServiceClassClient,
  UserServiceClassServer
)

commands/UsersService.ts

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>(UserServiceClassServer)
    this.logger.info('users service run')
  }
}

start/routes.ts

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

version: '3.7'
services:
  nats:
    image: 'nats'
    ports:
      - '4222:4222'

Run nats

docker-compose up nats

Update manifest

node ace generate:manifest

Run service and http server

node ace users:service
node ace serve --watch

test

http://localhost:3333/get-users-ids

Keywords

FAQs

Package last updated on 27 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc