@tomsd/redis-client 1.0.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/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # @tomsd/redis-client
2
+
3
+ It's a wrapper of [ioredis](https://www.npmjs.com/package/ioredis).
4
+ See [redis-client.netlify.app](https://redis-client.netlify.app/) also.
5
+
6
+ ## Installation
7
+
8
+ ``` shell
9
+ npm install @tomsd/redis-client
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ <details open><summary><h3>importing Redis class.</h3></summary>
15
+
16
+ ``` typescript
17
+ import { Redis } from "@tomsd/redis-client";
18
+ ```
19
+
20
+ </details>
21
+
22
+ <details open><summary><h3>creating an instance.</h3></summary>
23
+
24
+ ``` typescript
25
+ interface Some {
26
+ key: string;
27
+ name: string;
28
+ message: string;
29
+ }
30
+
31
+ const redis = new Redis<Some>({
32
+ keyProp: "key",
33
+ options: {
34
+ port: 6379,
35
+ host: "somehost",
36
+ }
37
+ });
38
+ ```
39
+
40
+ </details>
41
+
42
+ <details open><summary><h3>setting data.</h3></summary>
43
+
44
+ ``` typescript
45
+ console.log(
46
+ await redis.set({
47
+ key: "key",
48
+ name: "name",
49
+ message: "message",
50
+ })
51
+ ) // { key: "key", name: "name", message: "message" }
52
+ ```
53
+
54
+ </details>
55
+
56
+ <details open><summary><h3>getting keys.</h3></summary>
57
+
58
+ ``` typescript
59
+ console.og(
60
+ await redis.getKeys()
61
+ ) // ["key"]
62
+ ```
63
+
64
+ </details>
65
+
66
+ <details open><summary><h3>getting data.</h3></summary>
67
+
68
+ ``` typescript
69
+ console.log(
70
+ await redis.get("key")
71
+ ) // { key: "key", name: "name", message: "message" }
72
+ ```
73
+
74
+ </details>
75
+
76
+ <details open><summary><h3>deleting data.</h3></summary>
77
+
78
+ ``` typescript
79
+ console.log(
80
+ await redis.del("key")
81
+ ) // 1
82
+ ```
83
+
84
+ </details>
@@ -0,0 +1,67 @@
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
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;
@@ -0,0 +1,20 @@
1
+ import IORedis, { RedisOptions } from "ioredis";
2
+ interface EasyRedisConfig {
3
+ keyProp?: string;
4
+ expireSeconds?: number;
5
+ options: RedisOptions;
6
+ }
7
+ export declare class Redis<T = any> {
8
+ config: EasyRedisConfig;
9
+ protected redis: IORedis | undefined;
10
+ constructor(config: EasyRedisConfig);
11
+ get keyProp(): string;
12
+ get expireSeconds(): number | undefined;
13
+ getRedis(): IORedis;
14
+ getKeys(): Promise<string[]>;
15
+ get(key: string): Promise<T>;
16
+ set(value: T | Partial<T>): Promise<T>;
17
+ del(key: string): Promise<number>;
18
+ disconnect(): void;
19
+ }
20
+ export {};
@@ -0,0 +1,74 @@
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
+ var _a, _b, _c;
31
+ return __awaiter(this, void 0, void 0, function* () {
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
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
+ }
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@tomsd/redis-client",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json",
10
+ "format": "npm run format:src && npm run format:test",
11
+ "format:src": "prettier --write src/",
12
+ "format:test": "prettier --write __test__/",
13
+ "lint": "npm run lint:src && npm run lint:test",
14
+ "lint:src": "eslint src/**/*.ts",
15
+ "lint:test": "eslint __test__/**/*.ts",
16
+ "prepare": "husky install",
17
+ "start-redis": "npm run build-redis && npm run serve-redis",
18
+ "build-redis": "docker image build -f Dockerfile -t redis-alpine-image:v1.0.0 .",
19
+ "serve-redis": "docker run --name redis-alpine-instance -p 6379:6379 --rm -d redis-alpine-image:v1.0.0",
20
+ "stop-redis": "docker container stop redis-alpine-instance",
21
+ "serve:doc": "mdbook --serve --directory docs",
22
+ "test": "jest"
23
+ },
24
+ "lint-staged": {
25
+ "src/**/*.ts": [
26
+ "npm run lint:src",
27
+ "npm run format:src"
28
+ ],
29
+ "__test__/**/*.ts": [
30
+ "npm run lint:test",
31
+ "npm run format:test"
32
+ ]
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/tomsdoo/redis-client.git"
37
+ },
38
+ "keywords": [],
39
+ "author": "tom",
40
+ "license": "MIT",
41
+ "bugs": {
42
+ "url": "https://github.com/tomsdoo/redis-client/issues"
43
+ },
44
+ "homepage": "https://github.com/tomsdoo/redis-client#readme",
45
+ "devDependencies": {
46
+ "@babel/core": "^7.20.12",
47
+ "@babel/preset-env": "^7.20.2",
48
+ "@babel/preset-typescript": "^7.18.6",
49
+ "@tomsd/md-book": "^1.0.2",
50
+ "@types/uuid": "^9.0.0",
51
+ "@typescript-eslint/eslint-plugin": "^5.48.0",
52
+ "babel-jest": "^29.3.1",
53
+ "eslint": "^8.31.0",
54
+ "eslint-config-prettier": "^8.6.0",
55
+ "eslint-config-standard-with-typescript": "^26.0.0",
56
+ "eslint-plugin-import": "^2.26.0",
57
+ "eslint-plugin-n": "^15.6.0",
58
+ "eslint-plugin-promise": "^6.1.1",
59
+ "husky": "^8.0.3",
60
+ "jest": "^29.3.1",
61
+ "lint-staged": "^13.1.0",
62
+ "prettier": "^2.8.2",
63
+ "ts-node": "^10.9.1",
64
+ "typescript": "^4.9.4"
65
+ },
66
+ "dependencies": {
67
+ "ioredis": "^5.2.4",
68
+ "uuid": "^9.0.0"
69
+ }
70
+ }