@candlerip/shared 0.0.127 → 0.0.130

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,4 @@
1
+ import { Cache } from '../cache/index.js';
2
+ type ElementOrArray<T> = T extends readonly (infer U)[] ? U | T : T;
3
+ export type CacheData = ElementOrArray<Cache[keyof Cache]>;
4
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export * from './domains.js';
@@ -0,0 +1 @@
1
+ export * from './domains.js';
@@ -6,3 +6,4 @@ export * from './language-dictionary-cache-key/index.js';
6
6
  export * from './page-dictionary-cache-key/index.js';
7
7
  export * from './page-data-cache-key/index.js';
8
8
  export * from './page-data-dictionary-cache-key/index.js';
9
+ export * from './candle-lighting-profiles-cache-key/index.js';
@@ -6,3 +6,4 @@ export * from './language-dictionary-cache-key/index.js';
6
6
  export * from './page-dictionary-cache-key/index.js';
7
7
  export * from './page-data-cache-key/index.js';
8
8
  export * from './page-data-dictionary-cache-key/index.js';
9
+ export * from './candle-lighting-profiles-cache-key/index.js';
@@ -1,2 +1,3 @@
1
1
  export * from './cache/index.js';
2
2
  export * from './cache-key/index.js';
3
+ export * from './cache-data/index.js';
@@ -1,2 +1,3 @@
1
1
  export * from './cache/index.js';
2
2
  export * from './cache-key/index.js';
3
+ export * from './cache-data/index.js';
@@ -0,0 +1,2 @@
1
+ import { ComposeGetRedisData } from './type.js';
2
+ export declare const composeGetRedisData: ComposeGetRedisData;
@@ -0,0 +1,26 @@
1
+ import { isString } from '../../../../common/index.js';
2
+ export const composeGetRedisData = (props) => {
3
+ const { cacheKey } = props;
4
+ let objKey;
5
+ if ('objKey' in props && props.objKey) {
6
+ objKey = props.objKey;
7
+ }
8
+ let objKeys;
9
+ if ('objKeys' in props && props.objKeys) {
10
+ objKeys = props.objKeys;
11
+ }
12
+ let pathKeys;
13
+ if (objKey) {
14
+ const [key, value] = Object.entries(objKey)[0];
15
+ pathKeys = `$.[?(@.${key}==${isString(value) ? `"${value}"` : value})]`;
16
+ }
17
+ if (objKeys) {
18
+ pathKeys = objKeys?.map((it) => `$.${it}`);
19
+ }
20
+ return {
21
+ cacheKey,
22
+ objKey,
23
+ objKeys,
24
+ pathKeys,
25
+ };
26
+ };
@@ -0,0 +1,11 @@
1
+ import { CacheKey } from '../../../common/index.js';
2
+ import { ObjKey, ObjKeys } from '../type.js';
3
+ export type ComposeGetRedisData = (props: {
4
+ cacheKey: CacheKey;
5
+ objKey?: ObjKey;
6
+ objKeys?: ObjKeys;
7
+ }) => {
8
+ cacheKey: CacheKey;
9
+ objKeys?: ObjKeys;
10
+ pathKeys: string | string[] | undefined;
11
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,6 @@
1
- import { CustomErrorWorker, isArray, isString } from '../../../common/index.js';
1
+ import { CustomErrorWorker } from '../../../common/index.js';
2
2
  import { createClient } from 'redis';
3
+ import { composeGetRedisData } from './compose-redis-get-data/index.js';
3
4
  export const CacheWorker = (environmentMode, url, password, options) => {
4
5
  const _isEnabled = Boolean(options?.isEnabled);
5
6
  const customErrorWorker = CustomErrorWorker();
@@ -37,138 +38,99 @@ export const CacheWorker = (environmentMode, url, password, options) => {
37
38
  data: _client,
38
39
  };
39
40
  };
40
- const getCache = async (key) => {
41
- const { composeCustomError } = CustomErrorWorker({ info: { key } });
42
- const { data: client, customError } = _getClient();
43
- if (customError) {
44
- return { customError };
45
- }
46
- let data;
47
- try {
48
- const cache = await client.get(key);
49
- if (cache) {
50
- data = JSON.parse(cache);
51
- }
52
- }
53
- catch (err) {
54
- return {
55
- customError: composeCustomError('Error get data from Redis', { err }),
56
- };
57
- }
58
- if (!data) {
59
- return {
60
- customError: composeCustomError('No data in Redis'),
61
- };
62
- }
63
- return {
64
- data,
65
- };
66
- };
67
- const mGetCache = async (keys) => {
68
- const { composeCustomError } = CustomErrorWorker({ info: { keys } });
41
+ const getCache = async (props) => {
42
+ const { composeCustomError } = CustomErrorWorker({ info: props });
43
+ const { cacheKey, objKeys, pathKeys } = composeGetRedisData(props);
69
44
  const { data: client, customError } = _getClient();
70
45
  if (customError) {
71
46
  return { customError };
72
47
  }
73
48
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
- const data = {};
49
+ let data;
75
50
  try {
76
- const cache = await client.mGet(keys);
77
- if (!isArray(cache, isString)) {
51
+ data = await client.json.get(cacheKey, { path: pathKeys });
52
+ if (!data || data?.length === 0) {
78
53
  return {
79
- customError: composeCustomError('Error get data from redis', { cache }),
54
+ customError: composeCustomError('No data in Redis'),
80
55
  };
81
56
  }
82
- cache.forEach((it, i) => {
83
- data[keys[i]] = JSON.parse(it);
84
- });
85
57
  }
86
58
  catch (err) {
87
59
  return {
88
- customError: composeCustomError('Error get data from redis', { err }),
89
- };
90
- }
91
- if (Object.keys(data).length !== keys.length) {
92
- return {
93
- customError: composeCustomError('Missing data from redis', { data }),
60
+ customError: composeCustomError('Error get data by object keys from Redis', { err }),
94
61
  };
95
62
  }
96
- return {
97
- data,
98
- };
99
- };
100
- const setCache = async (key, data) => {
101
- const { composeCustomError } = CustomErrorWorker({ info: { key } });
102
- const { data: client, customError } = _getClient();
103
- if (customError) {
104
- return { customError };
63
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
+ let normalizedData = {};
65
+ if (objKeys) {
66
+ objKeys.forEach((objKey) => {
67
+ normalizedData[objKey] = objKeys?.length === 1 ? data[0] : data[`$.${objKey}`][0];
68
+ });
105
69
  }
106
- try {
107
- await client.set(key, JSON.stringify(data));
70
+ else {
71
+ normalizedData = data;
108
72
  }
109
- catch (err) {
73
+ if (Object.keys(normalizedData).some((key) => normalizedData[key] === null)) {
110
74
  return {
111
- customError: composeCustomError('Error set data to redis', { err }),
75
+ customError: composeCustomError('Error get data by object keys from Redis', { data }),
112
76
  };
113
77
  }
114
78
  return {
115
- data: null,
79
+ data: normalizedData,
116
80
  };
117
81
  };
118
- const hGetCache = async (cacheKey, objKeys) => {
119
- const { composeCustomError } = CustomErrorWorker({ info: { cacheKey, objKeys } });
82
+ const mGetCache = async (props) => {
83
+ const { composeCustomError } = CustomErrorWorker({ info: { props } });
84
+ const getRedisData = props.map((it) => composeGetRedisData(it));
120
85
  const { data: client, customError } = _getClient();
121
86
  if (customError) {
122
87
  return { customError };
123
88
  }
89
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
124
90
  let data;
125
- if (!objKeys) {
126
- try {
127
- data = await client.hGetAll(cacheKey);
128
- if (Object.keys(data).length === 0) {
129
- return {
130
- customError: composeCustomError('No data in Redis'),
131
- };
132
- }
133
- return {
134
- data,
135
- };
136
- }
137
- catch (err) {
91
+ try {
92
+ const promises = getRedisData.map((it) => client.json.get(it.cacheKey, { path: it.pathKeys }));
93
+ data = await Promise.all(promises);
94
+ console.error('***', data);
95
+ if (!data || data.some((it) => it.length === 0)) {
138
96
  return {
139
- customError: composeCustomError('Error get data from Redis', { err }),
97
+ customError: composeCustomError('No data in Redis'),
140
98
  };
141
99
  }
142
100
  }
143
- try {
144
- data = await client.hmGet(cacheKey, objKeys);
145
- }
146
101
  catch (err) {
147
102
  return {
148
103
  customError: composeCustomError('Error get data by object keys from Redis', { err }),
149
104
  };
150
105
  }
151
- if (data.some((item) => item === null)) {
152
- return {
153
- customError: composeCustomError('Error get data by object keys from Redis', { data }),
154
- };
155
- }
106
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
107
+ const normalizedData = {};
108
+ getRedisData.forEach((it, index) => {
109
+ const { objKeys } = it;
110
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
+ let normalizedCacheKeyData = {};
112
+ if (objKeys) {
113
+ objKeys.forEach((objKey) => {
114
+ normalizedCacheKeyData[objKey] = objKeys?.length === 1 ? data[index][0] : data[index][`$.${objKey}`][0];
115
+ });
116
+ }
117
+ else {
118
+ normalizedCacheKeyData = data[index];
119
+ }
120
+ normalizedData[it.cacheKey] = normalizedCacheKeyData;
121
+ });
156
122
  return {
157
- data: data.reduce((acc, curr, index) => ({
158
- ...acc,
159
- [objKeys[index]]: curr,
160
- }), {}),
123
+ data: normalizedData,
161
124
  };
162
125
  };
163
- const hSetCache = async (key, data) => {
126
+ const setCache = async (key, data) => {
164
127
  const { composeCustomError } = CustomErrorWorker({ info: { key } });
165
128
  const { data: client, customError } = _getClient();
166
129
  if (customError) {
167
130
  return { customError };
168
131
  }
169
132
  try {
170
- await client.del(key);
171
- await _client.json.set(key, '$', data);
133
+ await client.json.set(key, '$', data);
172
134
  }
173
135
  catch (err) {
174
136
  return {
@@ -180,8 +142,6 @@ export const CacheWorker = (environmentMode, url, password, options) => {
180
142
  };
181
143
  };
182
144
  return {
183
- hGetCache,
184
- hSetCache,
185
145
  getCache,
186
146
  mGetCache,
187
147
  setCache,
@@ -1,19 +1,34 @@
1
1
  import { EnvironmentMode, TCustomError } from '../../../common/index.js';
2
- import { Cache } from '../../common/cache/domains.js';
2
+ import { CacheKey, CandleLightingProfilesCacheKey, Cache, CacheData } from '../../common/index.js';
3
3
  import { createClient } from 'redis';
4
- import { CacheKey, CommonPageDictionaryCacheKey } from '../../common/index.js';
5
4
  export type TCacheWorker = (environmentMode: EnvironmentMode, url: string, password: string, options?: {
6
5
  isEnabled?: boolean;
7
6
  }) => {
8
7
  getCache: GetCache;
9
- hGetCache: HGetCache;
10
- hSetCache: HSetCache;
11
8
  mGetCache: MGetCache;
12
9
  setCache: SetCache;
13
10
  };
14
- export type GetCache = <T extends CacheKey = never, D extends Cache[T] = Cache[T]>(key: T) => Promise<TCustomError<D>>;
15
- export type MGetCache = <T extends CacheKey = never, D extends Pick<Cache, T> = Pick<Cache, T>>(keys: T[]) => Promise<TCustomError<D>>;
16
- export type SetCache = <T extends CacheKey = never>(key: T, data: Cache[T]) => Promise<TCustomError<null>>;
17
11
  export type GetClient = () => TCustomError<ReturnType<typeof createClient>>;
18
- export type HGetCache = <T extends CommonPageDictionaryCacheKey, D extends Cache[T]>(cacheKey: T, objKeys?: string[]) => Promise<TCustomError<D>>;
19
- export type HSetCache = <T extends CommonPageDictionaryCacheKey>(key: T, data: Cache[T]) => Promise<TCustomError<null>>;
12
+ export type SetCache = <T extends keyof Cache>(key: T, data: Cache[T]) => Promise<TCustomError<null>>;
13
+ export type GetCache = <T extends CacheData = never, C extends CacheKey = CacheKey>(props: GetCacheProps<C>) => Promise<TCustomError<T>>;
14
+ export type GetCacheProps<C extends CacheKey> = C extends CandleLightingProfilesCacheKey ? {
15
+ cacheKey: C;
16
+ objKey?: ObjKey;
17
+ } : {
18
+ cacheKey: C;
19
+ objKeys?: string[];
20
+ };
21
+ export type MGetCache = <T extends {
22
+ [key in CacheKey]?: CacheData;
23
+ } = never, C extends CacheKey = CacheKey>(props: GetCacheProps<C>[]) => Promise<TCustomError<T>>;
24
+ export type ObjKeys = string[];
25
+ export type ObjKey = {
26
+ code: number;
27
+ _id?: never;
28
+ } | {
29
+ code?: never;
30
+ _id: string;
31
+ } | {
32
+ code?: never;
33
+ _id?: never;
34
+ };
@@ -3,13 +3,13 @@ export declare const CandleLightingProfileDbSchema: mongoose.Schema<any, mongoos
3
3
  versionKey: false;
4
4
  }, {
5
5
  createdAt?: NativeDate | null | undefined;
6
- burningSeconds?: number | null | undefined;
7
6
  code?: string | null | undefined;
7
+ burningSeconds?: number | null | undefined;
8
8
  isDefault?: boolean | null | undefined;
9
9
  }, mongoose.Document<unknown, {}, {
10
10
  createdAt?: NativeDate | null | undefined;
11
- burningSeconds?: number | null | undefined;
12
11
  code?: string | null | undefined;
12
+ burningSeconds?: number | null | undefined;
13
13
  isDefault?: boolean | null | undefined;
14
14
  }, {
15
15
  id: string;
@@ -17,8 +17,8 @@ export declare const CandleLightingProfileDbSchema: mongoose.Schema<any, mongoos
17
17
  versionKey: false;
18
18
  }>> & Omit<{
19
19
  createdAt?: NativeDate | null | undefined;
20
- burningSeconds?: number | null | undefined;
21
20
  code?: string | null | undefined;
21
+ burningSeconds?: number | null | undefined;
22
22
  isDefault?: boolean | null | undefined;
23
23
  } & {
24
24
  _id: mongoose.Types.ObjectId;
@@ -29,8 +29,8 @@ export declare const CandleLightingProfileDbSchema: mongoose.Schema<any, mongoos
29
29
  } | {
30
30
  [x: string]: mongoose.SchemaDefinitionProperty<any, any, mongoose.Document<unknown, {}, {
31
31
  createdAt?: NativeDate | null | undefined;
32
- burningSeconds?: number | null | undefined;
33
32
  code?: string | null | undefined;
33
+ burningSeconds?: number | null | undefined;
34
34
  isDefault?: boolean | null | undefined;
35
35
  }, {
36
36
  id: string;
@@ -38,8 +38,8 @@ export declare const CandleLightingProfileDbSchema: mongoose.Schema<any, mongoos
38
38
  versionKey: false;
39
39
  }>> & Omit<{
40
40
  createdAt?: NativeDate | null | undefined;
41
- burningSeconds?: number | null | undefined;
42
41
  code?: string | null | undefined;
42
+ burningSeconds?: number | null | undefined;
43
43
  isDefault?: boolean | null | undefined;
44
44
  } & {
45
45
  _id: mongoose.Types.ObjectId;
@@ -48,8 +48,8 @@ export declare const CandleLightingProfileDbSchema: mongoose.Schema<any, mongoos
48
48
  }> | undefined;
49
49
  }, {
50
50
  createdAt?: NativeDate | null | undefined;
51
- burningSeconds?: number | null | undefined;
52
51
  code?: string | null | undefined;
52
+ burningSeconds?: number | null | undefined;
53
53
  isDefault?: boolean | null | undefined;
54
54
  } & {
55
55
  _id: mongoose.Types.ObjectId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@candlerip/shared",
3
- "version": "0.0.127",
3
+ "version": "0.0.130",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": "=22.19.0"