@candlerip/shared 0.0.129 → 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
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,18 +38,18 @@ export const CacheWorker = (environmentMode, url, password, options) => {
37
38
  data: _client,
38
39
  };
39
40
  };
40
- const getCache = async (cacheKey, objKeys) => {
41
- const { composeCustomError } = CustomErrorWorker({ info: { cacheKey, objKeys } });
41
+ const getCache = async (props) => {
42
+ const { composeCustomError } = CustomErrorWorker({ info: props });
43
+ const { cacheKey, objKeys, pathKeys } = composeGetRedisData(props);
42
44
  const { data: client, customError } = _getClient();
43
45
  if (customError) {
44
46
  return { customError };
45
47
  }
46
48
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
49
  let data;
48
- const pathKeys = objKeys?.map((it) => `$.${it}`);
49
50
  try {
50
51
  data = await client.json.get(cacheKey, { path: pathKeys });
51
- if (!data) {
52
+ if (!data || data?.length === 0) {
52
53
  return {
53
54
  customError: composeCustomError('No data in Redis'),
54
55
  };
@@ -63,7 +64,7 @@ export const CacheWorker = (environmentMode, url, password, options) => {
63
64
  let normalizedData = {};
64
65
  if (objKeys) {
65
66
  objKeys.forEach((objKey) => {
66
- normalizedData[objKey] = data[`$.${objKey}`][0];
67
+ normalizedData[objKey] = objKeys?.length === 1 ? data[0] : data[`$.${objKey}`][0];
67
68
  });
68
69
  }
69
70
  else {
@@ -78,6 +79,50 @@ export const CacheWorker = (environmentMode, url, password, options) => {
78
79
  data: normalizedData,
79
80
  };
80
81
  };
82
+ const mGetCache = async (props) => {
83
+ const { composeCustomError } = CustomErrorWorker({ info: { props } });
84
+ const getRedisData = props.map((it) => composeGetRedisData(it));
85
+ const { data: client, customError } = _getClient();
86
+ if (customError) {
87
+ return { customError };
88
+ }
89
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
90
+ let data;
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)) {
96
+ return {
97
+ customError: composeCustomError('No data in Redis'),
98
+ };
99
+ }
100
+ }
101
+ catch (err) {
102
+ return {
103
+ customError: composeCustomError('Error get data by object keys from Redis', { err }),
104
+ };
105
+ }
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
+ });
122
+ return {
123
+ data: normalizedData,
124
+ };
125
+ };
81
126
  const setCache = async (key, data) => {
82
127
  const { composeCustomError } = CustomErrorWorker({ info: { key } });
83
128
  const { data: client, customError } = _getClient();
@@ -98,6 +143,7 @@ export const CacheWorker = (environmentMode, url, password, options) => {
98
143
  };
99
144
  return {
100
145
  getCache,
146
+ mGetCache,
101
147
  setCache,
102
148
  };
103
149
  };
@@ -1,13 +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 { 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;
8
+ mGetCache: MGetCache;
9
9
  setCache: SetCache;
10
10
  };
11
11
  export type GetClient = () => TCustomError<ReturnType<typeof createClient>>;
12
- export type GetCache = <T extends CommonPageDictionaryCacheKey, D extends Cache[T]>(cacheKey: T, objKeys?: string[]) => Promise<TCustomError<D>>;
13
- export type SetCache = <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.129",
3
+ "version": "0.0.130",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": "=22.19.0"