node-consul-service 1.0.17 → 1.0.19
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/README.md +32 -0
- package/dist/consul/dataLink.d.ts +1 -1
- package/dist/index.cjs.js +4 -3
- package/dist/index.esm.js +4 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -64,7 +64,39 @@ const instances = await getServiceInstances('service-name');
|
|
|
64
64
|
|
|
65
65
|
// Get a random instance of a service
|
|
66
66
|
const instance = await getRandomServiceInstance('service-name');
|
|
67
|
+
|
|
68
|
+
### dataLink Function
|
|
69
|
+
This function is used to link data from different services. It aggregates data from multiple sources and links them based on common identifiers.
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
const { dataLink } = require('node-consul-service');
|
|
73
|
+
|
|
74
|
+
// Example of using dataLink
|
|
75
|
+
const data = [
|
|
76
|
+
{ userId: '123', name: 'John' },
|
|
77
|
+
{ userId: '456', name: 'Jane' }
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
const schema = [
|
|
81
|
+
{
|
|
82
|
+
filed: 'userId', // Field name in the original data
|
|
83
|
+
service: 'user-service', // Target service name
|
|
84
|
+
path: '/api/users/batch', // API endpoint path
|
|
85
|
+
cacheGetter: async (ids) => {
|
|
86
|
+
// Optional function to get data from cache
|
|
87
|
+
return await cache.getUsers(ids);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
const result = await dataLink(data, schema);
|
|
67
93
|
```
|
|
68
94
|
|
|
95
|
+
#### Schema Properties
|
|
96
|
+
- `filed`: Field name in the original data that contains the identifier
|
|
97
|
+
- `service`: Name of the service to be called
|
|
98
|
+
- `path`: API endpoint path in the target service
|
|
99
|
+
- `cacheGetter`: Optional function to get data from cache before calling the service
|
|
100
|
+
|
|
69
101
|
### License
|
|
70
102
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -2,7 +2,7 @@ interface LinkSchema {
|
|
|
2
2
|
filed: string;
|
|
3
3
|
service: string;
|
|
4
4
|
path: string;
|
|
5
|
-
cacheGetter?: (
|
|
5
|
+
cacheGetter?: (ids: string[]) => Promise<(any | null)[]>;
|
|
6
6
|
}
|
|
7
7
|
export declare function dataLink(data: any[], schema: LinkSchema[]): Promise<any[]>;
|
|
8
8
|
export {};
|
package/dist/index.cjs.js
CHANGED
|
@@ -19422,14 +19422,15 @@ async function dataLink(data, schema) {
|
|
|
19422
19422
|
let cacheMap = new Map();
|
|
19423
19423
|
let idsToFetch = uniqueIds;
|
|
19424
19424
|
if (cacheGetter) {
|
|
19425
|
-
const cacheResults = await
|
|
19425
|
+
const cacheResults = await cacheGetter(uniqueIds);
|
|
19426
19426
|
idsToFetch = [];
|
|
19427
19427
|
cacheResults.forEach((item, index) => {
|
|
19428
|
+
const id = uniqueIds[index];
|
|
19428
19429
|
if (item) {
|
|
19429
|
-
cacheMap.set(
|
|
19430
|
+
cacheMap.set(id, item);
|
|
19430
19431
|
}
|
|
19431
19432
|
else {
|
|
19432
|
-
idsToFetch.push(
|
|
19433
|
+
idsToFetch.push(id);
|
|
19433
19434
|
}
|
|
19434
19435
|
});
|
|
19435
19436
|
}
|
package/dist/index.esm.js
CHANGED
|
@@ -19420,14 +19420,15 @@ async function dataLink(data, schema) {
|
|
|
19420
19420
|
let cacheMap = new Map();
|
|
19421
19421
|
let idsToFetch = uniqueIds;
|
|
19422
19422
|
if (cacheGetter) {
|
|
19423
|
-
const cacheResults = await
|
|
19423
|
+
const cacheResults = await cacheGetter(uniqueIds);
|
|
19424
19424
|
idsToFetch = [];
|
|
19425
19425
|
cacheResults.forEach((item, index) => {
|
|
19426
|
+
const id = uniqueIds[index];
|
|
19426
19427
|
if (item) {
|
|
19427
|
-
cacheMap.set(
|
|
19428
|
+
cacheMap.set(id, item);
|
|
19428
19429
|
}
|
|
19429
19430
|
else {
|
|
19430
|
-
idsToFetch.push(
|
|
19431
|
+
idsToFetch.push(id);
|
|
19431
19432
|
}
|
|
19432
19433
|
});
|
|
19433
19434
|
}
|