@rsdk/nats.kv 5.4.0-next.2
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/CHANGELOG.md +10 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +5 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/inject-kv-service.decorator.d.ts +2 -0
- package/dist/inject-kv-service.decorator.js +19 -0
- package/dist/inject-kv-service.decorator.js.map +1 -0
- package/dist/interfaces/nats-kv-entry-meta.interface.d.ts +6 -0
- package/dist/interfaces/nats-kv-entry-meta.interface.js +3 -0
- package/dist/interfaces/nats-kv-entry-meta.interface.js.map +1 -0
- package/dist/interfaces/nats-kv-entry.interface.d.ts +6 -0
- package/dist/interfaces/nats-kv-entry.interface.js +3 -0
- package/dist/interfaces/nats-kv-entry.interface.js.map +1 -0
- package/dist/kv-storage.d.ts +23 -0
- package/dist/kv-storage.js +80 -0
- package/dist/kv-storage.js.map +1 -0
- package/dist/nats-kv-module.generator.d.ts +5 -0
- package/dist/nats-kv-module.generator.js +30 -0
- package/dist/nats-kv-module.generator.js.map +1 -0
- package/dist/nats-kv.module.d.ts +9 -0
- package/dist/nats-kv.module.js +63 -0
- package/dist/nats-kv.module.js.map +1 -0
- package/dist/nats-kv.service.d.ts +31 -0
- package/dist/nats-kv.service.js +101 -0
- package/dist/nats-kv.service.js.map +1 -0
- package/dist/tokens.fn.d.ts +2 -0
- package/dist/tokens.fn.js +8 -0
- package/dist/tokens.fn.js.map +1 -0
- package/dist/types/decode-func.type.d.ts +1 -0
- package/dist/types/decode-func.type.js +3 -0
- package/dist/types/decode-func.type.js.map +1 -0
- package/dist/watch-kv.decorator.d.ts +23 -0
- package/dist/watch-kv.decorator.js +19 -0
- package/dist/watch-kv.decorator.js.map +1 -0
- package/dist/watch-kv.service.d.ts +37 -0
- package/dist/watch-kv.service.js +116 -0
- package/dist/watch-kv.service.js.map +1 -0
- package/jest.config.js +1 -0
- package/jest.config.unit.js +1 -0
- package/package.json +42 -0
- package/src/constants.ts +1 -0
- package/src/index.ts +5 -0
- package/src/inject-kv-service.decorator.ts +39 -0
- package/src/interfaces/nats-kv-entry-meta.interface.ts +6 -0
- package/src/interfaces/nats-kv-entry.interface.ts +8 -0
- package/src/kv-storage.ts +121 -0
- package/src/nats-kv-module.generator.ts +49 -0
- package/src/nats-kv.module.ts +58 -0
- package/src/nats-kv.service.ts +104 -0
- package/src/tokens.fn.ts +8 -0
- package/src/types/decode-func.type.ts +1 -0
- package/src/watch-kv.decorator.ts +30 -0
- package/src/watch-kv.service.ts +124 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Inject } from '@nestjs/common';
|
|
2
|
+
import { DiscoveryService } from '@nestjs/core';
|
|
3
|
+
import { InjectLogger } from '@rsdk/core';
|
|
4
|
+
import { ILogger } from '@rsdk/logging';
|
|
5
|
+
import type { KV, KvEntry, KvWatchOptions } from 'nats';
|
|
6
|
+
import { KvWatchInclude } from 'nats';
|
|
7
|
+
|
|
8
|
+
import { NatsKvService } from './nats-kv.service';
|
|
9
|
+
import type { KvWatcher, WatchConfig } from './watch-kv.decorator';
|
|
10
|
+
import { WatchKv } from './watch-kv.decorator';
|
|
11
|
+
|
|
12
|
+
export class WatchKvService {
|
|
13
|
+
constructor(
|
|
14
|
+
@InjectLogger(WatchKvService)
|
|
15
|
+
private readonly logger: ILogger,
|
|
16
|
+
private readonly discovery: DiscoveryService,
|
|
17
|
+
private readonly nats: NatsKvService,
|
|
18
|
+
@Inject('CONNECTION_NAME')
|
|
19
|
+
private readonly connectionName?: string,
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Runs the watches for the key-value service.
|
|
24
|
+
* Waits for buckets to exist and starts the watch operation for each bucket.
|
|
25
|
+
*/
|
|
26
|
+
public async runWatches(): Promise<void> {
|
|
27
|
+
const watchers = await this.discoverWatchers();
|
|
28
|
+
|
|
29
|
+
await Promise.all(
|
|
30
|
+
watchers.map(async ({ instance, metadata }) => {
|
|
31
|
+
await this.nats.waitForBucketExists(metadata.bucket, 5000);
|
|
32
|
+
await this.runWatch(metadata, instance);
|
|
33
|
+
this.logger.info('NatsKV watcher started', { metadata });
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Stops all watches.
|
|
40
|
+
*
|
|
41
|
+
* @note This method is not implemented yet.
|
|
42
|
+
*/
|
|
43
|
+
public async stopWatches(): Promise<void> {
|
|
44
|
+
// TODO: Implement
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public async watch(
|
|
48
|
+
kv: KV,
|
|
49
|
+
opts: KvWatchOptions,
|
|
50
|
+
callback: (event: KvEntry) => Promise<void>,
|
|
51
|
+
): Promise<void> {
|
|
52
|
+
for await (const entry of await kv.watch(opts)) {
|
|
53
|
+
callback(entry).catch((error) => {
|
|
54
|
+
this.logger.error('Error processing KV entry', { entry, err: error });
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Runs the watch operation for the given metadata and watcher.
|
|
61
|
+
*
|
|
62
|
+
* @param config - The watch configuration.
|
|
63
|
+
* @param watcher - The KvWatcher instance.
|
|
64
|
+
* @returns A Promise that resolves when the watch operation is complete.
|
|
65
|
+
*/
|
|
66
|
+
private async runWatch(
|
|
67
|
+
config: WatchConfig,
|
|
68
|
+
watcher: KvWatcher,
|
|
69
|
+
): Promise<void> {
|
|
70
|
+
const kv = await this.nats.create({
|
|
71
|
+
bucketName: config.bucket,
|
|
72
|
+
bindOnly: true,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
this.watch(
|
|
76
|
+
kv.getRawStorage(),
|
|
77
|
+
{ include: KvWatchInclude.LastValue },
|
|
78
|
+
async (event) => {
|
|
79
|
+
switch (event.operation) {
|
|
80
|
+
case 'PUT':
|
|
81
|
+
await watcher.onPut(event);
|
|
82
|
+
break;
|
|
83
|
+
case 'DEL':
|
|
84
|
+
await watcher.onDelete(event);
|
|
85
|
+
break;
|
|
86
|
+
case 'PURGE':
|
|
87
|
+
await watcher.onDelete(event);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Discovers and returns an array of KvWatcher instances along with their corresponding metadata.
|
|
96
|
+
*
|
|
97
|
+
* @returns An array of objects containing the KvWatcher instance and its metadata.
|
|
98
|
+
*/
|
|
99
|
+
private discoverWatchers(): { instance: KvWatcher; metadata: WatchConfig }[] {
|
|
100
|
+
return this.discovery.getProviders().reduce(
|
|
101
|
+
(acc, watcher) => {
|
|
102
|
+
/**
|
|
103
|
+
* NOTE: This is a fix for TypeError in call .getMetadataByDecorator() above
|
|
104
|
+
* because some tokens are not instances of classes and have no constructor.
|
|
105
|
+
* */
|
|
106
|
+
const clsRef = watcher.instance?.constructor ?? watcher.metatype;
|
|
107
|
+
if (!clsRef) {
|
|
108
|
+
return acc;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const metadata = this.discovery.getMetadataByDecorator(
|
|
112
|
+
WatchKv,
|
|
113
|
+
watcher,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
if (metadata && metadata.connectionName === this.connectionName) {
|
|
117
|
+
acc.push({ instance: watcher.instance, metadata });
|
|
118
|
+
}
|
|
119
|
+
return acc;
|
|
120
|
+
},
|
|
121
|
+
[] as { instance: KvWatcher; metadata: WatchConfig }[],
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
}
|