@tomsd/redis-client 1.0.3 → 1.1.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/dist/index.cjs +88 -0
- package/dist/index.d.cts +22 -0
- package/dist/{esm/index.d.ts → index.d.ts} +5 -3
- package/dist/index.js +53 -0
- package/package.json +19 -29
- package/dist/cjs/index.js +0 -67
- package/dist/esm/index.js +0 -74
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
Redis: () => Redis
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
var import_ioredis = __toESM(require("ioredis"), 1);
|
|
37
|
+
var import_uuid = require("uuid");
|
|
38
|
+
var Redis = class {
|
|
39
|
+
config;
|
|
40
|
+
redis;
|
|
41
|
+
constructor(config) {
|
|
42
|
+
this.config = config;
|
|
43
|
+
this.redis = void 0;
|
|
44
|
+
}
|
|
45
|
+
get keyProp() {
|
|
46
|
+
return this.config.keyProp ?? "_id";
|
|
47
|
+
}
|
|
48
|
+
get expireSeconds() {
|
|
49
|
+
return this.config.expireSeconds;
|
|
50
|
+
}
|
|
51
|
+
getRedis() {
|
|
52
|
+
this.redis = this.redis ?? new import_ioredis.default(this.config.options);
|
|
53
|
+
return this.redis;
|
|
54
|
+
}
|
|
55
|
+
async getKeys() {
|
|
56
|
+
const keyFilter = `${this.config?.options?.keyPrefix ?? ""}*`;
|
|
57
|
+
const sliceKey = (s) => s.slice(this.config?.options?.keyPrefix?.length ?? 0);
|
|
58
|
+
return await this.getRedis().keys(keyFilter).then((keys) => keys.map(sliceKey));
|
|
59
|
+
}
|
|
60
|
+
async get(key) {
|
|
61
|
+
return await this.getRedis().get(key).then((value) => {
|
|
62
|
+
if (value == null) {
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
return JSON.parse(value);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async set(value) {
|
|
69
|
+
const redis = this.getRedis();
|
|
70
|
+
const savingObj = {
|
|
71
|
+
[this.keyProp]: (0, import_uuid.v4)(),
|
|
72
|
+
...value
|
|
73
|
+
};
|
|
74
|
+
const key = savingObj[this.keyProp];
|
|
75
|
+
return this.expireSeconds !== void 0 ? await redis.set(key, JSON.stringify(savingObj), "EX", this.expireSeconds).then(() => savingObj) : await redis.set(key, JSON.stringify(savingObj)).then(() => savingObj);
|
|
76
|
+
}
|
|
77
|
+
async del(key) {
|
|
78
|
+
return await this.getRedis().del(key);
|
|
79
|
+
}
|
|
80
|
+
disconnect() {
|
|
81
|
+
this.getRedis().disconnect();
|
|
82
|
+
this.redis = void 0;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
Redis
|
|
88
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import IORedis, { RedisOptions } from 'ioredis';
|
|
2
|
+
|
|
3
|
+
interface EasyRedisConfig {
|
|
4
|
+
keyProp?: string;
|
|
5
|
+
expireSeconds?: number;
|
|
6
|
+
options: RedisOptions;
|
|
7
|
+
}
|
|
8
|
+
declare class Redis<T = any> {
|
|
9
|
+
config: EasyRedisConfig;
|
|
10
|
+
protected redis: IORedis | undefined;
|
|
11
|
+
constructor(config: EasyRedisConfig);
|
|
12
|
+
get keyProp(): string;
|
|
13
|
+
get expireSeconds(): number | undefined;
|
|
14
|
+
getRedis(): IORedis;
|
|
15
|
+
getKeys(): Promise<string[]>;
|
|
16
|
+
get(key: string): Promise<T>;
|
|
17
|
+
set(value: T | Partial<T>): Promise<T>;
|
|
18
|
+
del(key: string): Promise<number>;
|
|
19
|
+
disconnect(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { Redis };
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import IORedis, {
|
|
1
|
+
import IORedis, { RedisOptions } from 'ioredis';
|
|
2
|
+
|
|
2
3
|
interface EasyRedisConfig {
|
|
3
4
|
keyProp?: string;
|
|
4
5
|
expireSeconds?: number;
|
|
5
6
|
options: RedisOptions;
|
|
6
7
|
}
|
|
7
|
-
|
|
8
|
+
declare class Redis<T = any> {
|
|
8
9
|
config: EasyRedisConfig;
|
|
9
10
|
protected redis: IORedis | undefined;
|
|
10
11
|
constructor(config: EasyRedisConfig);
|
|
@@ -17,4 +18,5 @@ export declare class Redis<T = any> {
|
|
|
17
18
|
del(key: string): Promise<number>;
|
|
18
19
|
disconnect(): void;
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
+
|
|
22
|
+
export { Redis };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import IORedis from "ioredis";
|
|
3
|
+
import { v4 as uuid } from "uuid";
|
|
4
|
+
var Redis = class {
|
|
5
|
+
config;
|
|
6
|
+
redis;
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.config = config;
|
|
9
|
+
this.redis = void 0;
|
|
10
|
+
}
|
|
11
|
+
get keyProp() {
|
|
12
|
+
return this.config.keyProp ?? "_id";
|
|
13
|
+
}
|
|
14
|
+
get expireSeconds() {
|
|
15
|
+
return this.config.expireSeconds;
|
|
16
|
+
}
|
|
17
|
+
getRedis() {
|
|
18
|
+
this.redis = this.redis ?? new IORedis(this.config.options);
|
|
19
|
+
return this.redis;
|
|
20
|
+
}
|
|
21
|
+
async getKeys() {
|
|
22
|
+
const keyFilter = `${this.config?.options?.keyPrefix ?? ""}*`;
|
|
23
|
+
const sliceKey = (s) => s.slice(this.config?.options?.keyPrefix?.length ?? 0);
|
|
24
|
+
return await this.getRedis().keys(keyFilter).then((keys) => keys.map(sliceKey));
|
|
25
|
+
}
|
|
26
|
+
async get(key) {
|
|
27
|
+
return await this.getRedis().get(key).then((value) => {
|
|
28
|
+
if (value == null) {
|
|
29
|
+
return void 0;
|
|
30
|
+
}
|
|
31
|
+
return JSON.parse(value);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async set(value) {
|
|
35
|
+
const redis = this.getRedis();
|
|
36
|
+
const savingObj = {
|
|
37
|
+
[this.keyProp]: uuid(),
|
|
38
|
+
...value
|
|
39
|
+
};
|
|
40
|
+
const key = savingObj[this.keyProp];
|
|
41
|
+
return this.expireSeconds !== void 0 ? await redis.set(key, JSON.stringify(savingObj), "EX", this.expireSeconds).then(() => savingObj) : await redis.set(key, JSON.stringify(savingObj)).then(() => savingObj);
|
|
42
|
+
}
|
|
43
|
+
async del(key) {
|
|
44
|
+
return await this.getRedis().del(key);
|
|
45
|
+
}
|
|
46
|
+
disconnect() {
|
|
47
|
+
this.getRedis().disconnect();
|
|
48
|
+
this.redis = void 0;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
export {
|
|
52
|
+
Redis
|
|
53
|
+
};
|
package/package.json
CHANGED
|
@@ -1,35 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomsd/redis-client",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "It's a wrapper of ioredis.",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"require": "./dist/index.cjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
8
13
|
"files": [
|
|
9
14
|
"dist"
|
|
10
15
|
],
|
|
11
16
|
"scripts": {
|
|
12
|
-
"build": "
|
|
13
|
-
"
|
|
14
|
-
"format:src": "prettier --write src/",
|
|
15
|
-
"format:test": "prettier --write __test__/",
|
|
16
|
-
"lint": "npm run lint:src && npm run lint:test",
|
|
17
|
-
"lint:src": "eslint src/**/*.ts",
|
|
18
|
-
"lint:test": "eslint __test__/**/*.ts",
|
|
17
|
+
"build": "tsup",
|
|
18
|
+
"lint": "biome check",
|
|
19
19
|
"prepare": "husky",
|
|
20
20
|
"start-redis": "docker run --rm -d -p 6379:6379 --name redis-instance redis:alpine",
|
|
21
21
|
"stop-redis": "docker container stop redis-instance",
|
|
22
22
|
"serve:doc": "mdbook --serve --directory docs",
|
|
23
|
-
"test": "
|
|
23
|
+
"test": "vitest"
|
|
24
24
|
},
|
|
25
25
|
"lint-staged": {
|
|
26
26
|
"src/**/*.ts": [
|
|
27
|
-
"npm run lint
|
|
28
|
-
"npm run format:src"
|
|
27
|
+
"npm run lint"
|
|
29
28
|
],
|
|
30
29
|
"__test__/**/*.ts": [
|
|
31
|
-
"npm run lint
|
|
32
|
-
"npm run format:test"
|
|
30
|
+
"npm run lint"
|
|
33
31
|
]
|
|
34
32
|
},
|
|
35
33
|
"repository": {
|
|
@@ -46,24 +44,16 @@
|
|
|
46
44
|
},
|
|
47
45
|
"homepage": "https://github.com/tomsdoo/redis-client#readme",
|
|
48
46
|
"devDependencies": {
|
|
49
|
-
"@
|
|
50
|
-
"@babel/preset-env": "7.26.0",
|
|
51
|
-
"@babel/preset-typescript": "7.26.0",
|
|
47
|
+
"@biomejs/biome": "1.9.4",
|
|
52
48
|
"@tomsd/md-book": "1.3.3",
|
|
49
|
+
"@types/node": "22.10.1",
|
|
53
50
|
"@types/uuid": "10.0.0",
|
|
54
|
-
"@typescript-eslint/eslint-plugin": "8.16.0",
|
|
55
|
-
"babel-jest": "29.7.0",
|
|
56
|
-
"eslint": "8.57.1",
|
|
57
|
-
"eslint-config-prettier": "9.1.0",
|
|
58
|
-
"eslint-plugin-import": "2.31.0",
|
|
59
|
-
"eslint-plugin-n": "17.14.0",
|
|
60
|
-
"eslint-plugin-promise": "7.2.1",
|
|
61
51
|
"husky": "9.1.7",
|
|
62
|
-
"jest": "29.7.0",
|
|
63
52
|
"lint-staged": "15.2.10",
|
|
64
|
-
"prettier": "3.4.1",
|
|
65
53
|
"ts-node": "10.9.2",
|
|
66
|
-
"
|
|
54
|
+
"tsup": "8.3.5",
|
|
55
|
+
"typescript": "5.7.2",
|
|
56
|
+
"vitest": "2.1.6"
|
|
67
57
|
},
|
|
68
58
|
"dependencies": {
|
|
69
59
|
"ioredis": "5.4.1",
|
package/dist/cjs/index.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Redis = void 0;
|
|
7
|
-
const ioredis_1 = __importDefault(require("ioredis"));
|
|
8
|
-
const uuid_1 = require("uuid");
|
|
9
|
-
class Redis {
|
|
10
|
-
constructor(config) {
|
|
11
|
-
this.config = config;
|
|
12
|
-
this.redis = undefined;
|
|
13
|
-
}
|
|
14
|
-
get keyProp() {
|
|
15
|
-
var _a;
|
|
16
|
-
return (_a = this.config.keyProp) !== null && _a !== void 0 ? _a : "_id";
|
|
17
|
-
}
|
|
18
|
-
get expireSeconds() {
|
|
19
|
-
return this.config.expireSeconds;
|
|
20
|
-
}
|
|
21
|
-
getRedis() {
|
|
22
|
-
var _a;
|
|
23
|
-
this.redis = (_a = this.redis) !== null && _a !== void 0 ? _a : new ioredis_1.default(this.config.options);
|
|
24
|
-
return this.redis;
|
|
25
|
-
}
|
|
26
|
-
async getKeys() {
|
|
27
|
-
var _a, _b, _c;
|
|
28
|
-
const keyFilter = `${(_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.keyPrefix) !== null && _c !== void 0 ? _c : ""}*`;
|
|
29
|
-
const sliceKey = (s) => { var _a, _b, _c, _d; return s.slice((_d = (_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.keyPrefix) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0); };
|
|
30
|
-
return await this.getRedis()
|
|
31
|
-
.keys(keyFilter)
|
|
32
|
-
.then((keys) => keys.map(sliceKey));
|
|
33
|
-
}
|
|
34
|
-
async get(key) {
|
|
35
|
-
return await this.getRedis()
|
|
36
|
-
.get(key)
|
|
37
|
-
.then((value) => {
|
|
38
|
-
if (value == null) {
|
|
39
|
-
return undefined;
|
|
40
|
-
}
|
|
41
|
-
return JSON.parse(value);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
async set(value) {
|
|
45
|
-
const redis = this.getRedis();
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
47
|
-
const savingObj = {
|
|
48
|
-
[this.keyProp]: (0, uuid_1.v4)(),
|
|
49
|
-
...value,
|
|
50
|
-
};
|
|
51
|
-
// @ts-expect-error prop name
|
|
52
|
-
const key = savingObj[this.keyProp];
|
|
53
|
-
return this.expireSeconds !== undefined
|
|
54
|
-
? await redis
|
|
55
|
-
.set(key, JSON.stringify(savingObj), "EX", this.expireSeconds)
|
|
56
|
-
.then(() => savingObj)
|
|
57
|
-
: await redis.set(key, JSON.stringify(savingObj)).then(() => savingObj);
|
|
58
|
-
}
|
|
59
|
-
async del(key) {
|
|
60
|
-
return await this.getRedis().del(key);
|
|
61
|
-
}
|
|
62
|
-
disconnect() {
|
|
63
|
-
this.getRedis().disconnect();
|
|
64
|
-
this.redis = undefined;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
exports.Redis = Redis;
|
package/dist/esm/index.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import IORedis from "ioredis";
|
|
11
|
-
import { v4 as uuid } from "uuid";
|
|
12
|
-
export class Redis {
|
|
13
|
-
constructor(config) {
|
|
14
|
-
this.config = config;
|
|
15
|
-
this.redis = undefined;
|
|
16
|
-
}
|
|
17
|
-
get keyProp() {
|
|
18
|
-
var _a;
|
|
19
|
-
return (_a = this.config.keyProp) !== null && _a !== void 0 ? _a : "_id";
|
|
20
|
-
}
|
|
21
|
-
get expireSeconds() {
|
|
22
|
-
return this.config.expireSeconds;
|
|
23
|
-
}
|
|
24
|
-
getRedis() {
|
|
25
|
-
var _a;
|
|
26
|
-
this.redis = (_a = this.redis) !== null && _a !== void 0 ? _a : new IORedis(this.config.options);
|
|
27
|
-
return this.redis;
|
|
28
|
-
}
|
|
29
|
-
getKeys() {
|
|
30
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
-
var _a, _b, _c;
|
|
32
|
-
const keyFilter = `${(_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.keyPrefix) !== null && _c !== void 0 ? _c : ""}*`;
|
|
33
|
-
const sliceKey = (s) => { var _a, _b, _c, _d; return s.slice((_d = (_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.keyPrefix) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0); };
|
|
34
|
-
return yield this.getRedis()
|
|
35
|
-
.keys(keyFilter)
|
|
36
|
-
.then((keys) => keys.map(sliceKey));
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
get(key) {
|
|
40
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
return yield this.getRedis()
|
|
42
|
-
.get(key)
|
|
43
|
-
.then((value) => {
|
|
44
|
-
if (value == null) {
|
|
45
|
-
return undefined;
|
|
46
|
-
}
|
|
47
|
-
return JSON.parse(value);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
set(value) {
|
|
52
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
const redis = this.getRedis();
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
55
|
-
const savingObj = Object.assign({ [this.keyProp]: uuid() }, value);
|
|
56
|
-
// @ts-expect-error prop name
|
|
57
|
-
const key = savingObj[this.keyProp];
|
|
58
|
-
return this.expireSeconds !== undefined
|
|
59
|
-
? yield redis
|
|
60
|
-
.set(key, JSON.stringify(savingObj), "EX", this.expireSeconds)
|
|
61
|
-
.then(() => savingObj)
|
|
62
|
-
: yield redis.set(key, JSON.stringify(savingObj)).then(() => savingObj);
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
del(key) {
|
|
66
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
return yield this.getRedis().del(key);
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
disconnect() {
|
|
71
|
-
this.getRedis().disconnect();
|
|
72
|
-
this.redis = undefined;
|
|
73
|
-
}
|
|
74
|
-
}
|