@candlerip/shared 0.0.129 → 0.0.131

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-get-redis-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,19 @@ 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 get = async (props) => {
42
+ const { cacheKey } = props;
43
+ const { composeCustomError } = CustomErrorWorker({ info: props });
44
+ const { objKeys, pathKeys } = composeGetRedisData(props);
42
45
  const { data: client, customError } = _getClient();
43
46
  if (customError) {
44
47
  return { customError };
45
48
  }
46
49
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
50
  let data;
48
- const pathKeys = objKeys?.map((it) => `$.${it}`);
49
51
  try {
50
52
  data = await client.json.get(cacheKey, { path: pathKeys });
51
- if (!data) {
53
+ if (!data || data?.length === 0) {
52
54
  return {
53
55
  customError: composeCustomError('No data in Redis'),
54
56
  };
@@ -63,7 +65,7 @@ export const CacheWorker = (environmentMode, url, password, options) => {
63
65
  let normalizedData = {};
64
66
  if (objKeys) {
65
67
  objKeys.forEach((objKey) => {
66
- normalizedData[objKey] = data[`$.${objKey}`][0];
68
+ normalizedData[objKey] = objKeys?.length === 1 ? data[0] : data[`$.${objKey}`][0];
67
69
  });
68
70
  }
69
71
  else {
@@ -78,7 +80,51 @@ export const CacheWorker = (environmentMode, url, password, options) => {
78
80
  data: normalizedData,
79
81
  };
80
82
  };
81
- const setCache = async (key, data) => {
83
+ const gets = async (props) => {
84
+ const { composeCustomError } = CustomErrorWorker({ info: { props } });
85
+ const getRedisData = props.map((it) => composeGetRedisData(it));
86
+ const { data: client, customError } = _getClient();
87
+ if (customError) {
88
+ return { customError };
89
+ }
90
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
+ let data;
92
+ try {
93
+ const promises = getRedisData.map((it) => client.json.get(it.cacheKey, { path: it.pathKeys }));
94
+ data = await Promise.all(promises);
95
+ console.error('***', data);
96
+ if (!data || data.some((it) => it.length === 0)) {
97
+ return {
98
+ customError: composeCustomError('No data in Redis'),
99
+ };
100
+ }
101
+ }
102
+ catch (err) {
103
+ return {
104
+ customError: composeCustomError('Error get data by object keys from Redis', { err }),
105
+ };
106
+ }
107
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
108
+ const normalizedData = {};
109
+ getRedisData.forEach((it, index) => {
110
+ const { objKeys } = it;
111
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
112
+ let normalizedCacheKeyData = {};
113
+ if (objKeys) {
114
+ objKeys.forEach((objKey) => {
115
+ normalizedCacheKeyData[objKey] = objKeys?.length === 1 ? data[index][0] : data[index][`$.${objKey}`][0];
116
+ });
117
+ }
118
+ else {
119
+ normalizedCacheKeyData = data[index];
120
+ }
121
+ normalizedData[it.cacheKey] = normalizedCacheKeyData;
122
+ });
123
+ return {
124
+ data: normalizedData,
125
+ };
126
+ };
127
+ const set = async (key, data) => {
82
128
  const { composeCustomError } = CustomErrorWorker({ info: { key } });
83
129
  const { data: client, customError } = _getClient();
84
130
  if (customError) {
@@ -97,7 +143,8 @@ export const CacheWorker = (environmentMode, url, password, options) => {
97
143
  };
98
144
  };
99
145
  return {
100
- getCache,
101
- setCache,
146
+ get,
147
+ gets,
148
+ set,
102
149
  };
103
150
  };
@@ -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
- getCache: GetCache;
9
- setCache: SetCache;
7
+ get: Get;
8
+ gets: Gets;
9
+ set: Set;
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 Set = <T extends keyof Cache>(key: T, data: Cache[T]) => Promise<TCustomError<null>>;
13
+ export type Get = <T extends CacheData = never, C extends CacheKey = CacheKey>(props: GetProps<C>) => Promise<TCustomError<T>>;
14
+ export type GetProps<C extends CacheKey> = C extends CandleLightingProfilesCacheKey ? {
15
+ cacheKey: C;
16
+ objKey?: ObjKey;
17
+ } : {
18
+ cacheKey: C;
19
+ objKeys?: string[];
20
+ };
21
+ export type Gets = <T extends {
22
+ [key in CacheKey]?: CacheData;
23
+ } = never, C extends CacheKey = CacheKey>(props: GetProps<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.131",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": "=22.19.0"