@pi-r/redis 0.6.7 → 0.7.1
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 +3 -6
- package/client/index.js +35 -47
- package/package.json +4 -4
- package/types/index.d.ts +5 -5
package/README.md
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
### @pi-r/redis
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
https://e-mc.readthedocs.io/en/latest/db/redis.html
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
- [squared](https://squared.readthedocs.io)
|
|
7
|
-
|
|
8
|
-
## LICENSE
|
|
5
|
+
### LICENSE
|
|
9
6
|
|
|
10
7
|
MIT
|
package/client/index.js
CHANGED
|
@@ -22,7 +22,7 @@ class RedisPool extends DbPool {
|
|
|
22
22
|
const POOL_STATE = {};
|
|
23
23
|
const POOL_ACTIVE = new WeakSet();
|
|
24
24
|
async function getClient(db, options, poolKey) {
|
|
25
|
-
const socket = options.socket
|
|
25
|
+
const socket = options.socket ||= {};
|
|
26
26
|
if (!poolKey) {
|
|
27
27
|
socket.keepAlive = false;
|
|
28
28
|
}
|
|
@@ -36,21 +36,19 @@ async function getClient(db, options, poolKey) {
|
|
|
36
36
|
delete POOL_STATE[poolKey];
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
|
-
client.connect().then(() => resolve(client)).catch(err => reject(err));
|
|
39
|
+
client.connect().then(() => resolve(client)).catch((err) => reject(err));
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
function removePoolProperties(options) {
|
|
43
|
-
var _a;
|
|
44
43
|
if (!options.uuidKey && ('socket' in options || 'isolationPoolOptions' in options) && POOL_ACTIVE.has(options)) {
|
|
45
|
-
return { ...options, socket:
|
|
44
|
+
return { ...options, socket: options.socket?.tls ? { tls: true } : undefined, isolationPoolOptions: undefined };
|
|
46
45
|
}
|
|
47
46
|
return options;
|
|
48
47
|
}
|
|
49
48
|
const getPoolKey = (options) => options.url && JSON.stringify(options);
|
|
50
49
|
async function setCredential(item) {
|
|
51
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
52
50
|
const credential = this.getCredential(item);
|
|
53
|
-
const options = item.options
|
|
51
|
+
const options = item.options ||= {};
|
|
54
52
|
const client = (0, types_1.isPlainObject)(options.client) ? options.client : options.client = {};
|
|
55
53
|
let { uri, username, password, database = 0 } = item;
|
|
56
54
|
if (database > 0) {
|
|
@@ -58,21 +56,12 @@ async function setCredential(item) {
|
|
|
58
56
|
}
|
|
59
57
|
if (credential) {
|
|
60
58
|
const auth = (0, util_1.parseServerAuth)(credential, 6379);
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
const value = credential.database;
|
|
64
|
-
if (+value > 0) {
|
|
65
|
-
database = +value;
|
|
66
|
-
}
|
|
67
|
-
delete credential.database;
|
|
68
|
-
}
|
|
69
|
-
if (database > 0) {
|
|
70
|
-
client.database = database;
|
|
71
|
-
}
|
|
59
|
+
if (auth.database && (database = +auth.database) > 0) {
|
|
60
|
+
client.database ||= database;
|
|
72
61
|
}
|
|
73
|
-
uri
|
|
74
|
-
username
|
|
75
|
-
password
|
|
62
|
+
uri ||= (auth.protocol || 'redis:') + '//' + (auth.server || 'localhost:' + (auth.port ? auth.port : 6379));
|
|
63
|
+
username ||= auth.username;
|
|
64
|
+
password ||= auth.password;
|
|
76
65
|
}
|
|
77
66
|
else {
|
|
78
67
|
delete item.credential;
|
|
@@ -80,7 +69,7 @@ async function setCredential(item) {
|
|
|
80
69
|
if (!uri) {
|
|
81
70
|
throw (0, types_1.errorMessage)("redis", 'Not defined - uri');
|
|
82
71
|
}
|
|
83
|
-
if (
|
|
72
|
+
if (client.socket?.tls) {
|
|
84
73
|
this.readTLSConfig(client.socket);
|
|
85
74
|
}
|
|
86
75
|
client.url = uri;
|
|
@@ -98,7 +87,7 @@ async function setCredential(item) {
|
|
|
98
87
|
username = undefined;
|
|
99
88
|
password = undefined;
|
|
100
89
|
if ((0, types_1.isString)(usePool)) {
|
|
101
|
-
if (username = client.username || (
|
|
90
|
+
if (username = client.username || (0, util_1.parseConnectionString)(uri)?.username) {
|
|
102
91
|
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
103
92
|
if (pool) {
|
|
104
93
|
pool.add(item, password);
|
|
@@ -111,26 +100,26 @@ async function setCredential(item) {
|
|
|
111
100
|
}
|
|
112
101
|
}
|
|
113
102
|
const config = this.getPoolConfig("redis", password);
|
|
114
|
-
const poolOptions = client.isolationPoolOptions
|
|
103
|
+
const poolOptions = client.isolationPoolOptions ||= {};
|
|
115
104
|
if (config) {
|
|
116
105
|
const { min, max, idle, queue_max, queue_idle, timeout } = config;
|
|
117
106
|
if (min >= 0) {
|
|
118
|
-
|
|
107
|
+
poolOptions.min ??= min;
|
|
119
108
|
}
|
|
120
109
|
if (max > 0) {
|
|
121
|
-
|
|
110
|
+
poolOptions.max ??= max;
|
|
122
111
|
}
|
|
123
112
|
if (idle > 0) {
|
|
124
|
-
|
|
113
|
+
poolOptions.idleTimeoutMillis ??= idle;
|
|
125
114
|
}
|
|
126
115
|
if (queue_max >= 0) {
|
|
127
|
-
|
|
116
|
+
poolOptions.maxWaitingClients ??= queue_max;
|
|
128
117
|
}
|
|
129
118
|
if (queue_idle > 0) {
|
|
130
|
-
|
|
119
|
+
poolOptions.softIdleTimeoutMillis ??= queue_idle;
|
|
131
120
|
}
|
|
132
121
|
if (timeout > 0) {
|
|
133
|
-
|
|
122
|
+
poolOptions.acquireTimeoutMillis ??= timeout;
|
|
134
123
|
}
|
|
135
124
|
}
|
|
136
125
|
const poolKey = getPoolKey(client);
|
|
@@ -154,7 +143,6 @@ async function executeQuery(item, options) {
|
|
|
154
143
|
}
|
|
155
144
|
exports.executeQuery = executeQuery;
|
|
156
145
|
async function executeBatchQuery(batch, options = '', outResult) {
|
|
157
|
-
var _a, _b, _c;
|
|
158
146
|
const length = batch.length;
|
|
159
147
|
if (length === 0) {
|
|
160
148
|
return [];
|
|
@@ -177,12 +165,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
177
165
|
parallel = !batch.some(item => item.parallel === false);
|
|
178
166
|
}
|
|
179
167
|
if (!parallel) {
|
|
180
|
-
outResult
|
|
168
|
+
outResult ||= new Array(length);
|
|
181
169
|
}
|
|
182
170
|
const caching = this.hasCache("redis", sessionKey);
|
|
183
171
|
const tasks = new Array(length);
|
|
184
172
|
const clients = [];
|
|
185
|
-
let redisClient, redisCredential, redisCache = 0, onceCredential = connectOnce ?
|
|
173
|
+
let redisClient, redisCredential, redisCache = 0, onceCredential = connectOnce ? batch[0].options?.client : undefined;
|
|
186
174
|
const getConnection = async (item, credential) => {
|
|
187
175
|
item.transactionState = 64;
|
|
188
176
|
let client;
|
|
@@ -198,7 +186,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
198
186
|
if (connectOnce && parallel) {
|
|
199
187
|
const size = length - redisCache;
|
|
200
188
|
if (size > 1) {
|
|
201
|
-
(credential.isolationPoolOptions
|
|
189
|
+
(credential.isolationPoolOptions ||= {}).max = size;
|
|
202
190
|
}
|
|
203
191
|
}
|
|
204
192
|
clients.push(client = await getClient(redis, credential));
|
|
@@ -210,7 +198,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
210
198
|
item.transactionState &= ~64;
|
|
211
199
|
return client;
|
|
212
200
|
};
|
|
213
|
-
if (
|
|
201
|
+
if (this.host?.username) {
|
|
214
202
|
this.applyCommand(...batch);
|
|
215
203
|
}
|
|
216
204
|
for (let i = 0; i < length; ++i) {
|
|
@@ -236,7 +224,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
236
224
|
}
|
|
237
225
|
item.transactionState = 1;
|
|
238
226
|
const command = (0, types_1.isPlainObject)(clientOptions.command) ? redis.commandOptions(clientOptions.command) : null;
|
|
239
|
-
const uuidKey = (credential.uuidKey
|
|
227
|
+
const uuidKey = (credential.uuidKey ||= (onceCredential ? batch[0] : item).credential?.uuidKey);
|
|
240
228
|
const targetObject = typeof checkObject === 'string' ? this.hasCoerce("redis", 'options', uuidKey) && (0, types_1.asFunction)(checkObject) : checkObject;
|
|
241
229
|
const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
|
|
242
230
|
let queryString = '', rows;
|
|
@@ -309,10 +297,8 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
309
297
|
failKey();
|
|
310
298
|
return;
|
|
311
299
|
}
|
|
312
|
-
if (this.hasCoerce("redis", 'options', uuidKey)) {
|
|
313
|
-
|
|
314
|
-
({ outV: v } = (0, types_1.coerceObject)({ outV: v }));
|
|
315
|
-
}
|
|
300
|
+
if (this.hasCoerce("redis", 'options', uuidKey) && (0, types_1.isString)(v) && v.startsWith('new')) {
|
|
301
|
+
({ outV: v } = (0, types_1.coerceObject)({ outV: v }));
|
|
316
302
|
}
|
|
317
303
|
let { path = '$', command: cmd, start, stop, index } = target;
|
|
318
304
|
const name = ((0, types_1.isString)(cmd) ? cmd.toUpperCase() : 'SET');
|
|
@@ -352,8 +338,8 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
352
338
|
const data = (Array.isArray(v) ? v : [v])
|
|
353
339
|
.filter((m) => m.value !== undefined)
|
|
354
340
|
.map((m) => {
|
|
355
|
-
m.key
|
|
356
|
-
m.path
|
|
341
|
+
m.key ||= k;
|
|
342
|
+
m.path ||= path;
|
|
357
343
|
return m;
|
|
358
344
|
});
|
|
359
345
|
if (data.length) {
|
|
@@ -478,7 +464,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
478
464
|
}
|
|
479
465
|
}
|
|
480
466
|
if (ttl) {
|
|
481
|
-
PX = await client.pTTL(k).catch(err => {
|
|
467
|
+
PX = await client.pTTL(k).catch((err) => {
|
|
482
468
|
failed(err);
|
|
483
469
|
return 0;
|
|
484
470
|
});
|
|
@@ -496,13 +482,13 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
496
482
|
failed((0, types_1.errorMessage)(f, "Invalid value", has(EX) ? 'EX: ' + EX : has(PX) ? 'PX: ' + PX : has(EXAT) ? 'EXAT: ' + EX : 'PXAT: ' + PXAT));
|
|
497
483
|
}
|
|
498
484
|
})
|
|
499
|
-
.catch(err => failed(err));
|
|
485
|
+
.catch((err) => failed(err));
|
|
500
486
|
}
|
|
501
487
|
else {
|
|
502
488
|
success(target);
|
|
503
489
|
}
|
|
504
490
|
})
|
|
505
|
-
.catch(err => failed(err));
|
|
491
|
+
.catch((err) => failed(err));
|
|
506
492
|
}
|
|
507
493
|
else {
|
|
508
494
|
failValue();
|
|
@@ -518,7 +504,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
518
504
|
}
|
|
519
505
|
}
|
|
520
506
|
})
|
|
521
|
-
.catch(err => {
|
|
507
|
+
.catch((err) => {
|
|
522
508
|
if (this.handleFail(err, item, { errorQuery, commandType })) {
|
|
523
509
|
reject(err);
|
|
524
510
|
}
|
|
@@ -549,7 +535,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
549
535
|
}
|
|
550
536
|
finally {
|
|
551
537
|
if (idx && idx !== index) {
|
|
552
|
-
client.ft.dropIndex(idx).catch(err => this.writeFail([`Unable to drop ${target === search ? 'search' : 'aggregate'} index`, idx], err, { type: 65536, fatal: false }));
|
|
538
|
+
client.ft.dropIndex(idx).catch((err) => this.writeFail([`Unable to drop ${target === search ? 'search' : 'aggregate'} index`, idx], err, { type: 65536, fatal: false }));
|
|
553
539
|
}
|
|
554
540
|
}
|
|
555
541
|
}
|
|
@@ -632,7 +618,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
632
618
|
}
|
|
633
619
|
}
|
|
634
620
|
return this.processRows(batch, tasks, {
|
|
635
|
-
disconnect: () => clients.forEach(
|
|
621
|
+
disconnect: () => clients.forEach(item => {
|
|
622
|
+
item.disconnect();
|
|
623
|
+
}),
|
|
636
624
|
parallel
|
|
637
625
|
}, outResult);
|
|
638
626
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/redis",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Redis client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@e-mc/db": "^0.
|
|
25
|
-
"@e-mc/types": "^0.
|
|
26
|
-
"redis": "^4.6.
|
|
24
|
+
"@e-mc/db": "^0.9.2",
|
|
25
|
+
"@e-mc/types": "^0.9.2",
|
|
26
|
+
"redis": "^4.6.14"
|
|
27
27
|
}
|
|
28
28
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type { RedisCommandArgument } from '@redis/client/dist/lib/commands';
|
|
|
9
9
|
import type { RedisJSON } from '@redis/json/dist/commands';
|
|
10
10
|
import type { RediSearchSchema, SearchOptions, SetOptions } from 'redis';
|
|
11
11
|
|
|
12
|
-
export interface RedisDataSource extends DbDataSource<string, PlainObject, ArrayOf<RedisSetValue> | ArrayOf<RedisJSONValue>,
|
|
12
|
+
export interface RedisDataSource extends DbDataSource<string, PlainObject, ArrayOf<RedisSetValue> | ArrayOf<RedisJSONValue>, RedisCredential, string>, CascadeAction, AuthValue {
|
|
13
13
|
source: "redis";
|
|
14
14
|
key?: ArrayOf<RedisCommandArgument>;
|
|
15
15
|
field?: RedisCommandArgument;
|
|
@@ -55,9 +55,9 @@ export interface RedisSetValue extends RedisCommand<"HASH", RedisCommandArgument
|
|
|
55
55
|
export interface RedisJSONValue extends RedisCommand<"JSON", string, ArrayOf<RedisJSON>> {
|
|
56
56
|
command?: RedisCommandJSON;
|
|
57
57
|
path?: string;
|
|
58
|
-
start?:
|
|
59
|
-
stop?:
|
|
60
|
-
index?:
|
|
58
|
+
start?: number | string;
|
|
59
|
+
stop?: number | string;
|
|
60
|
+
index?: number | string;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
export interface RedisQuery {
|
|
@@ -78,6 +78,6 @@ export type RedisCommandJSON = "ARRAPPEND" | "ARRINDEX" | "ARRINSERT" | "ARRPOP"
|
|
|
78
78
|
export type RedisExpireCondition = "NX" | "XX" | "GT" | "LT";
|
|
79
79
|
export type RedisCommandValue = RedisCommandArgument | number;
|
|
80
80
|
export type RedisCommandOptions = CommandOptions<ClientCommandOptions>;
|
|
81
|
-
export type RedisHSETObject = Record<
|
|
81
|
+
export type RedisHSETObject = Record<number | string, RedisCommandValue>;
|
|
82
82
|
|
|
83
83
|
export type { RedisCommandArgument, RedisJSON };
|