@bejibun/redis 0.1.34 → 0.1.35

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,28 @@ All notable changes to this project will be documented in this file.
3
3
 
4
4
  ---
5
5
 
6
+ ## [v0.1.35](https://github.com/crenata/bejibun-redis/compare/v0.1.34...v0.1.35) - 2025-12-07
7
+
8
+ ### 🩹 Fixes
9
+
10
+ ### 📖 Changes
11
+ What's New :
12
+ - Adding `.setClient(cfg: RedisConfig)` to override connection.
13
+
14
+ By default, [@bejibun/redis](https://github.com/crenata/bejibun-redis) use connection from `config/redis.ts`.
15
+
16
+ Now, you can override it by using `setClient()`.
17
+
18
+ #### Upgrade [@bejibun/utils](https://github.com/crenata/bejibun-utils) to [v0.1.24](https://github.com/crenata/bejibun-utils/releases/tag/v0.1.24)
19
+ - Fix empty validation for class
20
+
21
+ ### ❤️Contributors
22
+ - Havea Crenata ([@crenata](https://github.com/crenata))
23
+
24
+ **Full Changelog**: https://github.com/crenata/bejibun-redis/blob/master/CHANGELOG.md
25
+
26
+ ---
27
+
6
28
  ## [v0.1.34](https://github.com/crenata/bejibun-redis/compare/v0.1.33...v0.1.34) - 2025-11-23
7
29
 
8
30
  ### 🩹 Fixes
@@ -1,7 +1,8 @@
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): 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>;
@@ -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
  }
@@ -8,6 +8,21 @@ import RedisException from "../exceptions/RedisException";
8
8
  export default class RedisBuilder {
9
9
  static clients = {};
10
10
  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
+ }
17
+ return {
18
+ del: (key) => this.del(key, connectionName),
19
+ get: (key) => this.get(key, connectionName),
20
+ pipeline: (fn) => this.pipeline(fn, connectionName),
21
+ publish: (channel, message) => this.publish(channel, message, connectionName),
22
+ set: (key, value, ttl) => this.set(key, value, ttl, connectionName),
23
+ subscribe: (channel, listener) => this.subscribe(channel, listener, connectionName),
24
+ };
25
+ }
11
26
  static connection(name) {
12
27
  return {
13
28
  del: (key) => this.del(key, name),
@@ -43,35 +58,69 @@ export default class RedisBuilder {
43
58
  }
44
59
  }
45
60
  static async get(key, connection) {
46
- const response = await this.getClient(connection).get(key);
47
- return this.deserialize(response);
61
+ try {
62
+ const response = await this.getClient(connection).get(key);
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
70
  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;
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
+ return data;
78
+ }
79
+ catch (error) {
80
+ Logger.setContext("Redis").error("Failed to set value.").trace(error);
81
+ return 0;
82
+ }
56
83
  }
57
84
  static async del(key, connection) {
58
- return await this.getClient(connection).del(key);
85
+ try {
86
+ return await this.getClient(connection).del(key);
87
+ }
88
+ catch (error) {
89
+ Logger.setContext("Redis").error("Failed to delete key.").trace(error);
90
+ return 0;
91
+ }
59
92
  }
60
93
  static async publish(channel, message, connection) {
61
- const serialized = this.serialize(message);
62
- return await this.getClient(connection).publish(channel, serialized);
94
+ try {
95
+ const serialized = this.serialize(message);
96
+ return await this.getClient(connection).publish(channel, serialized);
97
+ }
98
+ catch (error) {
99
+ Logger.setContext("Redis").error("Failed to publish channel.").trace(error);
100
+ return 0;
101
+ }
63
102
  }
64
103
  static async subscribe(channel, listener, connection) {
65
- const cfg = this.getConfig(connection);
66
- const client = this.createClient(this.config.default, cfg);
104
+ const client = this.getClient(connection);
67
105
  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.`);
106
+ try {
107
+ await client.subscribe(channel, (message, channel) => listener(this.deserialize(message), channel));
108
+ Logger.setContext("Redis").info(`Subscribed to "${channel}" channel.`);
109
+ }
110
+ catch (error) {
111
+ Logger.setContext("Redis").error(`Failed to subscribe "${channel}" channel.`).trace(error);
112
+ }
70
113
  const unsubscribe = async () => {
71
- await client.unsubscribe(channel);
72
- await client.close();
73
- Logger.setContext("Redis").warn(`Unsubscribed from "${channel}" channel.`);
74
- return true;
114
+ try {
115
+ await client.unsubscribe(channel);
116
+ await client.close();
117
+ Logger.setContext("Redis").warn(`Unsubscribed from "${channel}" channel.`);
118
+ return true;
119
+ }
120
+ catch (error) {
121
+ Logger.setContext("Redis").error(`Failed to unsubscribe from "${channel}" channel.`).trace(error);
122
+ return false;
123
+ }
75
124
  };
76
125
  return {
77
126
  client,
@@ -177,10 +226,7 @@ export default class RedisBuilder {
177
226
  return value;
178
227
  }
179
228
  }
180
- static ensureExitHooks() {
181
- this.setupExitHooks();
182
- }
183
- static setupExitHooks = (() => {
229
+ static ensureExitHooks = (() => {
184
230
  let initialized = false;
185
231
  return () => {
186
232
  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): 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) {
4
+ return RedisBuilder.setClient(cfg);
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.35",
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.24"
42
42
  }
43
43
  }