lesgo 2.1.5 → 2.1.7
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/dist/services/ElastiCacheRedisService/deleteRedisCache.d.ts +1 -1
- package/dist/services/ElastiCacheRedisService/deleteRedisCache.js +8 -8
- package/dist/services/ElastiCacheRedisService/index.d.ts +1 -0
- package/dist/services/ElastiCacheRedisService/index.js +1 -0
- package/dist/services/ElastiCacheRedisService/scanRedisCache.d.ts +12 -0
- package/dist/services/ElastiCacheRedisService/scanRedisCache.js +83 -0
- package/dist/services/RDSAuroraMySQLProxyService/getMySQLProxyClient.d.ts +3 -3
- package/dist/services/RDSAuroraMySQLProxyService/getMySQLProxyClient.js +6 -8
- package/dist/services/RDSAuroraMySQLProxyService/query.js +2 -2
- package/dist/utils/cache/redis/deleteCache.d.ts +6 -5
- package/dist/utils/cache/redis/deleteCache.js +7 -6
- package/dist/utils/cache/redis/index.d.ts +1 -0
- package/dist/utils/cache/redis/index.js +1 -0
- package/dist/utils/cache/redis/scanCache.d.ts +23 -0
- package/dist/utils/cache/redis/scanCache.js +25 -0
- package/dist/utils/db/mysql/proxy/getClient.d.ts +1 -1
- package/dist/utils/secretsmanager/getSecretValue.d.ts +3 -0
- package/dist/utils/secretsmanager/getSecretValue.js +12 -1
- package/package.json +1 -1
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ClientOptions } from '../../types/aws';
|
|
2
|
-
declare const deleteRedisCache: (
|
|
2
|
+
declare const deleteRedisCache: (keys: string | string[], clientOpts?: ClientOptions) => Promise<number>;
|
|
3
3
|
export default deleteRedisCache;
|
|
@@ -31,20 +31,20 @@ var __awaiter =
|
|
|
31
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
|
-
import { logger
|
|
34
|
+
import { logger } from '../../utils';
|
|
35
35
|
import { LesgoException } from '../../exceptions';
|
|
36
36
|
import getElastiCacheRedisClient from './getElastiCacheRedisClient';
|
|
37
37
|
const FILE = 'lesgo.services.ElastiCacheRedis.deleteRedisCache';
|
|
38
|
-
const deleteRedisCache = (
|
|
38
|
+
const deleteRedisCache = (keys, clientOpts) =>
|
|
39
39
|
__awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
-
const input = validateFields({ key }, [
|
|
41
|
-
{ key: 'key', type: 'string', required: true },
|
|
42
|
-
]);
|
|
43
40
|
const client = yield getElastiCacheRedisClient(clientOpts);
|
|
44
41
|
let resp;
|
|
42
|
+
if (!Array.isArray(keys)) {
|
|
43
|
+
keys = [keys];
|
|
44
|
+
}
|
|
45
45
|
try {
|
|
46
|
-
resp = yield client.del(
|
|
47
|
-
logger.debug(`${FILE}::RECEIVED_RESPONSE`, { resp,
|
|
46
|
+
resp = yield client.del(...keys);
|
|
47
|
+
logger.debug(`${FILE}::RECEIVED_RESPONSE`, { resp, keys });
|
|
48
48
|
return resp;
|
|
49
49
|
} catch (err) {
|
|
50
50
|
throw new LesgoException(
|
|
@@ -53,7 +53,7 @@ const deleteRedisCache = (key, clientOpts) =>
|
|
|
53
53
|
500,
|
|
54
54
|
{
|
|
55
55
|
err,
|
|
56
|
-
|
|
56
|
+
keys,
|
|
57
57
|
clientOpts,
|
|
58
58
|
}
|
|
59
59
|
);
|
|
@@ -2,4 +2,5 @@ export { default as deleteRedisCache } from './deleteRedisCache';
|
|
|
2
2
|
export { default as disconnectElastiCacheRedisClient } from './disconnectElastiCacheRedisClient';
|
|
3
3
|
export { default as getElastiCacheRedisClient } from './getElastiCacheRedisClient';
|
|
4
4
|
export { default as getRedisCache } from './getRedisCache';
|
|
5
|
+
export { default as scanRedisCache } from './scanRedisCache';
|
|
5
6
|
export { default as setRedisCache } from './setRedisCache';
|
|
@@ -2,4 +2,5 @@ export { default as deleteRedisCache } from './deleteRedisCache';
|
|
|
2
2
|
export { default as disconnectElastiCacheRedisClient } from './disconnectElastiCacheRedisClient';
|
|
3
3
|
export { default as getElastiCacheRedisClient } from './getElastiCacheRedisClient';
|
|
4
4
|
export { default as getRedisCache } from './getRedisCache';
|
|
5
|
+
export { default as scanRedisCache } from './scanRedisCache';
|
|
5
6
|
export { default as setRedisCache } from './setRedisCache';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ClientOptions } from '../../types/aws';
|
|
2
|
+
/**
|
|
3
|
+
* Scans the Redis cache for keys matching a given pattern.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} pattern - The pattern to match keys against.
|
|
6
|
+
* @param {ClientOptions} [clientOpts] - Optional client options for connecting to the Redis instance.
|
|
7
|
+
* @returns {Promise<string[]>} - A promise that resolves to an array of keys matching the pattern.
|
|
8
|
+
*
|
|
9
|
+
* @throws {LesgoException} - Throws an exception if the scan operation fails.
|
|
10
|
+
*/
|
|
11
|
+
declare const scanRedisCache: (pattern: string, clientOpts?: ClientOptions) => Promise<string[]>;
|
|
12
|
+
export default scanRedisCache;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
var __awaiter =
|
|
2
|
+
(this && this.__awaiter) ||
|
|
3
|
+
function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) {
|
|
5
|
+
return value instanceof P
|
|
6
|
+
? value
|
|
7
|
+
: new P(function (resolve) {
|
|
8
|
+
resolve(value);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
12
|
+
function fulfilled(value) {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.next(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function rejected(value) {
|
|
20
|
+
try {
|
|
21
|
+
step(generator['throw'](value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function step(result) {
|
|
27
|
+
result.done
|
|
28
|
+
? resolve(result.value)
|
|
29
|
+
: adopt(result.value).then(fulfilled, rejected);
|
|
30
|
+
}
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
import { logger, validateFields } from '../../utils';
|
|
35
|
+
import { LesgoException } from '../../exceptions';
|
|
36
|
+
import getElastiCacheRedisClient from './getElastiCacheRedisClient';
|
|
37
|
+
const FILE = 'lesgo.services.ElastiCacheRedis.scanRedisCache';
|
|
38
|
+
/**
|
|
39
|
+
* Scans the Redis cache for keys matching a given pattern.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} pattern - The pattern to match keys against.
|
|
42
|
+
* @param {ClientOptions} [clientOpts] - Optional client options for connecting to the Redis instance.
|
|
43
|
+
* @returns {Promise<string[]>} - A promise that resolves to an array of keys matching the pattern.
|
|
44
|
+
*
|
|
45
|
+
* @throws {LesgoException} - Throws an exception if the scan operation fails.
|
|
46
|
+
*/
|
|
47
|
+
const scanRedisCache = (pattern, clientOpts) =>
|
|
48
|
+
__awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
+
const input = validateFields({ pattern }, [
|
|
50
|
+
{ key: 'pattern', type: 'string', required: true },
|
|
51
|
+
]);
|
|
52
|
+
const client = yield getElastiCacheRedisClient(clientOpts);
|
|
53
|
+
const keys = [];
|
|
54
|
+
try {
|
|
55
|
+
let cursor = '0'; // Start cursor
|
|
56
|
+
do {
|
|
57
|
+
// Perform SCAN operation
|
|
58
|
+
const [newCursor, matchedKeys] = yield client.scan(
|
|
59
|
+
cursor,
|
|
60
|
+
'MATCH',
|
|
61
|
+
pattern
|
|
62
|
+
);
|
|
63
|
+
cursor = newCursor;
|
|
64
|
+
if (matchedKeys.length > 0) {
|
|
65
|
+
keys.push(...matchedKeys);
|
|
66
|
+
}
|
|
67
|
+
} while (cursor !== '0'); // Continue until cursor is 0 (scan complete)
|
|
68
|
+
logger.debug(`${FILE}::RECEIVED_RESPONSE`, { keys, input });
|
|
69
|
+
} catch (err) {
|
|
70
|
+
throw new LesgoException(
|
|
71
|
+
'Failed to scan redis cache',
|
|
72
|
+
`${FILE}::SCAN_ERROR`,
|
|
73
|
+
500,
|
|
74
|
+
{
|
|
75
|
+
err,
|
|
76
|
+
input,
|
|
77
|
+
clientOpts,
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return keys;
|
|
82
|
+
});
|
|
83
|
+
export default scanRedisCache;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Pool, ConnectionOptions } from 'mysql2/promise';
|
|
2
2
|
import { RDSAuroraMySQLProxyClientOptions } from '../../types/aws';
|
|
3
3
|
export interface Singleton {
|
|
4
|
-
[key: string]:
|
|
4
|
+
[key: string]: Pool;
|
|
5
5
|
}
|
|
6
6
|
export declare const singleton: Singleton;
|
|
7
|
-
declare const getMySQLProxyClient: (connOptions?: ConnectionOptions, clientOpts?: RDSAuroraMySQLProxyClientOptions) => Promise<
|
|
7
|
+
declare const getMySQLProxyClient: (connOptions?: ConnectionOptions, clientOpts?: RDSAuroraMySQLProxyClientOptions) => Promise<Pool>;
|
|
8
8
|
export default getMySQLProxyClient;
|
|
@@ -31,7 +31,7 @@ var __awaiter =
|
|
|
31
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
|
-
import {
|
|
34
|
+
import { createPool } from 'mysql2/promise';
|
|
35
35
|
import { logger, isEmpty, validateFields } from '../../utils';
|
|
36
36
|
import { rds as rdsConfig } from '../../config';
|
|
37
37
|
import { getSecretValue } from '../../utils/secretsmanager';
|
|
@@ -53,10 +53,7 @@ const getMySQLProxyClient = (connOptions, clientOpts) =>
|
|
|
53
53
|
const databaseName =
|
|
54
54
|
options.databaseName || rdsConfig.aurora.mysql.databaseName;
|
|
55
55
|
if (!isEmpty(singleton[singletonConn])) {
|
|
56
|
-
logger.debug(`${FILE}::REUSE_RDS_CONNECTION
|
|
57
|
-
client: singleton[singletonConn],
|
|
58
|
-
region,
|
|
59
|
-
});
|
|
56
|
+
logger.debug(`${FILE}::REUSE_RDS_CONNECTION`);
|
|
60
57
|
return singleton[singletonConn];
|
|
61
58
|
}
|
|
62
59
|
const dbCredentials = yield getSecretValue(
|
|
@@ -80,17 +77,18 @@ const getMySQLProxyClient = (connOptions, clientOpts) =>
|
|
|
80
77
|
connOptions
|
|
81
78
|
);
|
|
82
79
|
logger.debug(`${FILE}::CONN_OPTS`, { connOpts });
|
|
83
|
-
const
|
|
80
|
+
const dbPool = createPool(
|
|
84
81
|
Object.assign(
|
|
85
82
|
{
|
|
86
83
|
user: validatedDbCredentials.username,
|
|
87
84
|
password: validatedDbCredentials.password,
|
|
85
|
+
connectionLimit: 20,
|
|
88
86
|
},
|
|
89
87
|
connOpts
|
|
90
88
|
)
|
|
91
89
|
);
|
|
92
|
-
singleton[singletonConn] =
|
|
90
|
+
singleton[singletonConn] = dbPool;
|
|
93
91
|
logger.debug(`${FILE}::NEW_RDS_CONNECTION`);
|
|
94
|
-
return
|
|
92
|
+
return dbPool;
|
|
95
93
|
});
|
|
96
94
|
export default getMySQLProxyClient;
|
|
@@ -41,9 +41,9 @@ const query = (sql, preparedValues, connOptions, clientOpts) =>
|
|
|
41
41
|
{ key: 'sql', type: 'string', required: true },
|
|
42
42
|
{ key: 'preparedValues', type: 'array', required: false },
|
|
43
43
|
]);
|
|
44
|
-
const
|
|
44
|
+
const pool = yield getClient(connOptions, clientOpts);
|
|
45
45
|
try {
|
|
46
|
-
const resp = yield
|
|
46
|
+
const resp = yield pool.execute(input.sql, input.preparedValues);
|
|
47
47
|
logger.debug(`${FILE}::RECEIVED_RESPONSE`, {
|
|
48
48
|
result: resp[0],
|
|
49
49
|
sql,
|
|
@@ -2,7 +2,7 @@ import { ClientOptions } from '../../../types/aws';
|
|
|
2
2
|
/**
|
|
3
3
|
* Deletes the cache value from the Redis cache.
|
|
4
4
|
*
|
|
5
|
-
* @param {string}
|
|
5
|
+
* @param {string | string[]} keys - The key(s) of the cache value to delete.
|
|
6
6
|
* @param {ClientOptions} clientOpts - Optional client options for Redis connection.
|
|
7
7
|
* @returns A promise that resolves to the deleted cache value.
|
|
8
8
|
* @throws {LesgoException} If there is an error deleting the cache.
|
|
@@ -11,9 +11,10 @@ import { ClientOptions } from '../../../types/aws';
|
|
|
11
11
|
* ```typescript
|
|
12
12
|
* import { deleteCache } from 'lesgo/utils/cache/redis';
|
|
13
13
|
*
|
|
14
|
-
* const
|
|
14
|
+
* const keys = 'myKey';
|
|
15
|
+
* // const keys = ['key1', 'key2'];
|
|
15
16
|
*
|
|
16
|
-
* await deleteCache(
|
|
17
|
+
* await deleteCache(keys);
|
|
17
18
|
*/
|
|
18
|
-
declare const
|
|
19
|
-
export default
|
|
19
|
+
declare const deleteCache: (keys: string | string[], clientOpts?: ClientOptions) => Promise<number>;
|
|
20
|
+
export default deleteCache;
|
|
@@ -2,7 +2,7 @@ import { deleteRedisCache } from '../../../services/ElastiCacheRedisService';
|
|
|
2
2
|
/**
|
|
3
3
|
* Deletes the cache value from the Redis cache.
|
|
4
4
|
*
|
|
5
|
-
* @param {string}
|
|
5
|
+
* @param {string | string[]} keys - The key(s) of the cache value to delete.
|
|
6
6
|
* @param {ClientOptions} clientOpts - Optional client options for Redis connection.
|
|
7
7
|
* @returns A promise that resolves to the deleted cache value.
|
|
8
8
|
* @throws {LesgoException} If there is an error deleting the cache.
|
|
@@ -11,11 +11,12 @@ import { deleteRedisCache } from '../../../services/ElastiCacheRedisService';
|
|
|
11
11
|
* ```typescript
|
|
12
12
|
* import { deleteCache } from 'lesgo/utils/cache/redis';
|
|
13
13
|
*
|
|
14
|
-
* const
|
|
14
|
+
* const keys = 'myKey';
|
|
15
|
+
* // const keys = ['key1', 'key2'];
|
|
15
16
|
*
|
|
16
|
-
* await deleteCache(
|
|
17
|
+
* await deleteCache(keys);
|
|
17
18
|
*/
|
|
18
|
-
const
|
|
19
|
-
return deleteRedisCache(
|
|
19
|
+
const deleteCache = (keys, clientOpts) => {
|
|
20
|
+
return deleteRedisCache(keys, clientOpts);
|
|
20
21
|
};
|
|
21
|
-
export default
|
|
22
|
+
export default deleteCache;
|
|
@@ -2,4 +2,5 @@ export { default as disconnectCache } from './disconnectCache';
|
|
|
2
2
|
export { default as deleteCache } from './deleteCache';
|
|
3
3
|
export { default as getCache } from './getCache';
|
|
4
4
|
export { default as getClient } from './getClient';
|
|
5
|
+
export { default as scanCache } from './scanCache';
|
|
5
6
|
export { default as setCache } from './setCache';
|
|
@@ -2,4 +2,5 @@ export { default as disconnectCache } from './disconnectCache';
|
|
|
2
2
|
export { default as deleteCache } from './deleteCache';
|
|
3
3
|
export { default as getCache } from './getCache';
|
|
4
4
|
export { default as getClient } from './getClient';
|
|
5
|
+
export { default as scanCache } from './scanCache';
|
|
5
6
|
export { default as setCache } from './setCache';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ClientOptions } from '../../../types/aws';
|
|
2
|
+
/**
|
|
3
|
+
* Scans the Redis cache for keys matching a given pattern.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} pattern - The pattern to match keys against.
|
|
6
|
+
* @param {ClientOptions} clientOpts - Optional client options for the cache client.
|
|
7
|
+
* @returns A promise that resolves when the value is retrieved from the cache.
|
|
8
|
+
*
|
|
9
|
+
* @throws {LesgoException} If there is an error retrieving the cache.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { scanCache } from 'lesgo/utils/cache/redis';
|
|
14
|
+
*
|
|
15
|
+
* const pattern = 'getUser:byId:*';
|
|
16
|
+
* // const pattern = 'getUser:*';
|
|
17
|
+
*
|
|
18
|
+
* const keys = await scanCache(pattern);
|
|
19
|
+
* console.log(keys); // Array of cache keys returned from the scan operation
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare const scanCache: (pattern: string, clientOpts?: ClientOptions) => Promise<string[]>;
|
|
23
|
+
export default scanCache;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { scanRedisCache } from '../../../services/ElastiCacheRedisService';
|
|
2
|
+
/**
|
|
3
|
+
* Scans the Redis cache for keys matching a given pattern.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} pattern - The pattern to match keys against.
|
|
6
|
+
* @param {ClientOptions} clientOpts - Optional client options for the cache client.
|
|
7
|
+
* @returns A promise that resolves when the value is retrieved from the cache.
|
|
8
|
+
*
|
|
9
|
+
* @throws {LesgoException} If there is an error retrieving the cache.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { scanCache } from 'lesgo/utils/cache/redis';
|
|
14
|
+
*
|
|
15
|
+
* const pattern = 'getUser:byId:*';
|
|
16
|
+
* // const pattern = 'getUser:*';
|
|
17
|
+
*
|
|
18
|
+
* const keys = await scanCache(pattern);
|
|
19
|
+
* console.log(keys); // Array of cache keys returned from the scan operation
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
const scanCache = (pattern, clientOpts) => {
|
|
23
|
+
return scanRedisCache(pattern, clientOpts);
|
|
24
|
+
};
|
|
25
|
+
export default scanCache;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { ConnectionOptions } from 'mysql2/promise';
|
|
2
2
|
import { RDSAuroraMySQLProxyClientOptions } from '../../../../types/aws';
|
|
3
|
-
declare const getClient: (connOptions?: ConnectionOptions, clientOpts?: RDSAuroraMySQLProxyClientOptions) => Promise<import("mysql2/promise").
|
|
3
|
+
declare const getClient: (connOptions?: ConnectionOptions, clientOpts?: RDSAuroraMySQLProxyClientOptions) => Promise<import("mysql2/promise").Pool>;
|
|
4
4
|
export default getClient;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { GetSecretValueOptions } from '../../services/SecretsManagerService/getSecretValue';
|
|
2
2
|
import { ClientOptions } from '../../types/aws';
|
|
3
|
+
export interface Secrets {
|
|
4
|
+
[key: string]: string | object;
|
|
5
|
+
}
|
|
3
6
|
declare const getSecretValue: (secretId: string, opts?: GetSecretValueOptions, clientOpts?: ClientOptions) => Promise<any>;
|
|
4
7
|
export default getSecretValue;
|
|
@@ -31,12 +31,18 @@ var __awaiter =
|
|
|
31
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
|
+
import { logger } from '../../utils';
|
|
34
35
|
import { LesgoException } from '../../exceptions';
|
|
35
36
|
import getSecretValueService from '../../services/SecretsManagerService/getSecretValue';
|
|
36
37
|
import isEmpty from '../isEmpty';
|
|
38
|
+
const secret = {};
|
|
37
39
|
const FILE = 'lesgo.utils.secretsmanager.getSecretValue';
|
|
38
40
|
const getSecretValue = (secretId, opts, clientOpts) =>
|
|
39
41
|
__awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
+
if (secret[secretId]) {
|
|
43
|
+
logger.debug(`${FILE}::CACHED_SECRET_VALUE`, { secretId });
|
|
44
|
+
return secret[secretId];
|
|
45
|
+
}
|
|
40
46
|
let secretString;
|
|
41
47
|
try {
|
|
42
48
|
const { SecretString } = yield getSecretValueService(
|
|
@@ -60,8 +66,13 @@ const getSecretValue = (secretId, opts, clientOpts) =>
|
|
|
60
66
|
);
|
|
61
67
|
}
|
|
62
68
|
try {
|
|
63
|
-
|
|
69
|
+
const secretObj = JSON.parse(secretString);
|
|
70
|
+
secret[secretId] = secretObj;
|
|
71
|
+
logger.debug(`${FILE}::SECRET_VALUE`, { secretId });
|
|
72
|
+
return secretObj;
|
|
64
73
|
} catch (error) {
|
|
74
|
+
secret[secretId] = secretString || '';
|
|
75
|
+
logger.debug(`${FILE}::SECRET_VALUE`, { secretId });
|
|
65
76
|
return secretString;
|
|
66
77
|
}
|
|
67
78
|
});
|