@bejibun/redis 0.1.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/LICENSE +21 -0
- package/README.md +62 -0
- package/dist/Redis.d.ts +14 -0
- package/dist/Redis.js +36 -0
- package/dist/builders/RedisBuilder.d.ts +25 -0
- package/dist/builders/RedisBuilder.js +201 -0
- package/dist/config/redis.d.ts +2 -0
- package/dist/config/redis.js +13 -0
- package/dist/exceptions/RedisException.d.ts +4 -0
- package/dist/exceptions/RedisException.js +11 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Havea Crenata
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
# Redis for Bejibun
|
|
12
|
+
Redis package with Built-in Bun for Bejibun Framework.
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### Installation
|
|
17
|
+
Install the package.
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# Using Bun
|
|
21
|
+
bun add @bejibun/redis
|
|
22
|
+
|
|
23
|
+
# Using Bejibun
|
|
24
|
+
bun ace install @bejibun/redis
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### How to Use
|
|
28
|
+
How to use tha package.
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import Redis from "@bejibun/Redis";
|
|
32
|
+
import {BunRequest} from "bun";
|
|
33
|
+
import BaseController from "@/app/controllers/BaseController";
|
|
34
|
+
|
|
35
|
+
export default class TestController extends BaseController {
|
|
36
|
+
public async redis(request: BunRequest): Promise<Response> {
|
|
37
|
+
await Redis.set("redis", {hello: "world"});
|
|
38
|
+
const redis = await Redis.get("redis");
|
|
39
|
+
|
|
40
|
+
const pipeline = await Redis.pipeline((pipe: RedisPipeline) => {
|
|
41
|
+
pipe.set("redis-pipeline-1", "This is redis pipeline 1");
|
|
42
|
+
pipe.set("redis-pipeline-2", "This is redis pipeline 2");
|
|
43
|
+
|
|
44
|
+
pipe.get("redis-pipeline-1");
|
|
45
|
+
pipe.get("redis-pipeline-2");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const subscriber = await Redis.subscribe("redis-subscribe", (message: string, channel: string) => {
|
|
49
|
+
console.log(`[${channel}]: ${message}`);
|
|
50
|
+
});
|
|
51
|
+
await Redis.publish("redis-subscribe", "Hai redis subscriber!");
|
|
52
|
+
setTimeout(async () => {
|
|
53
|
+
await subscriber.unsubscribe();
|
|
54
|
+
}, 500);
|
|
55
|
+
|
|
56
|
+
return super.response.setData({redis, pipeline}).send();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Contributors
|
|
62
|
+
- [Havea Crenata](mailto:havea.crenata@gmail.com)
|
package/dist/Redis.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RedisClient } from "bun";
|
|
2
|
+
export default class Redis {
|
|
3
|
+
static connection(name: string): Record<string, Function>;
|
|
4
|
+
static connect(name?: string): Promise<RedisClient>;
|
|
5
|
+
static disconnect(name?: string): Promise<void>;
|
|
6
|
+
static get(key: RedisClient.KeyLike, connection?: string): Promise<any>;
|
|
7
|
+
static set(key: RedisClient.KeyLike, value: any, ttl?: number, connection?: string): Promise<number | "OK">;
|
|
8
|
+
static del(key: RedisClient.KeyLike, connection?: string): Promise<number>;
|
|
9
|
+
static publish(channel: string, message: any, connection?: string): Promise<number>;
|
|
10
|
+
static subscribe(channel: string, listener: RedisClient.StringPubSubListener, connection?: string): Promise<RedisSubscribe>;
|
|
11
|
+
static pipeline(fn: (pipe: RedisPipeline) => void, connection?: string): Promise<any[]>;
|
|
12
|
+
static on(event: "connect" | "disconnect" | "error", listener: (...args: Array<any>) => void): void;
|
|
13
|
+
static off(event: "connect" | "disconnect" | "error", listener: (...args: Array<any>) => void): void;
|
|
14
|
+
}
|
package/dist/Redis.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import RedisBuilder from "./builders/RedisBuilder";
|
|
2
|
+
export default class Redis {
|
|
3
|
+
static connection(name) {
|
|
4
|
+
return RedisBuilder.connection(name);
|
|
5
|
+
}
|
|
6
|
+
static async connect(name) {
|
|
7
|
+
return RedisBuilder.connect(name);
|
|
8
|
+
}
|
|
9
|
+
static async disconnect(name) {
|
|
10
|
+
return RedisBuilder.disconnect(name);
|
|
11
|
+
}
|
|
12
|
+
static async get(key, connection) {
|
|
13
|
+
return RedisBuilder.get(key, connection);
|
|
14
|
+
}
|
|
15
|
+
static async set(key, value, ttl, connection) {
|
|
16
|
+
return RedisBuilder.set(key, value, ttl, connection);
|
|
17
|
+
}
|
|
18
|
+
static async del(key, connection) {
|
|
19
|
+
return RedisBuilder.del(key, connection);
|
|
20
|
+
}
|
|
21
|
+
static async publish(channel, message, connection) {
|
|
22
|
+
return RedisBuilder.publish(channel, message, connection);
|
|
23
|
+
}
|
|
24
|
+
static async subscribe(channel, listener, connection) {
|
|
25
|
+
return RedisBuilder.subscribe(channel, listener, connection);
|
|
26
|
+
}
|
|
27
|
+
static async pipeline(fn, connection) {
|
|
28
|
+
return RedisBuilder.pipeline(fn, connection);
|
|
29
|
+
}
|
|
30
|
+
static on(event, listener) {
|
|
31
|
+
return RedisBuilder.on(event, listener);
|
|
32
|
+
}
|
|
33
|
+
static off(event, listener) {
|
|
34
|
+
return RedisBuilder.off(event, listener);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { RedisClient } from "bun";
|
|
2
|
+
export default class RedisBuilder {
|
|
3
|
+
private static clients;
|
|
4
|
+
private static emitter;
|
|
5
|
+
static connection(name: string): Record<string, Function>;
|
|
6
|
+
static connect(name?: string): Promise<RedisClient>;
|
|
7
|
+
static disconnect(name?: string): Promise<void>;
|
|
8
|
+
static get(key: RedisClient.KeyLike, connection?: string): Promise<any>;
|
|
9
|
+
static set(key: RedisClient.KeyLike, value: any, ttl?: number, connection?: string): Promise<number | "OK">;
|
|
10
|
+
static del(key: RedisClient.KeyLike, connection?: string): Promise<number>;
|
|
11
|
+
static publish(channel: string, message: any, connection?: string): Promise<number>;
|
|
12
|
+
static subscribe(channel: string, listener: RedisClient.StringPubSubListener, connection?: string): Promise<RedisSubscribe>;
|
|
13
|
+
static pipeline(fn: (pipe: RedisPipeline) => void, connection?: string): Promise<any[]>;
|
|
14
|
+
static on(event: "connect" | "disconnect" | "error", listener: (...args: Array<any>) => void): void;
|
|
15
|
+
static off(event: "connect" | "disconnect" | "error", listener: (...args: Array<any>) => void): void;
|
|
16
|
+
private static buildUrl;
|
|
17
|
+
private static createClient;
|
|
18
|
+
private static getOptions;
|
|
19
|
+
private static getConfig;
|
|
20
|
+
private static getClient;
|
|
21
|
+
private static serialize;
|
|
22
|
+
private static deserialize;
|
|
23
|
+
private static ensureExitHooks;
|
|
24
|
+
private static setupExitHooks;
|
|
25
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { defineValue, isEmpty, isNotEmpty } from "@bejibun/core";
|
|
3
|
+
import { RedisClient } from "bun";
|
|
4
|
+
import { EventEmitter } from "events";
|
|
5
|
+
import config from "../config/redis";
|
|
6
|
+
class RedisBuilder {
|
|
7
|
+
static connection(name) {
|
|
8
|
+
return {
|
|
9
|
+
del: (key) => this.del(key, name),
|
|
10
|
+
get: (key) => this.get(key, name),
|
|
11
|
+
pipeline: (fn) => this.pipeline(fn, name),
|
|
12
|
+
publish: (channel, message) => this.publish(channel, message, name),
|
|
13
|
+
set: (key, value, ttl) => this.set(key, value, ttl, name),
|
|
14
|
+
subscribe: (channel, listener) => this.subscribe(channel, listener, name),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
static async connect(name) {
|
|
18
|
+
const client = this.getClient(name);
|
|
19
|
+
await client.connect();
|
|
20
|
+
console.log(`[Redis]: Connected manually to "${defineValue(name, "default")}" connection.`);
|
|
21
|
+
this.emitter.emit("connect", defineValue(name, "default"));
|
|
22
|
+
return client;
|
|
23
|
+
}
|
|
24
|
+
static async disconnect(name) {
|
|
25
|
+
if (isNotEmpty(name)) {
|
|
26
|
+
const client = this.clients[name];
|
|
27
|
+
if (isNotEmpty(client)) {
|
|
28
|
+
await client.close();
|
|
29
|
+
delete this.clients[name];
|
|
30
|
+
console.log(`[Redis]: Disconnected manually from "${name}" connection.`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
for (const [connectionName, client] of Object.entries(this.clients)) {
|
|
35
|
+
await client.close();
|
|
36
|
+
console.log(`[Redis]: Disconnected manually from "${connectionName}" connection.`);
|
|
37
|
+
}
|
|
38
|
+
this.clients = {};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
static async get(key, connection) {
|
|
42
|
+
const response = await this.getClient(connection).get(key);
|
|
43
|
+
return this.deserialize(response);
|
|
44
|
+
}
|
|
45
|
+
static async set(key, value, ttl, connection) {
|
|
46
|
+
const client = this.getClient(connection);
|
|
47
|
+
const serialized = this.serialize(value);
|
|
48
|
+
if (ttl)
|
|
49
|
+
return await client.expire(key, ttl);
|
|
50
|
+
return await client.set(key, serialized);
|
|
51
|
+
}
|
|
52
|
+
static async del(key, connection) {
|
|
53
|
+
return await this.getClient(connection).del(key);
|
|
54
|
+
}
|
|
55
|
+
static async publish(channel, message, connection) {
|
|
56
|
+
const serialized = this.serialize(message);
|
|
57
|
+
return await this.getClient(connection).publish(channel, serialized);
|
|
58
|
+
}
|
|
59
|
+
static async subscribe(channel, listener, connection) {
|
|
60
|
+
const cfg = this.getConfig(connection);
|
|
61
|
+
const client = this.createClient(config.default, cfg);
|
|
62
|
+
this.clients[channel] = client;
|
|
63
|
+
await client.subscribe(channel, (message, channel) => listener(this.deserialize(message), channel));
|
|
64
|
+
console.log(`[Redis]: Subscribed to "${channel}" channel.`);
|
|
65
|
+
const unsubscribe = async () => {
|
|
66
|
+
await client.unsubscribe(channel);
|
|
67
|
+
console.log(`[Redis]: Unsubscribed from "${channel}" channel.`);
|
|
68
|
+
await client.close();
|
|
69
|
+
return true;
|
|
70
|
+
};
|
|
71
|
+
return {
|
|
72
|
+
client,
|
|
73
|
+
unsubscribe: unsubscribe
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
static async pipeline(fn, connection) {
|
|
77
|
+
const client = this.getClient(connection);
|
|
78
|
+
const ops = [];
|
|
79
|
+
const pipe = {
|
|
80
|
+
del: (key) => {
|
|
81
|
+
ops.push(client.del(key));
|
|
82
|
+
},
|
|
83
|
+
get: (key) => {
|
|
84
|
+
ops.push(client.get(key));
|
|
85
|
+
},
|
|
86
|
+
set: (key, value, ttl) => {
|
|
87
|
+
const serialized = this.serialize(value);
|
|
88
|
+
if (isNotEmpty(ttl))
|
|
89
|
+
ops.push(client.expire(key, ttl));
|
|
90
|
+
ops.push(client.set(key, serialized));
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
fn(pipe);
|
|
94
|
+
const results = await Promise.all(ops);
|
|
95
|
+
return results.map((result) => this.deserialize(result));
|
|
96
|
+
}
|
|
97
|
+
static on(event, listener) {
|
|
98
|
+
this.emitter.on(event, listener);
|
|
99
|
+
}
|
|
100
|
+
static off(event, listener) {
|
|
101
|
+
this.emitter.off(event, listener);
|
|
102
|
+
}
|
|
103
|
+
static buildUrl(cfg) {
|
|
104
|
+
const url = new URL(`redis://${cfg.host}:${cfg.port}`);
|
|
105
|
+
if (isNotEmpty(cfg.password))
|
|
106
|
+
url.password = cfg.password;
|
|
107
|
+
if (isNotEmpty(cfg.database))
|
|
108
|
+
url.pathname = `/${cfg.database}`;
|
|
109
|
+
return url.toString();
|
|
110
|
+
}
|
|
111
|
+
static createClient(name, cfg) {
|
|
112
|
+
const url = this.buildUrl(cfg);
|
|
113
|
+
const client = new RedisClient(url, this.getOptions(cfg));
|
|
114
|
+
client.onconnect = () => {
|
|
115
|
+
console.log(`[Redis]: Connected to "${name}" connection.`);
|
|
116
|
+
this.emitter.emit("connect", name);
|
|
117
|
+
};
|
|
118
|
+
client.onclose = (error) => {
|
|
119
|
+
console.warn(`[Redis]: Disconnected from "${name}" connection.`, error.message);
|
|
120
|
+
this.emitter.emit("disconnect", name, error);
|
|
121
|
+
};
|
|
122
|
+
return client;
|
|
123
|
+
}
|
|
124
|
+
static getOptions(cfg) {
|
|
125
|
+
return {
|
|
126
|
+
autoReconnect: true,
|
|
127
|
+
maxRetries: cfg.maxRetries
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
static getConfig(name) {
|
|
131
|
+
const connectionName = defineValue(name, config.default);
|
|
132
|
+
const connection = config.connections[connectionName];
|
|
133
|
+
if (isEmpty(connection))
|
|
134
|
+
throw new RedisException(`[Redis]: Connection "${connectionName}" not found.`);
|
|
135
|
+
return connection;
|
|
136
|
+
}
|
|
137
|
+
static getClient(name) {
|
|
138
|
+
const connectionName = defineValue(name, config.default);
|
|
139
|
+
this.ensureExitHooks();
|
|
140
|
+
if (isEmpty(this.clients[connectionName])) {
|
|
141
|
+
const cfg = this.getConfig(connectionName);
|
|
142
|
+
this.clients[connectionName] = this.createClient(connectionName, cfg);
|
|
143
|
+
}
|
|
144
|
+
return this.clients[connectionName];
|
|
145
|
+
}
|
|
146
|
+
static serialize(value) {
|
|
147
|
+
if (isEmpty(value))
|
|
148
|
+
return "";
|
|
149
|
+
if (typeof value === "object")
|
|
150
|
+
return JSON.stringify(value);
|
|
151
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
152
|
+
return String(value);
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
static deserialize(value) {
|
|
156
|
+
if (isEmpty(value))
|
|
157
|
+
return null;
|
|
158
|
+
try {
|
|
159
|
+
return JSON.parse(value);
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
return value;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
static ensureExitHooks() {
|
|
166
|
+
this.setupExitHooks();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
_a = RedisBuilder;
|
|
170
|
+
RedisBuilder.clients = {};
|
|
171
|
+
RedisBuilder.emitter = new EventEmitter();
|
|
172
|
+
RedisBuilder.setupExitHooks = (() => {
|
|
173
|
+
let initialized = false;
|
|
174
|
+
return () => {
|
|
175
|
+
if (initialized)
|
|
176
|
+
return;
|
|
177
|
+
initialized = true;
|
|
178
|
+
const handleExit = async (signal) => {
|
|
179
|
+
try {
|
|
180
|
+
await _a.disconnect();
|
|
181
|
+
console.log(`[Redis]: Disconnected on "${defineValue(signal, "exit")}".`);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
console.error("[Redis]: Error during disconnect.", error.message);
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
process.exit(0);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
process.on("exit", async () => {
|
|
191
|
+
await handleExit();
|
|
192
|
+
});
|
|
193
|
+
process.on("SIGINT", async () => {
|
|
194
|
+
await handleExit("SIGINT");
|
|
195
|
+
});
|
|
196
|
+
process.on("SIGTERM", async () => {
|
|
197
|
+
await handleExit("SIGTERM");
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
})();
|
|
201
|
+
export default RedisBuilder;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const config = {
|
|
2
|
+
default: process.env.REDIS_CONNECTION,
|
|
3
|
+
connections: {
|
|
4
|
+
local: {
|
|
5
|
+
host: process.env.REDIS_HOST,
|
|
6
|
+
port: process.env.REDIS_PORT,
|
|
7
|
+
password: process.env.REDIS_PASSWORD,
|
|
8
|
+
database: process.env.REDIS_DATABASE,
|
|
9
|
+
maxRetries: Number(process.env.REDIS_MAX_RETRIES)
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
export default config;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineValue } from "@bejibun/core";
|
|
2
|
+
export default class RedisException extends Error {
|
|
3
|
+
constructor(message, code) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "RedisException";
|
|
6
|
+
this.code = defineValue(code, 503);
|
|
7
|
+
if (Error.captureStackTrace) {
|
|
8
|
+
Error.captureStackTrace(this, RedisException);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bejibun/redis",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Redis for Bejibun Framework",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bun",
|
|
7
|
+
"bun framework",
|
|
8
|
+
"redis",
|
|
9
|
+
"bejibun",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/crenata/bejibun-redis#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/crenata/bejibun-redis/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/crenata/bejibun-redis.git"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Havea Crenata <havea.crenata@gmail.com>",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts"
|
|
30
|
+
},
|
|
31
|
+
"./*": "./dist/*"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"clean": "rm -rf dist",
|
|
38
|
+
"build": "bun run clean && tsc -p tsconfig.json && tsc-alias -p tsconfig.json"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@bejibun/core": "^0.1.18"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/bun": "latest",
|
|
45
|
+
"tsc-alias": "^1.8.16",
|
|
46
|
+
"typescript": "^5.9.3"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"typescript": "^5"
|
|
50
|
+
}
|
|
51
|
+
}
|