@bejibun/redis 0.1.35 → 0.1.36

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/CHANGELOG.md CHANGED
@@ -3,6 +3,22 @@ All notable changes to this project will be documented in this file.
3
3
 
4
4
  ---
5
5
 
6
+ ## [v0.1.36](https://github.com/crenata/bejibun-redis/compare/v0.1.35...v0.1.36) - 2025-12-11
7
+
8
+ ### 🩹 Fixes
9
+
10
+ ### 📖 Changes
11
+ - Set random connection name for `.setClient()` and disconnect after used then clear the connection lists.
12
+
13
+ #### Upgrade [@bejibun/utils](https://github.com/crenata/bejibun-utils) to [v0.1.27](https://github.com/crenata/bejibun-utils/releases/tag/v0.1.24)
14
+
15
+ ### ❤️Contributors
16
+ - Havea Crenata ([@crenata](https://github.com/crenata))
17
+
18
+ **Full Changelog**: https://github.com/crenata/bejibun-redis/blob/master/CHANGELOG.md
19
+
20
+ ---
21
+
6
22
  ## [v0.1.35](https://github.com/crenata/bejibun-redis/compare/v0.1.34...v0.1.35) - 2025-12-07
7
23
 
8
24
  ### 🩹 Fixes
package/README.md CHANGED
@@ -74,6 +74,15 @@ export default class TestController extends BaseController {
74
74
  await Redis.connection("local").set("connection", "This is using custom connection.");
75
75
  const connection = await Redis.connection("local").get("connection");
76
76
 
77
+ await Redis.setClient({
78
+ host: "127.0.0.1",
79
+ port: 6379,
80
+ password: "",
81
+ database: 0,
82
+ maxRetries: 10
83
+ }, "optional-connection-name").set("redis", {hello: "world"});
84
+ // for publish and subscibe recommended using custom connection name to make sure connection matched
85
+
77
86
  const pipeline = await Redis.pipeline((pipe: RedisPipeline) => {
78
87
  pipe.set("redis-pipeline-1", "This is redis pipeline 1");
79
88
  pipe.set("redis-pipeline-2", "This is redis pipeline 2");
@@ -2,16 +2,16 @@ import type { RedisConfig, RedisPipeline, RedisSubscribe } from "../types/redis"
2
2
  export default class RedisBuilder {
3
3
  private static clients;
4
4
  private static emitter;
5
- static setClient(cfg: RedisConfig): Record<string, Function>;
5
+ static setClient(cfg: RedisConfig, name?: string): Record<string, Function>;
6
6
  static connection(name: string): Record<string, Function>;
7
7
  static connect(name?: string): Promise<Bun.RedisClient>;
8
8
  static disconnect(name?: string): Promise<void>;
9
- static get(key: Bun.RedisClient.KeyLike, connection?: string): Promise<any>;
10
- static set(key: Bun.RedisClient.KeyLike, value: any, ttl?: number, connection?: string): Promise<number | "OK">;
11
- static del(key: Bun.RedisClient.KeyLike, connection?: string): Promise<number>;
9
+ static get(key: Bun.RedisClient.KeyLike, connection?: string, disconnectAfter?: boolean): Promise<any>;
10
+ static set(key: Bun.RedisClient.KeyLike, value: any, ttl?: number, connection?: string, disconnectAfter?: boolean): Promise<number | "OK">;
11
+ static del(key: Bun.RedisClient.KeyLike, connection?: string, disconnectAfter?: boolean): Promise<number>;
12
12
  static publish(channel: string, message: any, connection?: string): Promise<number>;
13
13
  static subscribe(channel: string, listener: Bun.RedisClient.StringPubSubListener, connection?: string): Promise<RedisSubscribe>;
14
- static pipeline(fn: (pipe: RedisPipeline) => void, connection?: string): Promise<any[]>;
14
+ static pipeline(fn: (pipe: RedisPipeline) => void, connection?: string, disconnectAfter?: boolean): Promise<any[]>;
15
15
  static on(event: "connect" | "disconnect" | "error", listener: (...args: Array<any>) => void): void;
16
16
  static off(event: "connect" | "disconnect" | "error", listener: (...args: Array<any>) => void): void;
17
17
  private static get config();
@@ -1,6 +1,7 @@
1
1
  import App from "@bejibun/app";
2
2
  import Logger from "@bejibun/logger";
3
3
  import { defineValue, isEmpty, isNotEmpty } from "@bejibun/utils";
4
+ import Str from "@bejibun/utils/facades/Str";
4
5
  import { EventEmitter } from "events";
5
6
  import fs from "fs";
6
7
  import RedisConf from "../config/redis";
@@ -8,18 +9,15 @@ import RedisException from "../exceptions/RedisException";
8
9
  export default class RedisBuilder {
9
10
  static clients = {};
10
11
  static emitter = new EventEmitter();
11
- static setClient(cfg) {
12
- const connectionName = "override";
13
- this.ensureExitHooks();
14
- if (isEmpty(this.clients[connectionName])) {
15
- this.clients[connectionName] = this.createClient(connectionName, cfg);
16
- }
12
+ static setClient(cfg, name) {
13
+ const connectionName = defineValue(name, Str.random());
14
+ this.clients[connectionName] = this.createClient(connectionName, cfg);
17
15
  return {
18
- del: (key) => this.del(key, connectionName),
19
- get: (key) => this.get(key, connectionName),
20
- pipeline: (fn) => this.pipeline(fn, connectionName),
16
+ del: (key) => this.del(key, connectionName, isNotEmpty(name)),
17
+ get: (key) => this.get(key, connectionName, isNotEmpty(name)),
18
+ pipeline: (fn) => this.pipeline(fn, connectionName, isNotEmpty(name)),
21
19
  publish: (channel, message) => this.publish(channel, message, connectionName),
22
- set: (key, value, ttl) => this.set(key, value, ttl, connectionName),
20
+ set: (key, value, ttl) => this.set(key, value, ttl, connectionName, isNotEmpty(name)),
23
21
  subscribe: (channel, listener) => this.subscribe(channel, listener, connectionName),
24
22
  };
25
23
  }
@@ -57,9 +55,11 @@ export default class RedisBuilder {
57
55
  this.clients = {};
58
56
  }
59
57
  }
60
- static async get(key, connection) {
58
+ static async get(key, connection, disconnectAfter) {
61
59
  try {
62
60
  const response = await this.getClient(connection).get(key);
61
+ if (disconnectAfter)
62
+ await this.disconnect(connection);
63
63
  return this.deserialize(response);
64
64
  }
65
65
  catch (error) {
@@ -67,13 +67,15 @@ export default class RedisBuilder {
67
67
  return null;
68
68
  }
69
69
  }
70
- static async set(key, value, ttl, connection) {
70
+ static async set(key, value, ttl, connection, disconnectAfter) {
71
71
  try {
72
72
  const client = this.getClient(connection);
73
73
  const serialized = this.serialize(value);
74
74
  const data = await client.set(key, serialized);
75
75
  if (isNotEmpty(ttl))
76
76
  return await client.expire(key, ttl);
77
+ if (disconnectAfter)
78
+ await this.disconnect(connection);
77
79
  return data;
78
80
  }
79
81
  catch (error) {
@@ -81,9 +83,12 @@ export default class RedisBuilder {
81
83
  return 0;
82
84
  }
83
85
  }
84
- static async del(key, connection) {
86
+ static async del(key, connection, disconnectAfter) {
85
87
  try {
86
- return await this.getClient(connection).del(key);
88
+ const data = await this.getClient(connection).del(key);
89
+ if (disconnectAfter)
90
+ await this.disconnect(connection);
91
+ return data;
87
92
  }
88
93
  catch (error) {
89
94
  Logger.setContext("Redis").error("Failed to delete key.").trace(error);
@@ -127,7 +132,7 @@ export default class RedisBuilder {
127
132
  unsubscribe: unsubscribe
128
133
  };
129
134
  }
130
- static async pipeline(fn, connection) {
135
+ static async pipeline(fn, connection, disconnectAfter) {
131
136
  const client = this.getClient(connection);
132
137
  const ops = [];
133
138
  const pipe = {
@@ -147,6 +152,8 @@ export default class RedisBuilder {
147
152
  };
148
153
  fn(pipe);
149
154
  const results = await Promise.all(ops);
155
+ if (disconnectAfter)
156
+ await this.disconnect(connection);
150
157
  return results.map((result) => this.deserialize(result));
151
158
  }
152
159
  static on(event, listener) {
@@ -1,6 +1,6 @@
1
1
  import type { RedisConfig, RedisPipeline, RedisSubscribe } from "../types/redis";
2
2
  export default class Redis {
3
- static setClient(cfg: RedisConfig): Record<string, Function>;
3
+ static setClient(cfg: RedisConfig, name?: string): Record<string, Function>;
4
4
  static connection(name: string): Record<string, Function>;
5
5
  static connect(name?: string): Promise<Bun.RedisClient>;
6
6
  static disconnect(name?: string): Promise<void>;
package/facades/Redis.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import RedisBuilder from "../builders/RedisBuilder";
2
2
  export default class Redis {
3
- static setClient(cfg) {
4
- return RedisBuilder.setClient(cfg);
3
+ static setClient(cfg, name) {
4
+ return RedisBuilder.setClient(cfg, name);
5
5
  }
6
6
  static connection(name) {
7
7
  return RedisBuilder.connection(name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bejibun/redis",
3
- "version": "0.1.35",
3
+ "version": "0.1.36",
4
4
  "author": "Havea Crenata <havea.crenata@gmail.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -38,6 +38,6 @@
38
38
  "dependencies": {
39
39
  "@bejibun/app": "^0.1.22",
40
40
  "@bejibun/logger": "^0.1.22",
41
- "@bejibun/utils": "^0.1.24"
41
+ "@bejibun/utils": "^0.1.27"
42
42
  }
43
43
  }