2.1.20 • Published 2 years ago

vendease-common v2.1.20

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

vendease backend common

contains a set of common utility functions, classes, and modules that are used across the Vendease platform's backend system

Code example Jetstream setup

const CONFIG = {
  connectionOptions: {
    servers: ["localhost"],
    name: process.env.APP_NAME ?? "e-procurement",
  },
  consumerOptions: {
    deliverGroup: process.env.JETSTREAM_DELIVER_GROUP ?? "core-service-group",
    durable: process.env.JETSTREAM_CONSUMER_DURABLE ?? "core-services-durable",
    deliverTo: process.env.JETSTREAM_CONSUMER_DELIVER_TO ?? "core-services-deliver-to",
    manualAck: Boolean(process.env.JETSTREAM_MANUAL_ACK ?? "true"),
  },
  streamConfig: {
    name: process.env.JETSTREAM_STREAM_NAME || "core-services",
    subjects: (process.env.JETSTREAM_STREAM_SUBJECTS || "core.*").split(","),
  },

  //   host: process.env.NATS_HOST || 'demo.nats.io',
  //   port: Number(process.env.NATS_PORT) || 4222,
};
interface ICreateCompany {
  subject: Subjects.CompanyCreated;
  data: { id: number; name: string };
}
interface IUserCreatedCompany {
  subject: Subjects.UserCreated;
  data: { id: number; first_name: string; last_name: string };
}

// Singleton
class SingleInstanceStream extends BaseStream {
  queueGroupName: string = "sample group";

  protected async onMessage(data: any, msg: JsMsg): Promise<void> {
    console.log(data);
    console.log(msg.subject);

    switch (msg.subject) {
      case "Subjects.UserCreated":
        return this.handleUserCreated(data);
      case Subjects.CompanyCreated:
        return this.handleCompanyCreated(data);
      default:
        return "i probably don't understand this message";
    }

    msg.ack();
  }
}

export async function testConnection() {
  const stream = new SingleInstanceStream(config);

  await stream.connect();

  await stream.publish<IUserCreatedCompany>(Subjects.UserCreated, { id: 20, first_name: "bola", last_name: "tinubu" });
  await stream.publish<ICreateCompany>(Subjects.CompanyCreated, { id: 20, name: "dayo's company company created" });

  await stream.listen();

  await stream.close();
}
2.1.19

2 years ago

2.1.20

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.1.9

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.14

2 years ago

2.1.15

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

1.0.32

2 years ago

2.1.18

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.8

2 years ago

2.1.7

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago