vona-module-a-redis 5.0.8 → 5.0.10
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/.metadata/index.d.ts +68 -0
- package/dist/.metadata/index.js +32 -0
- package/dist/.metadata/this.d.ts +2 -0
- package/dist/.metadata/this.js +2 -0
- package/dist/bean/bean.redis.d.ts +7 -0
- package/dist/bean/bean.redis.js +22 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/main.d.ts +9 -0
- package/dist/main.js +32 -0
- package/dist/service/redisClient.d.ts +11 -0
- package/dist/service/redisClient.js +78 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/redis.d.ts +30 -0
- package/dist/types/redis.js +1 -0
- package/package.json +7 -3
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { BeanScopeUtil } from 'vona';
|
|
2
|
+
/** bean: end */
|
|
3
|
+
/** bean: begin */
|
|
4
|
+
import type { BeanRedis } from '../bean/bean.redis.ts';
|
|
5
|
+
/** service: end */
|
|
6
|
+
/** service: begin */
|
|
7
|
+
import type { ServiceRedisClient } from '../service/redisClient.ts';
|
|
8
|
+
/** main: end */
|
|
9
|
+
/** scope: begin */
|
|
10
|
+
import { BeanScopeBase } from 'vona';
|
|
11
|
+
/** bean: begin */
|
|
12
|
+
import 'vona';
|
|
13
|
+
/** service: end */
|
|
14
|
+
/** service: begin */
|
|
15
|
+
import 'vona';
|
|
16
|
+
import 'vona';
|
|
17
|
+
import 'vona';
|
|
18
|
+
import 'vona';
|
|
19
|
+
export * from '../bean/bean.redis.ts';
|
|
20
|
+
declare module 'vona' {
|
|
21
|
+
}
|
|
22
|
+
declare module 'vona-module-a-redis' {
|
|
23
|
+
interface BeanRedis {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
declare module 'vona' {
|
|
27
|
+
interface IBeanRecordGlobal {
|
|
28
|
+
redis: BeanRedis;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** service: end */
|
|
32
|
+
/** main: begin */
|
|
33
|
+
export * from '../main.ts';
|
|
34
|
+
declare module 'vona-module-a-web' {
|
|
35
|
+
interface IServiceRecord {
|
|
36
|
+
'a-redis:redisClient': never;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
declare module 'vona-module-a-redis' {
|
|
40
|
+
interface ServiceRedisClient {
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export interface IModuleService {
|
|
44
|
+
redisClient: ServiceRedisClient;
|
|
45
|
+
}
|
|
46
|
+
declare module 'vona' {
|
|
47
|
+
interface IBeanRecordGeneral {
|
|
48
|
+
'a-redis.service.redisClient': ServiceRedisClient;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** bean: end */
|
|
52
|
+
/** service: begin */
|
|
53
|
+
export * from '../service/redisClient.ts';
|
|
54
|
+
export declare class ScopeModuleARedis extends BeanScopeBase {
|
|
55
|
+
}
|
|
56
|
+
export interface ScopeModuleARedis {
|
|
57
|
+
util: BeanScopeUtil;
|
|
58
|
+
service: IModuleService;
|
|
59
|
+
}
|
|
60
|
+
declare module 'vona' {
|
|
61
|
+
interface IBeanScopeRecord {
|
|
62
|
+
'a-redis': ScopeModuleARedis;
|
|
63
|
+
}
|
|
64
|
+
interface IBeanScopeContainer {
|
|
65
|
+
redis: ScopeModuleARedis;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** scope: end */
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
/** main: end */
|
|
8
|
+
/** scope: begin */
|
|
9
|
+
import { BeanScopeBase } from 'vona';
|
|
10
|
+
import { Scope } from 'vona-module-a-bean';
|
|
11
|
+
/** bean: begin */
|
|
12
|
+
import 'vona';
|
|
13
|
+
/** service: end */
|
|
14
|
+
/** service: begin */
|
|
15
|
+
import 'vona';
|
|
16
|
+
import 'vona';
|
|
17
|
+
import 'vona';
|
|
18
|
+
import 'vona';
|
|
19
|
+
export * from "../bean/bean.redis.js";
|
|
20
|
+
/** service: end */
|
|
21
|
+
/** main: begin */
|
|
22
|
+
export * from "../main.js";
|
|
23
|
+
/** bean: end */
|
|
24
|
+
/** service: begin */
|
|
25
|
+
export * from "../service/redisClient.js";
|
|
26
|
+
let ScopeModuleARedis = class ScopeModuleARedis extends BeanScopeBase {
|
|
27
|
+
};
|
|
28
|
+
ScopeModuleARedis = __decorate([
|
|
29
|
+
Scope()
|
|
30
|
+
], ScopeModuleARedis);
|
|
31
|
+
export { ScopeModuleARedis };
|
|
32
|
+
/** scope: end */
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { IRedisClientRecord } from '../types/redis.ts';
|
|
2
|
+
import { BeanBase } from 'vona';
|
|
3
|
+
import { ServiceRedisClient } from '../service/redisClient.ts';
|
|
4
|
+
export declare class BeanRedis extends BeanBase {
|
|
5
|
+
getClient(clientName?: keyof IRedisClientRecord): ServiceRedisClient;
|
|
6
|
+
get(clientName?: keyof IRedisClientRecord): import("ioredis").default;
|
|
7
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { BeanBase } from 'vona';
|
|
8
|
+
import { Bean } from 'vona-module-a-bean';
|
|
9
|
+
import { ServiceRedisClient } from "../service/redisClient.js";
|
|
10
|
+
let BeanRedis = class BeanRedis extends BeanBase {
|
|
11
|
+
getClient(clientName) {
|
|
12
|
+
return this.app.bean._getBeanSelector(ServiceRedisClient, clientName);
|
|
13
|
+
}
|
|
14
|
+
get(clientName) {
|
|
15
|
+
const client = this.getClient(clientName);
|
|
16
|
+
return client.instance;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
BeanRedis = __decorate([
|
|
20
|
+
Bean()
|
|
21
|
+
], BeanRedis);
|
|
22
|
+
export { BeanRedis };
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { IModuleMain, PowerPartial, VonaApplication } from 'vona';
|
|
2
|
+
import type { ConfigRedis } from './types/redis.ts';
|
|
3
|
+
import { BeanSimple } from 'vona';
|
|
4
|
+
export declare class Main extends BeanSimple implements IModuleMain {
|
|
5
|
+
moduleLoading(): Promise<void>;
|
|
6
|
+
moduleLoaded(): Promise<void>;
|
|
7
|
+
configLoaded(_config: any): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export declare function configDefault(app: VonaApplication): Promise<PowerPartial<ConfigRedis>>;
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BeanSimple, combineConfigDefault, deepExtend } from 'vona';
|
|
2
|
+
export class Main extends BeanSimple {
|
|
3
|
+
async moduleLoading() {
|
|
4
|
+
const _configDefault = await combineConfigDefault(this.app, configDefault);
|
|
5
|
+
this.app.config.redis = deepExtend({}, _configDefault, this.app.config.redis);
|
|
6
|
+
}
|
|
7
|
+
async moduleLoaded() { }
|
|
8
|
+
async configLoaded(_config) { }
|
|
9
|
+
}
|
|
10
|
+
export async function configDefault(app) {
|
|
11
|
+
return {
|
|
12
|
+
default: {
|
|
13
|
+
host: app.meta.env.REDIS_DEFAULT_HOST,
|
|
14
|
+
port: Number.parseInt(app.meta.env.REDIS_DEFAULT_PORT),
|
|
15
|
+
password: app.meta.env.REDIS_DEFAULT_PASSWORD,
|
|
16
|
+
db: Number.parseInt(app.meta.env.REDIS_DEFAULT_DB),
|
|
17
|
+
maxRetriesPerRequest: null,
|
|
18
|
+
enableReadyCheck: false,
|
|
19
|
+
},
|
|
20
|
+
clients: {
|
|
21
|
+
default: { keyPrefix: `default_${app.name}:` },
|
|
22
|
+
redlock: {},
|
|
23
|
+
queue: {},
|
|
24
|
+
broadcast: {},
|
|
25
|
+
cache: { keyPrefix: `cache_${app.name}:` },
|
|
26
|
+
io: { keyPrefix: `io_${app.name}:` },
|
|
27
|
+
auth: { keyPrefix: `auth_${app.name}:` },
|
|
28
|
+
summer: { keyPrefix: `summer_${app.name}:` },
|
|
29
|
+
model: { keyPrefix: `model_${app.name}:` },
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Redis } from 'ioredis';
|
|
2
|
+
import { BeanBase } from 'vona';
|
|
3
|
+
export declare class ServiceRedisClient extends BeanBase {
|
|
4
|
+
private _redisInstance;
|
|
5
|
+
get instance(): Redis;
|
|
6
|
+
protected __init__(clientName?: string): void;
|
|
7
|
+
protected __dispose__(): Promise<void>;
|
|
8
|
+
private _createClient;
|
|
9
|
+
clearAllData(): Promise<void>;
|
|
10
|
+
private _clearRedisKeys;
|
|
11
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { Redis } from 'ioredis';
|
|
8
|
+
import { BeanBase, cast, deepExtend } from 'vona';
|
|
9
|
+
import { Service } from 'vona-module-a-web';
|
|
10
|
+
let ServiceRedisClient = class ServiceRedisClient extends BeanBase {
|
|
11
|
+
_redisInstance;
|
|
12
|
+
get instance() {
|
|
13
|
+
return this._redisInstance;
|
|
14
|
+
}
|
|
15
|
+
__init__(clientName) {
|
|
16
|
+
// instance
|
|
17
|
+
this._redisInstance = this._createClient(clientName);
|
|
18
|
+
}
|
|
19
|
+
async __dispose__() {
|
|
20
|
+
await this._redisInstance?.quit();
|
|
21
|
+
}
|
|
22
|
+
_createClient(clientName) {
|
|
23
|
+
clientName = clientName || 'default';
|
|
24
|
+
const configRedis = this.app.config.redis;
|
|
25
|
+
const configClient = configRedis.clients[clientName];
|
|
26
|
+
if (!configClient)
|
|
27
|
+
throw new Error(`redis client not found: ${clientName}`);
|
|
28
|
+
if (cast(configClient).sentinels) {
|
|
29
|
+
// sentinels
|
|
30
|
+
return new Redis(configClient);
|
|
31
|
+
}
|
|
32
|
+
else if (cast(configClient).nodes) {
|
|
33
|
+
return new Redis.Cluster(cast(configClient).nodes, cast(configClient));
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
const configNode = deepExtend({}, configRedis.default, configClient);
|
|
37
|
+
return new Redis(configNode);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async clearAllData() {
|
|
41
|
+
const app = this.app;
|
|
42
|
+
// clear keys
|
|
43
|
+
// await this._clearRedisKeys(app.bean.redis.get('limiter'), `b_${app.name}:*`);
|
|
44
|
+
await this._clearRedisKeys(app.bean.redis.get('queue'), `bull_${app.name}:*`);
|
|
45
|
+
// broadcast channel has subscribed
|
|
46
|
+
// await _clearRedisKeys(app.redis.get('broadcast'), `broadcast_${app.name}:*`);
|
|
47
|
+
// redlock
|
|
48
|
+
for (const clientName of this.$scope.redlock.config.redlock.clients) {
|
|
49
|
+
await this._clearRedisKeys(app.bean.redis.get(clientName), `redlock_${app.name}:*`);
|
|
50
|
+
}
|
|
51
|
+
for (const clientName in app.config.redis.clients) {
|
|
52
|
+
if (['redlock', 'limiter', 'queue', 'broadcast'].includes(clientName))
|
|
53
|
+
continue;
|
|
54
|
+
if (clientName.includes('redlock'))
|
|
55
|
+
continue;
|
|
56
|
+
const client = app.config.redis.clients[clientName];
|
|
57
|
+
await this._clearRedisKeys(app.bean.redis.get(clientName), `${client.keyPrefix}*`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async _clearRedisKeys(redis, pattern) {
|
|
61
|
+
if (!redis)
|
|
62
|
+
return;
|
|
63
|
+
const keyPrefix = redis.options.keyPrefix;
|
|
64
|
+
const keys = await redis.keys(pattern);
|
|
65
|
+
const keysDel = [];
|
|
66
|
+
for (const fullKey of keys) {
|
|
67
|
+
const key = keyPrefix ? fullKey.substring(keyPrefix.length) : fullKey;
|
|
68
|
+
keysDel.push(key);
|
|
69
|
+
}
|
|
70
|
+
if (keysDel.length > 0) {
|
|
71
|
+
await redis.del(keysDel);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
ServiceRedisClient = __decorate([
|
|
76
|
+
Service()
|
|
77
|
+
], ServiceRedisClient);
|
|
78
|
+
export { ServiceRedisClient };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './redis.ts';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./redis.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ClusterOptions, RedisOptions } from 'ioredis';
|
|
2
|
+
export interface IRedisClientRecord {
|
|
3
|
+
default: never;
|
|
4
|
+
redlock: never;
|
|
5
|
+
queue: never;
|
|
6
|
+
broadcast: never;
|
|
7
|
+
cache: never;
|
|
8
|
+
io: never;
|
|
9
|
+
auth: never;
|
|
10
|
+
summer: never;
|
|
11
|
+
model: never;
|
|
12
|
+
}
|
|
13
|
+
export interface ConfigRedisCluster extends ClusterOptions {
|
|
14
|
+
nodes: RedisOptions[];
|
|
15
|
+
}
|
|
16
|
+
export interface ConfigRedis {
|
|
17
|
+
default: RedisOptions;
|
|
18
|
+
clients: Record<keyof IRedisClientRecord, RedisOptions | ConfigRedisCluster>;
|
|
19
|
+
}
|
|
20
|
+
declare module 'vona' {
|
|
21
|
+
interface VonaConfig {
|
|
22
|
+
redis: ConfigRedis;
|
|
23
|
+
}
|
|
24
|
+
interface VonaConfigEnv {
|
|
25
|
+
REDIS_DEFAULT_HOST: string | undefined;
|
|
26
|
+
REDIS_DEFAULT_PORT: string | undefined;
|
|
27
|
+
REDIS_DEFAULT_PASSWORD: string | undefined;
|
|
28
|
+
REDIS_DEFAULT_DB: string | undefined;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vona-module-a-redis",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "5.0.
|
|
4
|
+
"version": "5.0.10",
|
|
5
5
|
"title": "a-redis",
|
|
6
6
|
"vonaModule": {
|
|
7
7
|
"dependencies": {},
|
|
@@ -31,8 +31,12 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"ioredis": "^5.4.1"
|
|
33
33
|
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"clean-package": "^2.2.0",
|
|
36
|
+
"rimraf": "^6.0.1"
|
|
37
|
+
},
|
|
34
38
|
"scripts": {
|
|
35
|
-
"clean": "rimraf dist tsconfig.tsbuildinfo",
|
|
36
|
-
"tsc:publish": "npm run clean && tsc"
|
|
39
|
+
"clean": "rimraf dist tsconfig.build.tsbuildinfo",
|
|
40
|
+
"tsc:publish": "npm run clean && tsc -p tsconfig.build.json"
|
|
37
41
|
}
|
|
38
42
|
}
|