vona-module-a-redis 5.0.10 → 5.0.12

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.
@@ -1,36 +1,25 @@
1
- import type { BeanScopeUtil } from 'vona';
2
- /** bean: end */
3
1
  /** 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
2
  export * from '../bean/bean.redis.ts';
3
+ import 'vona';
20
4
  declare module 'vona' {
21
5
  }
22
6
  declare module 'vona-module-a-redis' {
23
7
  interface BeanRedis {
24
8
  }
25
9
  }
10
+ /** bean: end */
11
+ /** bean: begin */
12
+ import type { BeanRedis } from '../bean/bean.redis.ts';
13
+ import 'vona';
26
14
  declare module 'vona' {
27
15
  interface IBeanRecordGlobal {
28
- redis: BeanRedis;
16
+ 'redis': BeanRedis;
29
17
  }
30
18
  }
31
- /** service: end */
32
- /** main: begin */
33
- export * from '../main.ts';
19
+ /** bean: end */
20
+ /** service: begin */
21
+ export * from '../service/redisClient.ts';
22
+ import 'vona';
34
23
  declare module 'vona-module-a-web' {
35
24
  interface IServiceRecord {
36
25
  'a-redis:redisClient': never;
@@ -40,23 +29,33 @@ declare module 'vona-module-a-redis' {
40
29
  interface ServiceRedisClient {
41
30
  }
42
31
  }
32
+ /** service: end */
33
+ /** service: begin */
34
+ import type { ServiceRedisClient } from '../service/redisClient.ts';
43
35
  export interface IModuleService {
44
- redisClient: ServiceRedisClient;
36
+ 'redisClient': ServiceRedisClient;
45
37
  }
38
+ /** service: end */
39
+ /** service: begin */
40
+ import 'vona';
46
41
  declare module 'vona' {
47
42
  interface IBeanRecordGeneral {
48
43
  'a-redis.service.redisClient': ServiceRedisClient;
49
44
  }
50
45
  }
51
- /** bean: end */
52
- /** service: begin */
53
- export * from '../service/redisClient.ts';
46
+ /** service: end */
47
+ /** main: begin */
48
+ export * from '../main.ts';
49
+ /** main: end */
50
+ /** scope: begin */
51
+ import { BeanScopeBase, type BeanScopeUtil } from 'vona';
54
52
  export declare class ScopeModuleARedis extends BeanScopeBase {
55
53
  }
56
54
  export interface ScopeModuleARedis {
57
55
  util: BeanScopeUtil;
58
56
  service: IModuleService;
59
57
  }
58
+ import 'vona';
60
59
  declare module 'vona' {
61
60
  interface IBeanScopeRecord {
62
61
  'a-redis': ScopeModuleARedis;
package/dist/index.js CHANGED
@@ -1,2 +1,136 @@
1
- export * from "./.metadata/index.js";
2
- export * from "./types/index.js";
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-redis",
3
3
  "type": "module",
4
- "version": "5.0.10",
4
+ "version": "5.0.12",
5
5
  "title": "a-redis",
6
6
  "vonaModule": {
7
7
  "dependencies": {},
@@ -37,6 +37,6 @@
37
37
  },
38
38
  "scripts": {
39
39
  "clean": "rimraf dist tsconfig.build.tsbuildinfo",
40
- "tsc:publish": "npm run clean && tsc -p tsconfig.build.json"
40
+ "tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
41
41
  }
42
42
  }
@@ -1,32 +0,0 @@
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 */
@@ -1,2 +0,0 @@
1
- export const __ThisModule__ = 'a-redis';
2
- export { ScopeModuleARedis as ScopeModule } from "./index.js";
@@ -1,22 +0,0 @@
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/main.js DELETED
@@ -1,32 +0,0 @@
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
- }
@@ -1,78 +0,0 @@
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 };
@@ -1 +0,0 @@
1
- export * from "./redis.js";
@@ -1 +0,0 @@
1
- export {};