vona-module-a-redis 5.0.9 → 5.0.11

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.
@@ -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,2 @@
1
+ export declare const __ThisModule__ = "a-redis";
2
+ export { ScopeModuleARedis as ScopeModule } from './index.ts';
@@ -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,2 @@
1
+ export * from './.metadata/index.ts';
2
+ export * from './types/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,136 @@
1
+ import { BeanInfo, BeanBase, cast, deepExtend, BeanSimple, combineConfigDefault, BeanScopeBase } from 'vona';
2
+ import { Bean, Scope } from 'vona-module-a-bean';
3
+ import { Redis } from 'ioredis';
4
+ import { Service } from 'vona-module-a-web';
5
+
6
+ var _dec$2, _dec2$2, _class$2;
7
+ let ServiceRedisClient = (_dec$2 = Service(), _dec2$2 = BeanInfo({
8
+ module: "a-redis"
9
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = class ServiceRedisClient extends BeanBase {
10
+ constructor(...args) {
11
+ super(...args);
12
+ this._redisInstance = void 0;
13
+ }
14
+ get instance() {
15
+ return this._redisInstance;
16
+ }
17
+ __init__(clientName) {
18
+ // instance
19
+ this._redisInstance = this._createClient(clientName);
20
+ }
21
+ async __dispose__() {
22
+ await this._redisInstance?.quit();
23
+ }
24
+ _createClient(clientName) {
25
+ clientName = clientName || 'default';
26
+ const configRedis = this.app.config.redis;
27
+ const configClient = configRedis.clients[clientName];
28
+ if (!configClient) throw new Error(`redis client not found: ${clientName}`);
29
+ if (cast(configClient).sentinels) {
30
+ // sentinels
31
+ return new Redis(configClient);
32
+ } else if (cast(configClient).nodes) {
33
+ return new Redis.Cluster(cast(configClient).nodes, cast(configClient));
34
+ } else {
35
+ const configNode = deepExtend({}, configRedis.default, configClient);
36
+ return new Redis(configNode);
37
+ }
38
+ }
39
+ async clearAllData() {
40
+ const app = this.app;
41
+ // clear keys
42
+ // await this._clearRedisKeys(app.bean.redis.get('limiter'), `b_${app.name}:*`);
43
+ await this._clearRedisKeys(app.bean.redis.get('queue'), `bull_${app.name}:*`);
44
+ // broadcast channel has subscribed
45
+ // await _clearRedisKeys(app.redis.get('broadcast'), `broadcast_${app.name}:*`);
46
+ // redlock
47
+ for (const clientName of this.$scope.redlock.config.redlock.clients) {
48
+ await this._clearRedisKeys(app.bean.redis.get(clientName), `redlock_${app.name}:*`);
49
+ }
50
+ for (const clientName in app.config.redis.clients) {
51
+ if (['redlock', 'limiter', 'queue', 'broadcast'].includes(clientName)) continue;
52
+ if (clientName.includes('redlock')) continue;
53
+ const client = app.config.redis.clients[clientName];
54
+ await this._clearRedisKeys(app.bean.redis.get(clientName), `${client.keyPrefix}*`);
55
+ }
56
+ }
57
+ async _clearRedisKeys(redis, pattern) {
58
+ if (!redis) return;
59
+ const keyPrefix = redis.options.keyPrefix;
60
+ const keys = await redis.keys(pattern);
61
+ const keysDel = [];
62
+ for (const fullKey of keys) {
63
+ const key = keyPrefix ? fullKey.substring(keyPrefix.length) : fullKey;
64
+ keysDel.push(key);
65
+ }
66
+ if (keysDel.length > 0) {
67
+ await redis.del(keysDel);
68
+ }
69
+ }
70
+ }) || _class$2) || _class$2);
71
+
72
+ var _dec$1, _dec2$1, _class$1;
73
+ let BeanRedis = (_dec$1 = Bean(), _dec2$1 = BeanInfo({
74
+ module: "a-redis"
75
+ }), _dec$1(_class$1 = _dec2$1(_class$1 = class BeanRedis extends BeanBase {
76
+ getClient(clientName) {
77
+ return this.app.bean._getBeanSelector(ServiceRedisClient, clientName);
78
+ }
79
+ get(clientName) {
80
+ const client = this.getClient(clientName);
81
+ return client.instance;
82
+ }
83
+ }) || _class$1) || _class$1);
84
+
85
+ class Main extends BeanSimple {
86
+ async moduleLoading() {
87
+ const _configDefault = await combineConfigDefault(this.app, configDefault);
88
+ this.app.config.redis = deepExtend({}, _configDefault, this.app.config.redis);
89
+ }
90
+ async moduleLoaded() {}
91
+ async configLoaded(_config) {}
92
+ }
93
+ async function configDefault(app) {
94
+ return {
95
+ default: {
96
+ host: app.meta.env.REDIS_DEFAULT_HOST,
97
+ port: Number.parseInt(app.meta.env.REDIS_DEFAULT_PORT),
98
+ password: app.meta.env.REDIS_DEFAULT_PASSWORD,
99
+ db: Number.parseInt(app.meta.env.REDIS_DEFAULT_DB),
100
+ maxRetriesPerRequest: null,
101
+ enableReadyCheck: false
102
+ },
103
+ clients: {
104
+ default: {
105
+ keyPrefix: `default_${app.name}:`
106
+ },
107
+ redlock: {},
108
+ queue: {},
109
+ broadcast: {},
110
+ cache: {
111
+ keyPrefix: `cache_${app.name}:`
112
+ },
113
+ io: {
114
+ keyPrefix: `io_${app.name}:`
115
+ },
116
+ auth: {
117
+ keyPrefix: `auth_${app.name}:`
118
+ },
119
+ summer: {
120
+ keyPrefix: `summer_${app.name}:`
121
+ },
122
+ model: {
123
+ keyPrefix: `model_${app.name}:`
124
+ }
125
+ }
126
+ };
127
+ }
128
+
129
+ var _dec, _dec2, _class;
130
+ let ScopeModuleARedis = (_dec = Scope(), _dec2 = BeanInfo({
131
+ module: "a-redis"
132
+ }), _dec(_class = _dec2(_class = class ScopeModuleARedis extends BeanScopeBase {}) || _class) || _class);
133
+
134
+ /** scope: end */
135
+
136
+ export { BeanRedis, Main, ScopeModuleARedis, ServiceRedisClient, configDefault };
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>>;
@@ -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 @@
1
+ export * from './redis.ts';
@@ -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
+ }
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.9",
4
+ "version": "5.0.11",
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 && vona :bin:buildModule && tsc -p tsconfig.build.json"
37
41
  }
38
42
  }