vona-module-a-redis 5.0.25 → 5.0.27

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/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './.metadata/index.ts';
2
+ export * from './lib/index.ts';
2
3
  export * from './types/index.ts';
package/dist/index.js CHANGED
@@ -1,7 +1,15 @@
1
- import { BeanInfo, BeanBase, cast, deepExtend, getRedisClientKeyPrefix, BeanSimple, combineConfigDefault, BeanScopeBase } from 'vona';
1
+ import { BeanInfo, BeanBase, cast, deepExtend, BeanSimple, combineConfigDefault, BeanScopeBase } from 'vona';
2
2
  import { Service, Bean, Scope } from 'vona-module-a-bean';
3
3
  import { Redis } from 'ioredis';
4
4
 
5
+ function getRedisClientKeyPrefix(clientName, appInfo) {
6
+ const mode = ['test', 'dev'].includes(appInfo.configMeta.mode) ? '_local' : '';
7
+ return `${clientName}_${appInfo.name}${mode}:`;
8
+ }
9
+ function prepareRedisClientKeyPrefix(keyPrefix, clientName, appInfo) {
10
+ return keyPrefix === true ? getRedisClientKeyPrefix(clientName, appInfo) : keyPrefix;
11
+ }
12
+
5
13
  var _dec$3, _dec2$3, _class$3;
6
14
  let ServiceRedisClient = (_dec$3 = Service(), _dec2$3 = BeanInfo({
7
15
  module: "a-redis"
@@ -30,7 +38,10 @@ let ServiceRedisClient = (_dec$3 = Service(), _dec2$3 = BeanInfo({
30
38
  } else if (cast(configClient).nodes) {
31
39
  return new Redis.Cluster(cast(configClient).nodes, cast(configClient));
32
40
  } else {
33
- const configNode = deepExtend({}, configRedis.base, configClient);
41
+ const keyPrefix = prepareRedisClientKeyPrefix(configClient.keyPrefix, clientName, this.app);
42
+ const configNode = deepExtend({}, configRedis.base, configClient, {
43
+ keyPrefix
44
+ });
34
45
  return new Redis(configNode);
35
46
  }
36
47
  }
@@ -60,15 +71,16 @@ let ServiceRedis = (_dec$1 = Service(), _dec2$1 = BeanInfo({
60
71
  await this._clearRedisKeys(app.bean.redis.get('queue'), `${getRedisClientKeyPrefix('bull', app)}*`);
61
72
  // broadcast channel has subscribed
62
73
  // await _clearRedisKeys(app.redis.get('broadcast'), `${getRedisClientKeyPrefix('broadcast', app)}*`);
63
- // redlock
64
- for (const clientName of this.$scope.redlock.config.redlock.clients) {
65
- await this._clearRedisKeys(app.bean.redis.get(clientName), `${getRedisClientKeyPrefix('redlock', app)}*`);
66
- }
67
- for (const clientName in app.config.redis.clients) {
68
- if (['redlock', 'limiter', 'queue', 'broadcast'].includes(clientName)) continue;
69
- if (clientName.includes('redlock')) continue;
70
- const client = app.config.redis.clients[clientName];
71
- await this._clearRedisKeys(app.bean.redis.get(clientName), `${client.keyPrefix}*`);
74
+ for (const _clientName in app.config.redis.clients) {
75
+ const clientName = _clientName;
76
+ if (['limiter', 'queue', 'broadcast'].includes(clientName)) continue;
77
+ if (clientName.startsWith('redlock')) {
78
+ await this._clearRedisKeys(app.bean.redis.get(clientName), `${getRedisClientKeyPrefix('redlock', app)}*`);
79
+ } else {
80
+ const client = app.config.redis.clients[clientName];
81
+ const keyPrefix = prepareRedisClientKeyPrefix(client.keyPrefix, clientName, this.app);
82
+ await this._clearRedisKeys(app.bean.redis.get(clientName), `${keyPrefix}*`);
83
+ }
72
84
  }
73
85
  }
74
86
  async _clearRedisKeys(redis, pattern) {
@@ -105,4 +117,4 @@ let ScopeModuleARedis = (_dec = Scope(), _dec2 = BeanInfo({
105
117
 
106
118
  /** scope: end */
107
119
 
108
- export { BeanRedis, Main, ScopeModuleARedis, ServiceRedis, ServiceRedisClient, configDefault };
120
+ export { BeanRedis, Main, ScopeModuleARedis, ServiceRedis, ServiceRedisClient, configDefault, getRedisClientKeyPrefix, prepareRedisClientKeyPrefix };
@@ -0,0 +1 @@
1
+ export * from './redis.ts';
@@ -0,0 +1,3 @@
1
+ import type { VonaAppInfo, VonaApplication } from 'vona';
2
+ export declare function getRedisClientKeyPrefix(clientName: string, appInfo: VonaAppInfo | VonaApplication): string;
3
+ export declare function prepareRedisClientKeyPrefix(keyPrefix: string | true | undefined, clientName: string, appInfo: VonaAppInfo | VonaApplication): string | undefined;
@@ -9,12 +9,15 @@ export interface IRedisClientRecord {
9
9
  summer: never;
10
10
  model: never;
11
11
  }
12
+ export interface IRedisClientOptions extends Omit<RedisOptions, 'keyPrefix'> {
13
+ keyPrefix?: string | true;
14
+ }
12
15
  export interface ConfigRedisCluster extends ClusterOptions {
13
16
  nodes: RedisOptions[];
14
17
  }
15
18
  export interface ConfigRedis {
16
19
  base: RedisOptions;
17
- clients: Record<keyof IRedisClientRecord, RedisOptions | ConfigRedisCluster>;
20
+ clients: Record<keyof IRedisClientRecord, IRedisClientOptions | ConfigRedisCluster>;
18
21
  }
19
22
  declare module 'vona' {
20
23
  interface VonaConfig {
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.25",
4
+ "version": "5.0.27",
5
5
  "title": "a-redis",
6
6
  "vonaModule": {
7
7
  "dependencies": {},