@pi-r/redis 0.3.4 → 0.3.6
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/LICENSE +6 -6
- package/README.md +4 -6
- package/client/index.d.ts +6 -6
- package/client/index.js +10 -9
- package/client/pool.js +36 -0
- package/package.json +28 -28
- package/types/index.d.ts +66 -66
package/LICENSE
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
Copyright
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
-
|
|
5
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
-
|
|
1
|
+
Copyright 2023 An Pham
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
7
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
package/client/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { IDbSourceClient } from '@e-mc/db/types';
|
|
2
|
-
|
|
3
|
-
import type { RedisDataSource } from '../types';
|
|
4
|
-
|
|
5
|
-
declare const Redis: IDbSourceClient<RedisDataSource>;
|
|
6
|
-
|
|
1
|
+
import type { IDbSourceClient } from '@e-mc/db/types';
|
|
2
|
+
|
|
3
|
+
import type { RedisDataSource } from '../types';
|
|
4
|
+
|
|
5
|
+
declare const Redis: IDbSourceClient<RedisDataSource>;
|
|
6
|
+
|
|
7
7
|
export = Redis;
|
package/client/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = void 0;
|
|
4
|
-
const redis = require("redis");
|
|
5
4
|
const util_1 = require("@e-mc/db/util");
|
|
6
5
|
const types_1 = require("@e-mc/types");
|
|
7
6
|
const Db = require("@e-mc/db");
|
|
@@ -108,6 +107,7 @@ async function setCredential(item) {
|
|
|
108
107
|
}
|
|
109
108
|
const poolKey = getPoolKey(client);
|
|
110
109
|
if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
|
|
110
|
+
const db = require("redis");
|
|
111
111
|
const config = this.getPoolConfig("redis", password);
|
|
112
112
|
const poolOptions = client.isolationPoolOptions || (client.isolationPoolOptions = {});
|
|
113
113
|
if (config) {
|
|
@@ -129,9 +129,9 @@ async function setCredential(item) {
|
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
try {
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
132
|
+
const redis = new RedisPool(await getClient(db, client, poolKey), poolKey, username && password ? { username, password } : undefined).add(item);
|
|
133
|
+
redis.parent = POOL_STATE;
|
|
134
|
+
redis.success = 1;
|
|
135
135
|
}
|
|
136
136
|
catch {
|
|
137
137
|
item.usePool = false;
|
|
@@ -154,6 +154,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
154
154
|
if (length === 0) {
|
|
155
155
|
return [];
|
|
156
156
|
}
|
|
157
|
+
const db = require("redis");
|
|
157
158
|
let parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss;
|
|
158
159
|
if ((0, types_1.isPlainObject)(options)) {
|
|
159
160
|
({ parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
|
|
@@ -195,7 +196,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
195
196
|
(credential.isolationPoolOptions || (credential.isolationPoolOptions = {})).max = size;
|
|
196
197
|
}
|
|
197
198
|
}
|
|
198
|
-
clients.push(client = await getClient(
|
|
199
|
+
clients.push(client = await getClient(db, credential));
|
|
199
200
|
}
|
|
200
201
|
if (connectOnce) {
|
|
201
202
|
redisClient = client;
|
|
@@ -230,7 +231,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
230
231
|
const targetObject = typeof checkObject === 'string' ? this.hasCoerce("redis", 'options', uuidKey) && (0, types_1.asFunction)(checkObject) : checkObject;
|
|
231
232
|
const renewCache = ignoreCache === 0;
|
|
232
233
|
const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
|
|
233
|
-
const command = (0, types_1.isPlainObject)(clientOptions.command) ?
|
|
234
|
+
const command = (0, types_1.isPlainObject)(clientOptions.command) ? db.commandOptions(clientOptions.command) : null;
|
|
234
235
|
let queryString = '', rows;
|
|
235
236
|
if (caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache) {
|
|
236
237
|
if ((0, types_1.isObject)(search)) {
|
|
@@ -292,7 +293,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
292
293
|
}
|
|
293
294
|
const failKey = () => failed((0, types_1.errorMessage)(f, "Invalid key", Db.asString(k) || "Unknown"));
|
|
294
295
|
const failValue = () => failed((0, types_1.errorMessage)(f, "Invalid value", Db.asString(v) || "Unknown"));
|
|
295
|
-
const commandSet = (0, types_1.isPlainObject)(setOptions.command) &&
|
|
296
|
+
const commandSet = (0, types_1.isPlainObject)(setOptions.command) && db.commandOptions(setOptions.command);
|
|
296
297
|
let pending;
|
|
297
298
|
if ((f = f.toUpperCase()) === 'JSON') {
|
|
298
299
|
if (typeof k !== 'string') {
|
|
@@ -453,7 +454,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
453
454
|
try {
|
|
454
455
|
if (target === search) {
|
|
455
456
|
const reply = await (command ? client.ft.search(command, index || idx, query, clientOptions.search) : client.ft.search(index || idx, query, clientOptions.search));
|
|
456
|
-
rows = reply.documents.map(doc => (doc.value
|
|
457
|
+
rows = reply.documents.map(doc => (doc.value['__id__'] = doc.id) && doc.value);
|
|
457
458
|
}
|
|
458
459
|
else {
|
|
459
460
|
({ results: rows } = await (command ? client.ft.aggregate(command, index || idx, query, clientOptions.aggregate) : client.ft.aggregate(index || idx, query, clientOptions.aggregate)));
|
|
@@ -500,7 +501,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
500
501
|
break;
|
|
501
502
|
default:
|
|
502
503
|
if (item.field) {
|
|
503
|
-
data = client.hGet(...command ? [command, key, item.field] : [key, item.field]);
|
|
504
|
+
data = await client.hGet(...command ? [command, key, item.field] : [key, item.field]);
|
|
504
505
|
}
|
|
505
506
|
else {
|
|
506
507
|
data = await client.hGetAll(...command ? [command, key] : [key]);
|
package/client/pool.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const DbPool = require('@e-mc/db/pool');
|
|
3
|
+
const POOL_ACTIVE = new WeakSet();
|
|
4
|
+
class RedisPool extends DbPool {
|
|
5
|
+
static asString(credential) {
|
|
6
|
+
if (credential.url) {
|
|
7
|
+
return JSON.stringify(credential);
|
|
8
|
+
}
|
|
9
|
+
return super.asString(credential);
|
|
10
|
+
}
|
|
11
|
+
static sanitize(credential) {
|
|
12
|
+
if (!POOL_ACTIVE.has(credential) || credential.uuidKey) {
|
|
13
|
+
return credential;
|
|
14
|
+
}
|
|
15
|
+
if ('socket' in credential || 'isolationPoolOptions' in credential) {
|
|
16
|
+
return { ...credential, socket: credential.socket?.tls ? { tls: true } : undefined, isolationPoolOptions: undefined };
|
|
17
|
+
}
|
|
18
|
+
return credential;
|
|
19
|
+
}
|
|
20
|
+
async getConnection(credential) {
|
|
21
|
+
if (credential) {
|
|
22
|
+
POOL_ACTIVE.add(credential);
|
|
23
|
+
}
|
|
24
|
+
return this.client;
|
|
25
|
+
}
|
|
26
|
+
async close() {
|
|
27
|
+
return this.client.disconnect();
|
|
28
|
+
}
|
|
29
|
+
isEmpty() {
|
|
30
|
+
return this.closed;
|
|
31
|
+
}
|
|
32
|
+
get closed() {
|
|
33
|
+
return !this.client.isOpen;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
module.exports = RedisPool;
|
package/package.json
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@pi-r/redis",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "Redis client driver for E-mc.",
|
|
5
|
-
"main": "client/index.js",
|
|
6
|
-
"types": "client/index.d.ts",
|
|
7
|
-
"publishConfig": {
|
|
8
|
-
"access": "public"
|
|
9
|
-
},
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
13
|
-
"directory": "src/db/redis"
|
|
14
|
-
},
|
|
15
|
-
"keywords": [
|
|
16
|
-
"squared",
|
|
17
|
-
"e-mc",
|
|
18
|
-
"squared-functions"
|
|
19
|
-
],
|
|
20
|
-
"author": "An Pham <anpham6@gmail.com>",
|
|
21
|
-
"license": "MIT",
|
|
22
|
-
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
|
-
"dependencies": {
|
|
24
|
-
"@e-mc/db": "^0.6.
|
|
25
|
-
"@e-mc/types": "^0.6.
|
|
26
|
-
"redis": "^4.6.
|
|
27
|
-
}
|
|
28
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@pi-r/redis",
|
|
3
|
+
"version": "0.3.6",
|
|
4
|
+
"description": "Redis client driver for E-mc.",
|
|
5
|
+
"main": "client/index.js",
|
|
6
|
+
"types": "client/index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
13
|
+
"directory": "src/db/redis"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"squared",
|
|
17
|
+
"e-mc",
|
|
18
|
+
"squared-functions"
|
|
19
|
+
],
|
|
20
|
+
"author": "An Pham <anpham6@gmail.com>",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@e-mc/db": "^0.6.16",
|
|
25
|
+
"@e-mc/types": "^0.6.16",
|
|
26
|
+
"redis": "^4.6.15"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
import type { DbDataSource } from '@e-mc/types/lib/squared';
|
|
2
|
-
|
|
3
|
-
import type { CascadeAction, ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
|
|
4
|
-
|
|
5
|
-
import type { CommandOptions } from '@redis/client/dist/lib/command-options';
|
|
6
|
-
import type { ClientCommandOptions } from '@redis/client/dist/lib/client';
|
|
7
|
-
import type { RedisClientOptions } from '@redis/client';
|
|
8
|
-
import type { RedisCommandArgument } from '@redis/client/dist/lib/commands';
|
|
9
|
-
import type { RedisJSON } from '@redis/json/dist/commands';
|
|
10
|
-
import type { AggregateOptions, RediSearchSchema, SearchOptions } from 'redis';
|
|
11
|
-
|
|
12
|
-
export interface RedisDataSource extends DbDataSource<string, PlainObject, ArrayOf<RedisSetValue> | ArrayOf<RedisJSONValue>, string | RedisCredential, string>, CascadeAction, AuthValue {
|
|
13
|
-
source: "redis";
|
|
14
|
-
key?: ArrayOf<RedisCommandArgument>;
|
|
15
|
-
field?: RedisCommandArgument;
|
|
16
|
-
path?: string;
|
|
17
|
-
format?: RedisFormat | "HKEYS" | "HVALS";
|
|
18
|
-
search?: RedisQuery;
|
|
19
|
-
aggregate?: RedisQuery;
|
|
20
|
-
options?: {
|
|
21
|
-
client?: RedisClientOptions;
|
|
22
|
-
command?: RedisCommandOptions;
|
|
23
|
-
get?: PlainObject;
|
|
24
|
-
search?: SearchOptions;
|
|
25
|
-
aggregate?: AggregateOptions;
|
|
26
|
-
};
|
|
27
|
-
database?: number;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export type RedisCredential = ServerAuth;
|
|
31
|
-
|
|
32
|
-
export interface RedisSetValue<T = "HASH" | undefined, U = RedisCommandArgument, V = RedisCommandValue | RedisHSETObject> {
|
|
33
|
-
format: T;
|
|
34
|
-
key?: U;
|
|
35
|
-
field?: V;
|
|
36
|
-
value?: V;
|
|
37
|
-
NX?: boolean;
|
|
38
|
-
XX?: boolean;
|
|
39
|
-
EX?: number;
|
|
40
|
-
PX?: number;
|
|
41
|
-
EXAT?: number;
|
|
42
|
-
PXAT?: number;
|
|
43
|
-
options?: {
|
|
44
|
-
set?: PlainObject;
|
|
45
|
-
expire?: { [K in RedisExpireCondition]?: boolean; };
|
|
46
|
-
command?: RedisCommandOptions;
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface RedisJSONValue extends Omit<RedisSetValue<"JSON", string, RedisJSON>, "field" | "EX" | "PX" | "EXAT" | "PXAT"> {
|
|
51
|
-
path?: string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface RedisQuery {
|
|
55
|
-
index?: string;
|
|
56
|
-
schema?: RediSearchSchema | string;
|
|
57
|
-
query?: string;
|
|
58
|
-
options?: PlainObject;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export type RedisFormat = "HASH" | "JSON";
|
|
62
|
-
export type RedisExpireCondition = "NX" | "XX" | "GT" | "LT";
|
|
63
|
-
export type RedisCommandValue = RedisCommandArgument | number;
|
|
64
|
-
export type RedisCommandOptions = CommandOptions<ClientCommandOptions>;
|
|
65
|
-
export type RedisHSETObject = Record<NumString, RedisCommandValue>;
|
|
66
|
-
|
|
1
|
+
import type { DbDataSource } from '@e-mc/types/lib/squared';
|
|
2
|
+
|
|
3
|
+
import type { CascadeAction, ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
|
|
4
|
+
|
|
5
|
+
import type { CommandOptions } from '@redis/client/dist/lib/command-options';
|
|
6
|
+
import type { ClientCommandOptions } from '@redis/client/dist/lib/client';
|
|
7
|
+
import type { RedisClientOptions } from '@redis/client';
|
|
8
|
+
import type { RedisCommandArgument } from '@redis/client/dist/lib/commands';
|
|
9
|
+
import type { RedisJSON } from '@redis/json/dist/commands';
|
|
10
|
+
import type { AggregateOptions, RediSearchSchema, SearchOptions } from 'redis';
|
|
11
|
+
|
|
12
|
+
export interface RedisDataSource extends DbDataSource<string, PlainObject, ArrayOf<RedisSetValue> | ArrayOf<RedisJSONValue>, string | RedisCredential, string>, CascadeAction, AuthValue {
|
|
13
|
+
source: "redis";
|
|
14
|
+
key?: ArrayOf<RedisCommandArgument>;
|
|
15
|
+
field?: RedisCommandArgument;
|
|
16
|
+
path?: string;
|
|
17
|
+
format?: RedisFormat | "HKEYS" | "HVALS";
|
|
18
|
+
search?: RedisQuery;
|
|
19
|
+
aggregate?: RedisQuery;
|
|
20
|
+
options?: {
|
|
21
|
+
client?: RedisClientOptions;
|
|
22
|
+
command?: RedisCommandOptions;
|
|
23
|
+
get?: PlainObject;
|
|
24
|
+
search?: SearchOptions;
|
|
25
|
+
aggregate?: AggregateOptions;
|
|
26
|
+
};
|
|
27
|
+
database?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type RedisCredential = ServerAuth;
|
|
31
|
+
|
|
32
|
+
export interface RedisSetValue<T = "HASH" | undefined, U = RedisCommandArgument, V = RedisCommandValue | RedisHSETObject> {
|
|
33
|
+
format: T;
|
|
34
|
+
key?: U;
|
|
35
|
+
field?: V;
|
|
36
|
+
value?: V;
|
|
37
|
+
NX?: boolean;
|
|
38
|
+
XX?: boolean;
|
|
39
|
+
EX?: number;
|
|
40
|
+
PX?: number;
|
|
41
|
+
EXAT?: number;
|
|
42
|
+
PXAT?: number;
|
|
43
|
+
options?: {
|
|
44
|
+
set?: PlainObject;
|
|
45
|
+
expire?: { [K in RedisExpireCondition]?: boolean; };
|
|
46
|
+
command?: RedisCommandOptions;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface RedisJSONValue extends Omit<RedisSetValue<"JSON", string, RedisJSON>, "field" | "EX" | "PX" | "EXAT" | "PXAT"> {
|
|
51
|
+
path?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface RedisQuery {
|
|
55
|
+
index?: string;
|
|
56
|
+
schema?: RediSearchSchema | string;
|
|
57
|
+
query?: string;
|
|
58
|
+
options?: PlainObject;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type RedisFormat = "HASH" | "JSON";
|
|
62
|
+
export type RedisExpireCondition = "NX" | "XX" | "GT" | "LT";
|
|
63
|
+
export type RedisCommandValue = RedisCommandArgument | number;
|
|
64
|
+
export type RedisCommandOptions = CommandOptions<ClientCommandOptions>;
|
|
65
|
+
export type RedisHSETObject = Record<NumString, RedisCommandValue>;
|
|
66
|
+
|
|
67
67
|
export type { RedisCommandArgument, RedisJSON };
|