@bejibun/redis 0.1.33 → 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,42 @@ 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
+
28
+ ## [v0.1.34](https://github.com/crenata/bejibun-redis/compare/v0.1.33...v0.1.34) - 2025-11-23
29
+
30
+ ### 🩹 Fixes
31
+ - Fix redis ttl
32
+
33
+ ### 📖 Changes
34
+
35
+ ### ❤️Contributors
36
+ - Havea Crenata ([@crenata](https://github.com/crenata))
37
+
38
+ **Full Changelog**: https://github.com/crenata/bejibun-redis/blob/master/CHANGELOG.md
39
+
40
+ ---
41
+
6
42
  ## [v0.1.33](https://github.com/crenata/bejibun-redis/compare/v0.1.30...v0.1.33) - 2025-11-09
7
43
 
8
44
  ### 🩹 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,34 +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
- if (isNotEmpty(ttl))
53
- return await client.expire(key, ttl);
54
- return await client.set(key, serialized);
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
+ }
55
83
  }
56
84
  static async del(key, connection) {
57
- 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
+ }
58
92
  }
59
93
  static async publish(channel, message, connection) {
60
- const serialized = this.serialize(message);
61
- 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
+ }
62
102
  }
63
103
  static async subscribe(channel, listener, connection) {
64
- const cfg = this.getConfig(connection);
65
- const client = this.createClient(this.config.default, cfg);
104
+ const client = this.getClient(connection);
66
105
  this.clients[channel] = client;
67
- await client.subscribe(channel, (message, channel) => listener(this.deserialize(message), channel));
68
- 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
+ }
69
113
  const unsubscribe = async () => {
70
- await client.unsubscribe(channel);
71
- await client.close();
72
- Logger.setContext("Redis").warn(`Unsubscribed from "${channel}" channel.`);
73
- 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
+ }
74
124
  };
75
125
  return {
76
126
  client,
@@ -89,9 +139,10 @@ export default class RedisBuilder {
89
139
  },
90
140
  set: (key, value, ttl) => {
91
141
  const serialized = this.serialize(value);
142
+ const data = client.set(key, serialized);
92
143
  if (isNotEmpty(ttl))
93
144
  ops.push(client.expire(key, ttl));
94
- ops.push(client.set(key, serialized));
145
+ ops.push(data);
95
146
  }
96
147
  };
97
148
  fn(pipe);
@@ -175,10 +226,7 @@ export default class RedisBuilder {
175
226
  return value;
176
227
  }
177
228
  }
178
- static ensureExitHooks() {
179
- this.setupExitHooks();
180
- }
181
- static setupExitHooks = (() => {
229
+ static ensureExitHooks = (() => {
182
230
  let initialized = false;
183
231
  return () => {
184
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.33",
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
  }