@pi-r/redis 0.5.0 → 0.6.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/client/index.js +67 -63
- package/package.json +4 -4
package/client/index.js
CHANGED
|
@@ -8,10 +8,10 @@ const Db = require("@e-mc/db");
|
|
|
8
8
|
const DbPool = require('@e-mc/db/pool');
|
|
9
9
|
const REDIS_V4 = Db.checkSemVer("redis" /* STRINGS.MODULE_NAME */, 4);
|
|
10
10
|
class RedisPool extends DbPool {
|
|
11
|
-
getConnection() {
|
|
12
|
-
return
|
|
11
|
+
async getConnection() {
|
|
12
|
+
return this.client;
|
|
13
13
|
}
|
|
14
|
-
close() {
|
|
14
|
+
async close() {
|
|
15
15
|
return this.client.disconnect();
|
|
16
16
|
}
|
|
17
17
|
isEmpty() {
|
|
@@ -22,7 +22,7 @@ class RedisPool extends DbPool {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
const POOL_STATE = {};
|
|
25
|
-
function getClient(db, options, poolKey) {
|
|
25
|
+
async function getClient(db, options, poolKey) {
|
|
26
26
|
const socket = options.socket || (options.socket = {});
|
|
27
27
|
if (!poolKey) {
|
|
28
28
|
socket.keepAlive = false;
|
|
@@ -93,56 +93,56 @@ async function setCredential(item) {
|
|
|
93
93
|
client.password = password;
|
|
94
94
|
}
|
|
95
95
|
const usePool = item.usePool;
|
|
96
|
-
if (usePool) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
96
|
+
if (!usePool) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
let pool;
|
|
100
|
+
username = undefined;
|
|
101
|
+
password = undefined;
|
|
102
|
+
if (typeof usePool === 'string' && (username = client.username || (0, util_1.parseConnectionString)(uri)?.username)) {
|
|
103
|
+
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
104
|
+
if (pool) {
|
|
105
|
+
pool.add(item, password);
|
|
106
|
+
return;
|
|
106
107
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
try {
|
|
133
|
-
const target = new RedisPool(await getClient(redis, client, poolKey), poolKey, username && password ? { username, password } : undefined).add(item);
|
|
134
|
-
target.parent = POOL_STATE;
|
|
135
|
-
target.success = 1;
|
|
136
|
-
}
|
|
137
|
-
catch {
|
|
138
|
-
item.usePool = false;
|
|
139
|
-
delete client.isolationPoolOptions;
|
|
140
|
-
}
|
|
108
|
+
}
|
|
109
|
+
const poolKey = getPoolKey(client);
|
|
110
|
+
if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
|
|
111
|
+
pool.add(item);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const config = this.getPoolConfig("redis" /* STRINGS.MODULE_NAME */, password);
|
|
115
|
+
const poolOptions = client.isolationPoolOptions || (client.isolationPoolOptions = {});
|
|
116
|
+
if (config) {
|
|
117
|
+
const { min, max, idle, queue_max, queue_idle, timeout } = config;
|
|
118
|
+
if (min >= 0) {
|
|
119
|
+
poolOptions.min ?? (poolOptions.min = min);
|
|
120
|
+
}
|
|
121
|
+
if (max > 0) {
|
|
122
|
+
poolOptions.max ?? (poolOptions.max = max);
|
|
123
|
+
}
|
|
124
|
+
if (idle > 0) {
|
|
125
|
+
poolOptions.idleTimeoutMillis ?? (poolOptions.idleTimeoutMillis = idle);
|
|
126
|
+
}
|
|
127
|
+
if (queue_max >= 0) {
|
|
128
|
+
poolOptions.maxWaitingClients ?? (poolOptions.maxWaitingClients = queue_max);
|
|
129
|
+
}
|
|
130
|
+
if (queue_idle > 0) {
|
|
131
|
+
poolOptions.softIdleTimeoutMillis ?? (poolOptions.softIdleTimeoutMillis = queue_idle);
|
|
141
132
|
}
|
|
142
|
-
|
|
143
|
-
|
|
133
|
+
if (timeout > 0) {
|
|
134
|
+
poolOptions.acquireTimeoutMillis ?? (poolOptions.acquireTimeoutMillis = timeout);
|
|
144
135
|
}
|
|
145
136
|
}
|
|
137
|
+
try {
|
|
138
|
+
const target = new RedisPool(await getClient(redis, client, poolKey), poolKey, username && password ? { username, password } : undefined).add(item);
|
|
139
|
+
target.parent = POOL_STATE;
|
|
140
|
+
target.success = 1;
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
item.usePool = false;
|
|
144
|
+
delete client.isolationPoolOptions;
|
|
145
|
+
}
|
|
146
146
|
}
|
|
147
147
|
exports.setCredential = setCredential;
|
|
148
148
|
async function executeQuery(item, options) {
|
|
@@ -204,6 +204,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
204
204
|
item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
|
|
205
205
|
return client;
|
|
206
206
|
};
|
|
207
|
+
if (this.host?.username) {
|
|
208
|
+
this.applyCommand(...batch);
|
|
209
|
+
}
|
|
207
210
|
for (let i = 0; i < length; ++i) {
|
|
208
211
|
const item = batch[i];
|
|
209
212
|
const { source, uri, key, format = 'HASH', search, aggregate, update, options: clientOptions = {}, cacheObjectKey, ignoreCache } = item;
|
|
@@ -240,10 +243,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
240
243
|
queryString = Db.asString(aggregate, true);
|
|
241
244
|
}
|
|
242
245
|
else if (!targetObject) {
|
|
243
|
-
queryString = Db.asString(key);
|
|
246
|
+
queryString = Db.asString(key, true);
|
|
244
247
|
}
|
|
245
248
|
else if (cacheObjectKey) {
|
|
246
|
-
queryString = Db.asString(key) + '_' + targetObject.toString() + cacheObjectKey;
|
|
249
|
+
queryString = Db.asString(key, true) + '_' + targetObject.toString() + cacheObjectKey;
|
|
247
250
|
}
|
|
248
251
|
if (queryString) {
|
|
249
252
|
queryString += '_' + format;
|
|
@@ -283,7 +286,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
283
286
|
const b = (arg) => typeof arg === 'number' || Buffer.isBuffer(arg) ? arg.toString() : arg;
|
|
284
287
|
if (update) {
|
|
285
288
|
commandType = this.commandType.UPDATE;
|
|
286
|
-
const values = (!Array.isArray(update) ? [update] : update).map(target => {
|
|
289
|
+
const values = (!Array.isArray(update) ? [update] : update).map(async (target) => {
|
|
287
290
|
return new Promise(async (success, failed) => {
|
|
288
291
|
let { key: k, value: v, format: f = 'HASH', NX, XX, EX = 0, PX = 0, EXAT = 0, PXAT = 0, options: setOptions = {} } = target;
|
|
289
292
|
if (v === undefined) {
|
|
@@ -460,13 +463,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
460
463
|
}
|
|
461
464
|
}
|
|
462
465
|
catch (err) {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
client.ft.dropIndex(idx).catch(err => this.writeFail([`Unable to drop ${target === search ? 'search' : 'aggregate'} index`, idx], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false }));
|
|
466
|
+
if (err instanceof Error) {
|
|
467
|
+
throw err;
|
|
468
|
+
}
|
|
467
469
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
+
finally {
|
|
471
|
+
if (idx && idx !== index) {
|
|
472
|
+
client.ft.dropIndex(idx).catch(err => this.writeFail([`Unable to drop ${target === search ? 'search' : 'aggregate'} index`, idx], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false }));
|
|
473
|
+
}
|
|
470
474
|
}
|
|
471
475
|
}
|
|
472
476
|
else if (key) {
|
|
@@ -474,10 +478,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
474
478
|
if (Array.isArray(key)) {
|
|
475
479
|
switch (format.toUpperCase()) {
|
|
476
480
|
case 'HKEYS':
|
|
477
|
-
data = await Promise.all(key.map(k => client.hKeys(...command ? [command, k] : [k])));
|
|
481
|
+
data = await Promise.all(key.map(async (k) => client.hKeys(...command ? [command, k] : [k])));
|
|
478
482
|
break;
|
|
479
483
|
case 'HVALS':
|
|
480
|
-
data = await Promise.all(key.map(k => client.hVals(...command ? [command, k] : [k])));
|
|
484
|
+
data = await Promise.all(key.map(async (k) => client.hVals(...command ? [command, k] : [k])));
|
|
481
485
|
break;
|
|
482
486
|
case 'JSON':
|
|
483
487
|
data = (await (command ? client.json.mGet(command, key.map(c => b(c)), item.path || '$') : client.json.mGet(key.map(c => b(c)), item.path || '$'))).flat();
|
|
@@ -548,12 +552,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
548
552
|
}
|
|
549
553
|
}
|
|
550
554
|
return this.processRows(batch, tasks, {
|
|
551
|
-
disconnect: () => clients.forEach(item => item.disconnect()),
|
|
555
|
+
disconnect: () => clients.forEach(async (item) => item.disconnect()),
|
|
552
556
|
parallel
|
|
553
557
|
}, outResult);
|
|
554
558
|
}
|
|
555
559
|
exports.executeBatchQuery = executeBatchQuery;
|
|
556
|
-
function checkTimeout(value, limit = 0) {
|
|
560
|
+
async function checkTimeout(value, limit = 0) {
|
|
557
561
|
return DbPool.checkTimeout(POOL_STATE, value, limit);
|
|
558
562
|
}
|
|
559
563
|
exports.checkTimeout = checkTimeout;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/redis",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
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.8.0",
|
|
25
|
+
"@e-mc/types": "^0.8.0",
|
|
26
|
+
"redis": "^4.6.12"
|
|
27
27
|
}
|
|
28
28
|
}
|