@declaro/redis 2.0.0-y.0 → 2.1.1
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/dist/browser/index.js +4 -0
- package/dist/browser/index.js.map +20 -0
- package/dist/node/index.cjs +493 -0
- package/dist/node/index.cjs.map +20 -0
- package/dist/node/index.js +458 -0
- package/dist/node/index.js.map +20 -0
- package/dist/{index.d.ts → ts/index.d.ts} +1 -0
- package/dist/ts/index.d.ts.map +1 -0
- package/dist/ts/placeholder.test.d.ts +2 -0
- package/dist/ts/placeholder.test.d.ts.map +1 -0
- package/dist/{redis → ts/redis}/index.d.ts +1 -0
- package/dist/ts/redis/index.d.ts.map +1 -0
- package/dist/{redis → ts/redis}/publish.d.ts +2 -1
- package/dist/ts/redis/publish.d.ts.map +1 -0
- package/dist/{redis → ts/redis}/queue.d.ts +1 -0
- package/dist/ts/redis/queue.d.ts.map +1 -0
- package/dist/{redis → ts/redis}/redis-event-adapter.d.ts +1 -0
- package/dist/ts/redis/redis-event-adapter.d.ts.map +1 -0
- package/dist/ts/redis/redis-event-adapter.test.d.ts +2 -0
- package/dist/ts/redis/redis-event-adapter.test.d.ts.map +1 -0
- package/dist/{redis → ts/redis}/redis-middleware.d.ts +7 -1
- package/dist/ts/redis/redis-middleware.d.ts.map +1 -0
- package/dist/{redis → ts/redis}/repository.d.ts +3 -2
- package/dist/ts/redis/repository.d.ts.map +1 -0
- package/dist/{redis → ts/redis}/storage.d.ts +2 -1
- package/dist/ts/redis/storage.d.ts.map +1 -0
- package/dist/{redis → ts/redis}/utils.d.ts +3 -2
- package/dist/ts/redis/utils.d.ts.map +1 -0
- package/dist/{settings → ts/settings}/index.d.ts +1 -0
- package/dist/ts/settings/index.d.ts.map +1 -0
- package/dist/{settings → ts/settings}/setting.d.ts +4 -3
- package/dist/ts/settings/setting.d.ts.map +1 -0
- package/dist/{settings → ts/settings}/site.d.ts +1 -0
- package/dist/ts/settings/site.d.ts.map +1 -0
- package/dist/{test-utils → ts/test-utils}/index.d.ts +1 -0
- package/dist/ts/test-utils/index.d.ts.map +1 -0
- package/package.json +28 -11
- package/src/redis/queue.ts +4 -3
- package/src/redis/redis-middleware.ts +11 -5
- package/src/redis/repository.ts +9 -3
- package/src/redis/utils.ts +5 -2
- package/src/settings/setting.ts +1 -1
- package/dist/pkg.cjs +0 -29
- package/dist/pkg.mjs +0 -55679
- package/tsconfig.json +0 -8
- package/vite.config.ts +0 -31
- /package/{LICENSE → LICENSE.md} +0 -0
|
@@ -18,3 +18,4 @@ export declare function pushMessage<T = string>(context: Context, channel: strin
|
|
|
18
18
|
* @param channel The channel to receive messages on
|
|
19
19
|
*/
|
|
20
20
|
export declare function onFulfillMessage<T>(context: Context, channel: string, handler: MessageHandler<T>): Promise<() => boolean>;
|
|
21
|
+
//# sourceMappingURL=queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../../src/redis/queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAEvC,OAAO,EAAE,KAAK,cAAc,EAAe,MAAM,SAAS,CAAA;AAE1D;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,EAAE,CAAC,qBAOrG;AAED;;;;;;GAMG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,0BAkBtG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis-event-adapter.d.ts","sourceRoot":"","sources":["../../../src/redis/redis-event-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAU,MAAM,eAAe,CAAA;AACzD,OAAO,KAAK,KAAK,MAAM,SAAS,CAAA;AAEhC,qBAAa,iBAAiB;IAEtB,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,YAAY;IAC7C,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,KAAK;IACnC,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,KAAK;gBAFjB,YAAY,EAAE,YAAY,EAC1B,SAAS,EAAE,KAAK,EAChB,UAAU,EAAE,KAAK;IAsBxC,SAAS,CAAC,SAAS,GAAI,SAAS,MAAM,EAAE,SAAS,MAAM,EAAE,SAAS,MAAM,UAcvE;IAED,WAAW;CAId"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis-event-adapter.test.d.ts","sourceRoot":"","sources":["../../../src/redis/redis-event-adapter.test.ts"],"names":[],"mappings":""}
|
|
@@ -9,7 +9,12 @@ export declare const REDIS_CLIENT_KEY: unique symbol;
|
|
|
9
9
|
export declare const REDIS_PUB_KEY: unique symbol;
|
|
10
10
|
export declare const REDIS_SUB_KEY: unique symbol;
|
|
11
11
|
export declare const REDIS_OPTIONS_KEY: unique symbol;
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use context's DI system instead
|
|
14
|
+
* @param options The options to pass to the Redis client
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
export declare const redisMiddleware: (options: RedisOptions) => (context: Context) => Promise<void>;
|
|
13
18
|
/**
|
|
14
19
|
* Get the centralized redis connection
|
|
15
20
|
* NOTE: Do not use this connection to subscribe to events. Create your own instead with createRedis.
|
|
@@ -31,3 +36,4 @@ export declare function useRedisOptions(context: Context): RedisOptions;
|
|
|
31
36
|
*/
|
|
32
37
|
export declare function createRedis(context: Context): RedisInstance;
|
|
33
38
|
export declare function useRedisRepository<T = string>(context: Context): RedisRepository<T>;
|
|
39
|
+
//# sourceMappingURL=redis-middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis-middleware.d.ts","sourceRoot":"","sources":["../../../src/redis/redis-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAO,MAAM,eAAe,CAAA;AAC5C,OAAc,EAAE,KAAK,IAAI,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,SAAS,CAAA;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAE9C,eAAO,MAAM,WAAW;;;CAGvB,CAAA;AAED,eAAO,MAAM,gBAAgB,eAAW,CAAA;AACxC,eAAO,MAAM,aAAa,eAAW,CAAA;AACrC,eAAO,MAAM,aAAa,eAAW,CAAA;AACrC,eAAO,MAAM,iBAAiB,eAAW,CAAA;AAEzC;;;;GAIG;AACH,eAAO,MAAM,eAAe,GAAI,SAAS,YAAY,MAAY,SAAS,OAAO,kBAmBhF,CAAA;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,aAAa,CAQxD;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,YAAY,CAI9D;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,aAAa,CAU3D;AAED,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,eAAe,CAAC,CAAC,CAAC,CAMnF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Redis } from 'ioredis';
|
|
1
|
+
import type { Redis } from 'ioredis';
|
|
2
2
|
export declare enum RedisRepositoryEvent {
|
|
3
3
|
SET = "SET",
|
|
4
4
|
GET = "GET"
|
|
@@ -9,8 +9,9 @@ export declare class RedisRepository<T> {
|
|
|
9
9
|
private readonly handlers;
|
|
10
10
|
constructor(redis: Redis);
|
|
11
11
|
set(id: string, item: T): Promise<'OK'>;
|
|
12
|
-
get(id: string): Promise<T>;
|
|
12
|
+
get(id: string): Promise<T | null | undefined>;
|
|
13
13
|
on(event: RedisRepositoryEvent, handler: RepositoryEventHandler<T>): () => void;
|
|
14
14
|
onSet(handler: RepositoryEventHandler<T>): () => void;
|
|
15
15
|
onGet(handler: RepositoryEventHandler<T>): () => void;
|
|
16
16
|
}
|
|
17
|
+
//# sourceMappingURL=repository.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../../src/redis/repository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAGpC,oBAAY,oBAAoB;IAC5B,GAAG,QAAQ;IACX,GAAG,QAAQ;CACd;AAED,MAAM,MAAM,sBAAsB,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,oBAAoB,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,KAAK,GAAG,CAAA;AAEjG,qBAAa,eAAe,CAAC,CAAC;IAMd,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK;IAL3C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAGxB;gBAE8B,KAAK,EAAE,KAAK;IAErC,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAUvC,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;IAgBpD,EAAE,CAAC,KAAK,EAAE,oBAAoB,EAAE,OAAO,EAAE,sBAAsB,CAAC,CAAC,CAAC;IAQlE,KAAK,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC,CAAC;IAIxC,KAAK,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC,CAAC;CAG3C"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { Context } from '@declaro/core';
|
|
2
2
|
export declare function set<T>(context: Context, id: string, item: T): Promise<"OK">;
|
|
3
|
-
export declare function get<T>(context: Context, id: string): Promise<T>;
|
|
3
|
+
export declare function get<T>(context: Context, id: string): Promise<T | null | undefined>;
|
|
4
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../../src/redis/storage.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAEvC,wBAAsB,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,iBAMjE;AAED,wBAAsB,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,iCAQxD"}
|
|
@@ -9,5 +9,6 @@ export declare function serialize<T = string>(message: T): string;
|
|
|
9
9
|
* @param message A JSON string to unserialize
|
|
10
10
|
* @returns
|
|
11
11
|
*/
|
|
12
|
-
export declare function unserialize<T = string>(message
|
|
13
|
-
export type MessageHandler<T = string> = (message
|
|
12
|
+
export declare function unserialize<T = string>(message?: string | null): T | null | undefined;
|
|
13
|
+
export type MessageHandler<T = string> = (message?: T) => any;
|
|
14
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/redis/utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,CAAC,UAE/C;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,IAAI,GAAG,SAAS,CAKrF;AAED,MAAM,MAAM,cAAc,CAAC,CAAC,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,GAAG,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/settings/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,QAAQ,CAAA"}
|
|
@@ -3,10 +3,10 @@ export declare const SETTINGS_KEY = "DeclaroSettings";
|
|
|
3
3
|
export declare const SETTINGS_NAMESPACES = "DeclaroSettingsNamespaces";
|
|
4
4
|
export declare class Config<T = any> {
|
|
5
5
|
readonly namespace: string;
|
|
6
|
-
readonly defaultValue?: T;
|
|
7
|
-
constructor(namespace: string, defaultValue?: T);
|
|
6
|
+
readonly defaultValue?: T | undefined;
|
|
7
|
+
constructor(namespace: string, defaultValue?: T | undefined);
|
|
8
8
|
private get namespaceKey();
|
|
9
|
-
get(context: Context): Promise<T>;
|
|
9
|
+
get(context: Context): Promise<T | undefined>;
|
|
10
10
|
set(context: Context, value: Partial<T>): Promise<void>;
|
|
11
11
|
}
|
|
12
12
|
export declare class ConfigSet<T extends object> {
|
|
@@ -20,3 +20,4 @@ export declare class ConfigSet<T extends object> {
|
|
|
20
20
|
}>;
|
|
21
21
|
get(context: Context, key: string): Promise<T>;
|
|
22
22
|
}
|
|
23
|
+
//# sourceMappingURL=setting.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setting.d.ts","sourceRoot":"","sources":["../../../src/settings/setting.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAIvC,eAAO,MAAM,YAAY,oBAAoB,CAAA;AAC7C,eAAO,MAAM,mBAAmB,8BAA8B,CAAA;AAE9D,qBAAa,MAAM,CAAC,CAAC,GAAG,GAAG;aACK,SAAS,EAAE,MAAM;aAAkB,YAAY,CAAC,EAAE,CAAC;gBAAnD,SAAS,EAAE,MAAM,EAAkB,YAAY,CAAC,EAAE,CAAC,YAAA;IAE/E,OAAO,KAAK,YAAY,GAEvB;IAEK,GAAG,CAAC,OAAO,EAAE,OAAO;IAWpB,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;CAQhD;AAED,qBAAa,SAAS,CAAC,CAAC,SAAS,MAAM;aACP,SAAS,EAAE,MAAM;aAAkB,aAAa,EAAE,CAAC;gBAAnD,SAAS,EAAE,MAAM,EAAkB,aAAa,EAAE,CAAC;IAE/E,OAAO,KAAK,YAAY,GAEvB;IAEK,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,CAAC,CAAC,CAAM;IAa1D,MAAM,CAAC,OAAO,EAAE,OAAO;;;IAiBvB,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;CAUvD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"site.d.ts","sourceRoot":"","sources":["../../../src/settings/site.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC,eAAO,MAAM,UAAU;;;EAGrB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/test-utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAKvC,eAAO,MAAM,mBAAmB,SAAgB,SAAS,OAAO,kBAS/D,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@declaro/redis",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Redis tools for Declaro",
|
|
5
|
-
"main": "dist/
|
|
6
|
-
"module": "dist/
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
5
|
+
"main": "dist/node/index.cjs",
|
|
6
|
+
"module": "dist/node/index.js",
|
|
7
|
+
"types": "dist/ts/index.d.ts",
|
|
8
8
|
"author": "Emmert Technology LLC",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"license": "MIT",
|
|
@@ -12,13 +12,17 @@
|
|
|
12
12
|
"access": "public"
|
|
13
13
|
},
|
|
14
14
|
"scripts": {
|
|
15
|
-
"dev": "
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
15
|
+
"dev": "bun run scripts/build.ts --watch",
|
|
16
|
+
"clean": "bun rimraf dist",
|
|
17
|
+
"build:typedefs": "bun tsc --emitDeclarationOnly --outDir dist/ts -p tsconfig.json",
|
|
18
|
+
"build:js": "bun run scripts/build.ts",
|
|
19
|
+
"build": "bun run clean && bun run build:js && bun run build:typedefs",
|
|
20
|
+
"test": "bun test",
|
|
21
|
+
"test:watch": "bun test --watch",
|
|
22
|
+
"typecheck": "bun tsc --noEmit"
|
|
19
23
|
},
|
|
20
24
|
"devDependencies": {
|
|
21
|
-
"@declaro/core": "^2.
|
|
25
|
+
"@declaro/core": "^2.1.1",
|
|
22
26
|
"@types/ioredis-mock": "^8.2.5",
|
|
23
27
|
"@vitest/coverage-v8": "^0.32.2",
|
|
24
28
|
"ioredis": "^5.5.0",
|
|
@@ -28,7 +32,7 @@
|
|
|
28
32
|
"vitest": "^0.32.2"
|
|
29
33
|
},
|
|
30
34
|
"peerDependencies": {
|
|
31
|
-
"@declaro/core": "^2.0.0-beta.
|
|
35
|
+
"@declaro/core": "^2.0.0-beta.50",
|
|
32
36
|
"@types/ioredis-mock": "^8.9.0",
|
|
33
37
|
"ioredis": "^5.5.0",
|
|
34
38
|
"ioredis-mock": "^8.9.0"
|
|
@@ -41,5 +45,18 @@
|
|
|
41
45
|
"url": "https://github.com/emmertio/declaro/issues"
|
|
42
46
|
},
|
|
43
47
|
"homepage": "https://github.com/emmertio/declaro/tree/main#readme",
|
|
44
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "9e452f76e7d8c8b72f6ef5dfe829f28687b864de",
|
|
49
|
+
"files": [
|
|
50
|
+
"dist",
|
|
51
|
+
"src",
|
|
52
|
+
"readme.md"
|
|
53
|
+
],
|
|
54
|
+
"exports": {
|
|
55
|
+
"bun": "./src/index.ts",
|
|
56
|
+
"types": "./dist/ts/index.d.ts",
|
|
57
|
+
"import": "./dist/node/index.js",
|
|
58
|
+
"require": "./dist/node/index.cjs",
|
|
59
|
+
"browser": "./dist/browser/index.js",
|
|
60
|
+
"default": "./dist/node/index.js"
|
|
61
|
+
}
|
|
45
62
|
}
|
package/src/redis/queue.ts
CHANGED
|
@@ -33,11 +33,12 @@ export async function onFulfillMessage<T>(context: Context, channel: string, han
|
|
|
33
33
|
let shouldContinue = true
|
|
34
34
|
|
|
35
35
|
while (shouldContinue) {
|
|
36
|
-
const
|
|
36
|
+
const result = await conn.brpop(channel, 1000)
|
|
37
|
+
const [msgChannel, message] = result ?? []
|
|
37
38
|
|
|
38
39
|
try {
|
|
39
|
-
const payload = unserialize<T>(message)
|
|
40
|
-
await handler(payload)
|
|
40
|
+
const payload = unserialize<T>(message ?? '')
|
|
41
|
+
await handler(payload ?? undefined)
|
|
41
42
|
} catch (e) {
|
|
42
43
|
pushMessage(context, channel, message)
|
|
43
44
|
}
|
|
@@ -12,22 +12,28 @@ export const REDIS_PUB_KEY = Symbol()
|
|
|
12
12
|
export const REDIS_SUB_KEY = Symbol()
|
|
13
13
|
export const REDIS_OPTIONS_KEY = Symbol()
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated Use context's DI system instead
|
|
17
|
+
* @param options The options to pass to the Redis client
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
export const redisMiddleware = (options: RedisOptions) => async (context: Context) => {
|
|
16
21
|
context.provide(REDIS_OPTIONS_KEY, options)
|
|
17
22
|
|
|
18
23
|
context.on(App.Events.Init, async (context) => {
|
|
19
24
|
const redis = new Redis(options)
|
|
20
25
|
|
|
21
26
|
context.provide(REDIS_CLIENT_KEY, redis)
|
|
22
|
-
await context.emit(RedisEvents.Connect
|
|
27
|
+
await context.emit(RedisEvents.Connect)
|
|
23
28
|
})
|
|
24
29
|
|
|
25
30
|
context.on(App.Events.Destroy, async (context) => {
|
|
26
31
|
const redis = useRedis(context)
|
|
27
|
-
await context.emit(RedisEvents.Destroy
|
|
32
|
+
await context.emit(RedisEvents.Destroy)
|
|
28
33
|
})
|
|
29
34
|
|
|
30
|
-
context.on(RedisEvents.Destroy, async (context
|
|
35
|
+
context.on(RedisEvents.Destroy, async (context) => {
|
|
36
|
+
const redis = useRedis(context)
|
|
31
37
|
await redis.quit()
|
|
32
38
|
})
|
|
33
39
|
}
|
|
@@ -57,7 +63,7 @@ export function useRedis(context: Context): RedisInstance {
|
|
|
57
63
|
export function useRedisOptions(context: Context): RedisOptions {
|
|
58
64
|
const options = context.inject<RedisOptions>(REDIS_OPTIONS_KEY)
|
|
59
65
|
|
|
60
|
-
return options
|
|
66
|
+
return options!
|
|
61
67
|
}
|
|
62
68
|
|
|
63
69
|
/**
|
package/src/redis/repository.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Redis } from 'ioredis'
|
|
1
|
+
import type { Redis } from 'ioredis'
|
|
2
2
|
import { serialize, unserialize } from './utils'
|
|
3
3
|
|
|
4
4
|
export enum RedisRepositoryEvent {
|
|
@@ -26,12 +26,18 @@ export class RedisRepository<T> {
|
|
|
26
26
|
return result
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
async get(id: string): Promise<T> {
|
|
29
|
+
async get(id: string): Promise<T | null | undefined> {
|
|
30
30
|
const payload = await this.redis.get(id)
|
|
31
31
|
|
|
32
|
+
if (!payload) {
|
|
33
|
+
return null
|
|
34
|
+
}
|
|
35
|
+
|
|
32
36
|
const item = unserialize<T>(payload)
|
|
33
37
|
|
|
34
|
-
|
|
38
|
+
if (item) {
|
|
39
|
+
await Promise.all(this.handlers.GET.map((handler) => handler(RedisRepositoryEvent.GET, id, item)))
|
|
40
|
+
}
|
|
35
41
|
|
|
36
42
|
return item
|
|
37
43
|
}
|
package/src/redis/utils.ts
CHANGED
|
@@ -12,8 +12,11 @@ export function serialize<T = string>(message: T) {
|
|
|
12
12
|
* @param message A JSON string to unserialize
|
|
13
13
|
* @returns
|
|
14
14
|
*/
|
|
15
|
-
export function unserialize<T = string>(message
|
|
15
|
+
export function unserialize<T = string>(message?: string | null): T | null | undefined {
|
|
16
|
+
if (!message) {
|
|
17
|
+
return null as any
|
|
18
|
+
}
|
|
16
19
|
return JSON.parse(message)
|
|
17
20
|
}
|
|
18
21
|
|
|
19
|
-
export type MessageHandler<T = string> = (message
|
|
22
|
+
export type MessageHandler<T = string> = (message?: T) => any
|
package/src/settings/setting.ts
CHANGED
|
@@ -27,7 +27,7 @@ export class Config<T = any> {
|
|
|
27
27
|
const redis = useRedis(context)
|
|
28
28
|
|
|
29
29
|
const existingValues = await this.get(context)
|
|
30
|
-
const merged = merge.all([this.defaultValue, existingValues, value])
|
|
30
|
+
const merged = merge.all([this.defaultValue ?? {}, existingValues ?? {}, value])
|
|
31
31
|
const body = JSON.stringify(merged)
|
|
32
32
|
await redis.set(this.namespaceKey, body)
|
|
33
33
|
}
|