@bejibun/redis 0.1.34 → 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,44 @@ 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
+
22
+ ## [v0.1.35](https://github.com/crenata/bejibun-redis/compare/v0.1.34...v0.1.35) - 2025-12-07
23
+
24
+ ### 🩹 Fixes
25
+
26
+ ### 📖 Changes
27
+ What's New :
28
+ - Adding `.setClient(cfg: RedisConfig)` to override connection.
29
+
30
+ By default, [@bejibun/redis](https://github.com/crenata/bejibun-redis) use connection from `config/redis.ts`.
31
+
32
+ Now, you can override it by using `setClient()`.
33
+
34
+ #### Upgrade [@bejibun/utils](https://github.com/crenata/bejibun-utils) to [v0.1.24](https://github.com/crenata/bejibun-utils/releases/tag/v0.1.24)
35
+ - Fix empty validation for class
36
+
37
+ ### ❤️Contributors
38
+ - Havea Crenata ([@crenata](https://github.com/crenata))
39
+
40
+ **Full Changelog**: https://github.com/crenata/bejibun-redis/blob/master/CHANGELOG.md
41
+
42
+ ---
43
+
6
44
  ## [v0.1.34](https://github.com/crenata/bejibun-redis/compare/v0.1.33...v0.1.34) - 2025-11-23
7
45
 
8
46
  ### 🩹 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");
@@ -1,16 +1,17 @@
1
- import type { RedisPipeline, RedisSubscribe } from "../types/redis";
1
+ 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, name?: string): Record<string, Function>;
5
6
  static connection(name: string): Record<string, Function>;
6
7
  static connect(name?: string): Promise<Bun.RedisClient>;
7
8
  static disconnect(name?: string): Promise<void>;
8
- static get(key: Bun.RedisClient.KeyLike, connection?: string): Promise<any>;
9
- static set(key: Bun.RedisClient.KeyLike, value: any, ttl?: number, connection?: string): Promise<number | "OK">;
10
- 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>;
11
12
  static publish(channel: string, message: any, connection?: string): Promise<number>;
12
13
  static subscribe(channel: string, listener: Bun.RedisClient.StringPubSubListener, connection?: string): Promise<RedisSubscribe>;
13
- static pipeline(fn: (pipe: RedisPipeline) => void, connection?: string): Promise<any[]>;
14
+ static pipeline(fn: (pipe: RedisPipeline) => void, connection?: string, disconnectAfter?: boolean): Promise<any[]>;
14
15
  static on(event: "connect" | "disconnect" | "error", listener: (...args: Array<any>) => void): void;
15
16
  static off(event: "connect" | "disconnect" | "error", listener: (...args: Array<any>) => void): void;
16
17
  private static get config();
@@ -22,5 +23,4 @@ export default class RedisBuilder {
22
23
  private static serialize;
23
24
  private static deserialize;
24
25
  private static ensureExitHooks;
25
- private static setupExitHooks;
26
26
  }
@@ -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,6 +9,18 @@ import RedisException from "../exceptions/RedisException";
8
9
  export default class RedisBuilder {
9
10
  static clients = {};
10
11
  static emitter = new EventEmitter();
12
+ static setClient(cfg, name) {
13
+ const connectionName = defineValue(name, Str.random());
14
+ this.clients[connectionName] = this.createClient(connectionName, cfg);
15
+ return {
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)),
19
+ publish: (channel, message) => this.publish(channel, message, connectionName),
20
+ set: (key, value, ttl) => this.set(key, value, ttl, connectionName, isNotEmpty(name)),
21
+ subscribe: (channel, listener) => this.subscribe(channel, listener, connectionName),
22
+ };
23
+ }
11
24
  static connection(name) {
12
25
  return {
13
26
  del: (key) => this.del(key, name),
@@ -42,43 +55,84 @@ export default class RedisBuilder {
42
55
  this.clients = {};
43
56
  }
44
57
  }
45
- static async get(key, connection) {
46
- const response = await this.getClient(connection).get(key);
47
- return this.deserialize(response);
58
+ static async get(key, connection, disconnectAfter) {
59
+ try {
60
+ const response = await this.getClient(connection).get(key);
61
+ if (disconnectAfter)
62
+ await this.disconnect(connection);
63
+ return this.deserialize(response);
64
+ }
65
+ catch (error) {
66
+ Logger.setContext("Redis").error("Failed to get value.").trace(error);
67
+ return null;
68
+ }
48
69
  }
49
- static async set(key, value, ttl, connection) {
50
- const client = this.getClient(connection);
51
- const serialized = this.serialize(value);
52
- const data = await client.set(key, serialized);
53
- if (isNotEmpty(ttl))
54
- return await client.expire(key, ttl);
55
- return data;
70
+ static async set(key, value, ttl, connection, disconnectAfter) {
71
+ try {
72
+ const client = this.getClient(connection);
73
+ const serialized = this.serialize(value);
74
+ const data = await client.set(key, serialized);
75
+ if (isNotEmpty(ttl))
76
+ return await client.expire(key, ttl);
77
+ if (disconnectAfter)
78
+ await this.disconnect(connection);
79
+ return data;
80
+ }
81
+ catch (error) {
82
+ Logger.setContext("Redis").error("Failed to set value.").trace(error);
83
+ return 0;
84
+ }
56
85
  }
57
- static async del(key, connection) {
58
- return await this.getClient(connection).del(key);
86
+ static async del(key, connection, disconnectAfter) {
87
+ try {
88
+ const data = await this.getClient(connection).del(key);
89
+ if (disconnectAfter)
90
+ await this.disconnect(connection);
91
+ return data;
92
+ }
93
+ catch (error) {
94
+ Logger.setContext("Redis").error("Failed to delete key.").trace(error);
95
+ return 0;
96
+ }
59
97
  }
60
98
  static async publish(channel, message, connection) {
61
- const serialized = this.serialize(message);
62
- return await this.getClient(connection).publish(channel, serialized);
99
+ try {
100
+ const serialized = this.serialize(message);
101
+ return await this.getClient(connection).publish(channel, serialized);
102
+ }
103
+ catch (error) {
104
+ Logger.setContext("Redis").error("Failed to publish channel.").trace(error);
105
+ return 0;
106
+ }
63
107
  }
64
108
  static async subscribe(channel, listener, connection) {
65
- const cfg = this.getConfig(connection);
66
- const client = this.createClient(this.config.default, cfg);
109
+ const client = this.getClient(connection);
67
110
  this.clients[channel] = client;
68
- await client.subscribe(channel, (message, channel) => listener(this.deserialize(message), channel));
69
- Logger.setContext("Redis").info(`Subscribed to "${channel}" channel.`);
111
+ try {
112
+ await client.subscribe(channel, (message, channel) => listener(this.deserialize(message), channel));
113
+ Logger.setContext("Redis").info(`Subscribed to "${channel}" channel.`);
114
+ }
115
+ catch (error) {
116
+ Logger.setContext("Redis").error(`Failed to subscribe "${channel}" channel.`).trace(error);
117
+ }
70
118
  const unsubscribe = async () => {
71
- await client.unsubscribe(channel);
72
- await client.close();
73
- Logger.setContext("Redis").warn(`Unsubscribed from "${channel}" channel.`);
74
- return true;
119
+ try {
120
+ await client.unsubscribe(channel);
121
+ await client.close();
122
+ Logger.setContext("Redis").warn(`Unsubscribed from "${channel}" channel.`);
123
+ return true;
124
+ }
125
+ catch (error) {
126
+ Logger.setContext("Redis").error(`Failed to unsubscribe from "${channel}" channel.`).trace(error);
127
+ return false;
128
+ }
75
129
  };
76
130
  return {
77
131
  client,
78
132
  unsubscribe: unsubscribe
79
133
  };
80
134
  }
81
- static async pipeline(fn, connection) {
135
+ static async pipeline(fn, connection, disconnectAfter) {
82
136
  const client = this.getClient(connection);
83
137
  const ops = [];
84
138
  const pipe = {
@@ -98,6 +152,8 @@ export default class RedisBuilder {
98
152
  };
99
153
  fn(pipe);
100
154
  const results = await Promise.all(ops);
155
+ if (disconnectAfter)
156
+ await this.disconnect(connection);
101
157
  return results.map((result) => this.deserialize(result));
102
158
  }
103
159
  static on(event, listener) {
@@ -177,10 +233,7 @@ export default class RedisBuilder {
177
233
  return value;
178
234
  }
179
235
  }
180
- static ensureExitHooks() {
181
- this.setupExitHooks();
182
- }
183
- static setupExitHooks = (() => {
236
+ static ensureExitHooks = (() => {
184
237
  let initialized = false;
185
238
  return () => {
186
239
  if (initialized)
@@ -1,5 +1,6 @@
1
- import type { RedisPipeline, RedisSubscribe } from "../types/redis";
1
+ import type { RedisConfig, RedisPipeline, RedisSubscribe } from "../types/redis";
2
2
  export default class Redis {
3
+ static setClient(cfg: RedisConfig, name?: string): Record<string, Function>;
3
4
  static connection(name: string): Record<string, Function>;
4
5
  static connect(name?: string): Promise<Bun.RedisClient>;
5
6
  static disconnect(name?: string): Promise<void>;
package/facades/Redis.js CHANGED
@@ -1,5 +1,8 @@
1
1
  import RedisBuilder from "../builders/RedisBuilder";
2
2
  export default class Redis {
3
+ static setClient(cfg, name) {
4
+ return RedisBuilder.setClient(cfg, name);
5
+ }
3
6
  static connection(name) {
4
7
  return RedisBuilder.connection(name);
5
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bejibun/redis",
3
- "version": "0.1.34",
3
+ "version": "0.1.36",
4
4
  "author": "Havea Crenata <havea.crenata@gmail.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,8 +36,8 @@
36
36
  "type": "module",
37
37
  "types": "index.d.ts",
38
38
  "dependencies": {
39
- "@bejibun/app": "^0.1.19",
40
- "@bejibun/logger": "^0.1.18",
41
- "@bejibun/utils": "^0.1.14"
39
+ "@bejibun/app": "^0.1.22",
40
+ "@bejibun/logger": "^0.1.22",
41
+ "@bejibun/utils": "^0.1.27"
42
42
  }
43
43
  }