@xrystal/core 3.27.6 → 3.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.27.6",
4
+ "version": "3.28.0",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -1,5 +1,6 @@
1
1
  import Configs from "../configs";
2
2
  import System from "../system";
3
+ import KafkaForCoreSeeder from "./seeder";
3
4
  import { IProvide } from "../../utils";
4
5
  type KafkaInstanceType = {
5
6
  clientId: string;
@@ -15,13 +16,13 @@ export default class KafkaForCore implements IProvide<any> {
15
16
  private brokers;
16
17
  private username;
17
18
  private password;
18
- constructor({ system, configs, }: {
19
+ constructor({ system, configs, kafkaForCoreSeeder }: {
19
20
  system: System;
20
21
  configs: Configs;
22
+ kafkaForCoreSeeder: KafkaForCoreSeeder;
21
23
  });
22
24
  onInit: ({}: {}) => Promise<void>;
23
25
  kafkaLoader: ({ clientId, brokers, username, password }: KafkaInstanceType) => Promise<any>;
24
- private initializeKafkaInfrastructure;
25
26
  sendMessage: (topic: string, message: any) => Promise<void>;
26
27
  get producer(): any;
27
28
  get instance(): any;
@@ -7,10 +7,12 @@ export default class KafkaForCore {
7
7
  username = null;
8
8
  password = null;
9
9
  #system;
10
+ #kafkaForCoreSeeder;
10
11
  #configs;
11
- constructor({ system, configs, }) {
12
+ constructor({ system, configs, kafkaForCoreSeeder }) {
12
13
  this.#system = system;
13
14
  this.#configs = configs;
15
+ this.#kafkaForCoreSeeder = kafkaForCoreSeeder;
14
16
  }
15
17
  onInit = async ({}) => {
16
18
  this.clientId = this.#configs.all?.kafkaClientId;
@@ -32,7 +34,7 @@ export default class KafkaForCore {
32
34
  username: this.username,
33
35
  password: this.password
34
36
  });
35
- await this.initializeKafkaInfrastructure();
37
+ await this.#kafkaForCoreSeeder.initializeKafkaInfrastructure();
36
38
  };
37
39
  kafkaLoader = async ({ clientId, brokers, username, password }) => {
38
40
  const kafkaConfig = {
@@ -62,48 +64,6 @@ export default class KafkaForCore {
62
64
  console.error(`Kafka ${error}`);
63
65
  }
64
66
  };
65
- initializeKafkaInfrastructure = async () => {
66
- const { kafkaBrokers, kafkaTopics, isKafkaPassive, } = {
67
- isKafkaPassive: process.env.IS_KAFKA_PASSIVE === 'true' ? true : false,
68
- kafkaBrokers: process.env?.KAFKA_BROKERS,
69
- kafkaTopics: [
70
- ...new Set([
71
- ...this.#configs.all.kafkaTopics
72
- ])
73
- ],
74
- };
75
- console.log(kafkaBrokers, kafkaTopics, isKafkaPassive);
76
- if (isKafkaPassive === true || !kafkaBrokers)
77
- return;
78
- const brokers = String(kafkaBrokers).split(",").map(b => b.trim());
79
- const topicsToCreate = Array.isArray(kafkaTopics) ? kafkaTopics : [];
80
- if (topicsToCreate.length === 0)
81
- return;
82
- const admin = this._instance.admin();
83
- try {
84
- await admin.connect();
85
- const existingTopics = await admin.listTopics();
86
- const newTopics = topicsToCreate
87
- .filter(topic => !existingTopics.includes(topic))
88
- .map(topic => ({
89
- topic,
90
- numPartitions: 1,
91
- replicationFactor: 1
92
- }));
93
- if (newTopics.length > 0) {
94
- await admin.createTopics({
95
- waitForLeaders: true,
96
- topics: newTopics
97
- });
98
- }
99
- }
100
- catch (error) {
101
- // => Error
102
- }
103
- finally {
104
- await admin.disconnect();
105
- }
106
- };
107
67
  // => Helpers
108
68
  sendMessage = async (topic, message) => {
109
69
  if (!this.producer)
@@ -0,0 +1,11 @@
1
+ import { Kafka } from "kafkajs";
2
+ import Configs from "source/loader/configs";
3
+ export default class KafkaForCoreSeeder {
4
+ #private;
5
+ constructor({ kafka, cache, configs, }: {
6
+ kafka: Kafka;
7
+ cache: Cache;
8
+ configs: Configs;
9
+ });
10
+ initializeKafkaInfrastructure: () => Promise<void>;
11
+ }
@@ -0,0 +1,50 @@
1
+ export default class KafkaForCoreSeeder {
2
+ #configs;
3
+ #kafka;
4
+ #cache;
5
+ constructor({ kafka, cache, configs, }) {
6
+ this.#kafka = kafka;
7
+ this.#cache = cache;
8
+ this.#configs = configs;
9
+ }
10
+ initializeKafkaInfrastructure = async () => {
11
+ const { kafkaBrokers, kafkaTopics, isKafkaPassive, } = {
12
+ isKafkaPassive: process.env.IS_KAFKA_PASSIVE === 'true' ? true : false,
13
+ kafkaBrokers: process.env?.KAFKA_BROKERS,
14
+ kafkaTopics: [
15
+ ...new Set([
16
+ ...this.#configs.all.kafkaTopics
17
+ ])
18
+ ],
19
+ };
20
+ if (isKafkaPassive === true || !kafkaBrokers)
21
+ return;
22
+ const topicsToCreate = Array.isArray(kafkaTopics) ? kafkaTopics : [];
23
+ if (topicsToCreate.length === 0)
24
+ return;
25
+ const admin = this.#kafka.admin();
26
+ try {
27
+ await admin.connect();
28
+ const existingTopics = await admin.listTopics();
29
+ const newTopics = topicsToCreate
30
+ .filter(topic => !existingTopics.includes(topic))
31
+ .map(topic => ({
32
+ topic,
33
+ numPartitions: 1,
34
+ replicationFactor: 1
35
+ }));
36
+ if (newTopics.length > 0) {
37
+ await admin.createTopics({
38
+ waitForLeaders: true,
39
+ topics: newTopics
40
+ });
41
+ }
42
+ }
43
+ catch (error) {
44
+ console.log('comming', error);
45
+ }
46
+ finally {
47
+ await admin.disconnect();
48
+ }
49
+ };
50
+ }