zet-lib 1.4.0 → 1.4.1
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/lib/zCache.js +174 -170
- package/package.json +1 -1
package/lib/zCache.js
CHANGED
|
@@ -1,301 +1,305 @@
|
|
|
1
|
-
const myCache = require(
|
|
2
|
-
const connection = require(
|
|
3
|
-
const Util = require(
|
|
4
|
-
const path = require(
|
|
5
|
-
const fs = require(
|
|
6
|
-
const directory =
|
|
7
|
-
const zRoute = require(
|
|
1
|
+
const myCache = require("./cache");
|
|
2
|
+
const connection = require("./connection");
|
|
3
|
+
const Util = require("./Util");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const fs = require("fs-extra");
|
|
6
|
+
const directory = "./";
|
|
7
|
+
const zRoute = require("./zRoute");
|
|
8
8
|
|
|
9
|
-
const zCache = {}
|
|
9
|
+
const zCache = {};
|
|
10
10
|
|
|
11
11
|
//constants
|
|
12
12
|
zCache.KEYS = {
|
|
13
|
-
ROUTES:
|
|
14
|
-
MYMODELS:
|
|
15
|
-
ROLES:
|
|
16
|
-
CONFIG:
|
|
17
|
-
MENU:
|
|
18
|
-
MENU_SYSTEMS:
|
|
19
|
-
ZFUNCTIONS:
|
|
20
|
-
VERSIONS:
|
|
21
|
-
MODELS_RELATIONS:
|
|
22
|
-
APPROVAL_LEVELS:
|
|
23
|
-
}
|
|
13
|
+
ROUTES: "ROUTES",
|
|
14
|
+
MYMODELS: "MYMODELS",
|
|
15
|
+
ROLES: "ROLES",
|
|
16
|
+
CONFIG: "CONFIG",
|
|
17
|
+
MENU: "MENU",
|
|
18
|
+
MENU_SYSTEMS: "MENU_SYSTEMS",
|
|
19
|
+
ZFUNCTIONS: "ZFUNCTIONS",
|
|
20
|
+
VERSIONS: "VERSIONS",
|
|
21
|
+
MODELS_RELATIONS: "MODELS_RELATIONS",
|
|
22
|
+
APPROVAL_LEVELS: "APPROVAL_LEVELS",
|
|
23
|
+
};
|
|
24
24
|
|
|
25
|
-
zCache.STATICS = zCache.KEYS
|
|
26
|
-
zCache.myCache = myCache
|
|
25
|
+
zCache.STATICS = zCache.KEYS;
|
|
26
|
+
zCache.myCache = myCache;
|
|
27
27
|
|
|
28
|
-
zCache.get = (name =
|
|
28
|
+
zCache.get = (name = "") => {
|
|
29
29
|
if (!myCache.has(name)) {
|
|
30
|
-
zCache[name]()
|
|
30
|
+
zCache[name]();
|
|
31
31
|
} else {
|
|
32
|
-
return myCache.get(name)
|
|
32
|
+
return myCache.get(name);
|
|
33
33
|
}
|
|
34
|
-
}
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
zCache.set = (X, Y) => {
|
|
37
|
-
myCache.set(X, Y)
|
|
38
|
-
}
|
|
37
|
+
myCache.set(X, Y);
|
|
38
|
+
};
|
|
39
39
|
|
|
40
40
|
zCache.cache = async (req, res, next) => {
|
|
41
|
-
await cache()
|
|
42
|
-
next()
|
|
43
|
-
}
|
|
41
|
+
await cache();
|
|
42
|
+
next();
|
|
43
|
+
};
|
|
44
44
|
|
|
45
45
|
const cache = async () => {
|
|
46
|
-
zCache.VERSIONS()
|
|
47
|
-
let i = 0
|
|
48
|
-
if (myCache.has(
|
|
46
|
+
zCache.VERSIONS();
|
|
47
|
+
let i = 0;
|
|
48
|
+
if (myCache.has("ROLES")) {
|
|
49
49
|
//console.log("cache ROLES has exist " + i)
|
|
50
|
-
i = i + 1
|
|
50
|
+
i = i + 1;
|
|
51
51
|
} else {
|
|
52
52
|
//console.log("cache ROLES not exist " + i)
|
|
53
|
-
await zCache.ROLES()
|
|
53
|
+
await zCache.ROLES();
|
|
54
54
|
}
|
|
55
|
-
if (myCache.has(
|
|
56
|
-
i = i + 1
|
|
55
|
+
if (myCache.has("ROUTES")) {
|
|
56
|
+
i = i + 1;
|
|
57
57
|
} else {
|
|
58
|
-
await zCache.ROUTES()
|
|
58
|
+
await zCache.ROUTES();
|
|
59
59
|
}
|
|
60
|
-
if (myCache.has(
|
|
61
|
-
i = i + 1
|
|
60
|
+
if (myCache.has("MYMODELS")) {
|
|
61
|
+
i = i + 1;
|
|
62
62
|
} else {
|
|
63
|
-
await zCache.MYMODELS()
|
|
63
|
+
await zCache.MYMODELS();
|
|
64
64
|
}
|
|
65
|
-
if (myCache.has(
|
|
66
|
-
i = i + 1
|
|
65
|
+
if (myCache.has("MENU")) {
|
|
66
|
+
i = i + 1;
|
|
67
67
|
} else {
|
|
68
68
|
//console.log("cache MENU not exist " + i);
|
|
69
69
|
//zup.menu();
|
|
70
|
-
await zCache.MENU()
|
|
70
|
+
await zCache.MENU();
|
|
71
71
|
}
|
|
72
|
-
if (myCache.has(
|
|
72
|
+
if (myCache.has("MENU_SYSTEMS")) {
|
|
73
73
|
//i = i + 1;
|
|
74
74
|
} else {
|
|
75
|
-
await zCache.MENU_SYSTEMS()
|
|
75
|
+
await zCache.MENU_SYSTEMS();
|
|
76
76
|
}
|
|
77
77
|
//console.log(i)
|
|
78
|
-
if (myCache.has(
|
|
78
|
+
if (myCache.has("ZFUNCTIONS")) {
|
|
79
79
|
//i = i + 1;
|
|
80
80
|
} else {
|
|
81
|
-
await zCache.ZFUNCTIONS()
|
|
81
|
+
await zCache.ZFUNCTIONS();
|
|
82
82
|
}
|
|
83
|
-
if (myCache.has(
|
|
84
|
-
i = i + 1
|
|
83
|
+
if (myCache.has("APPROVAL_LEVELS")) {
|
|
84
|
+
i = i + 1;
|
|
85
85
|
} else {
|
|
86
|
-
await zCache.APPROVAL_LEVELS()
|
|
86
|
+
await zCache.APPROVAL_LEVELS();
|
|
87
87
|
}
|
|
88
88
|
//await zCache.CURRENCIES();
|
|
89
89
|
//console.log(i)
|
|
90
90
|
if (i < 5) {
|
|
91
|
-
await cache()
|
|
91
|
+
await cache();
|
|
92
92
|
} else {
|
|
93
|
-
return i
|
|
93
|
+
return i;
|
|
94
94
|
}
|
|
95
95
|
//cache relations on startup
|
|
96
|
-
await zCache.MODELS_RELATIONS()
|
|
97
|
-
}
|
|
96
|
+
await zCache.MODELS_RELATIONS();
|
|
97
|
+
};
|
|
98
98
|
|
|
99
99
|
zCache.renew = async () => {
|
|
100
|
-
zCache.VERSIONS()
|
|
101
|
-
await zCache.ROLES()
|
|
102
|
-
await zCache.MENU()
|
|
103
|
-
await zCache.CONFIG()
|
|
104
|
-
await zCache.ROUTES()
|
|
105
|
-
await zCache.MYMODELS()
|
|
106
|
-
await zCache.MENU_SYSTEMS()
|
|
107
|
-
await zCache.MODELS_RELATIONS()
|
|
108
|
-
await zCache.APPROVAL_LEVELS()
|
|
109
|
-
}
|
|
100
|
+
zCache.VERSIONS();
|
|
101
|
+
await zCache.ROLES();
|
|
102
|
+
await zCache.MENU();
|
|
103
|
+
await zCache.CONFIG();
|
|
104
|
+
await zCache.ROUTES();
|
|
105
|
+
await zCache.MYMODELS();
|
|
106
|
+
await zCache.MENU_SYSTEMS();
|
|
107
|
+
await zCache.MODELS_RELATIONS();
|
|
108
|
+
await zCache.APPROVAL_LEVELS();
|
|
109
|
+
};
|
|
110
110
|
|
|
111
111
|
zCache.ROUTES = () => {
|
|
112
|
-
if (myCache.has(
|
|
113
|
-
return myCache.get(
|
|
112
|
+
if (myCache.has("ROUTES")) {
|
|
113
|
+
return myCache.get("ROUTES");
|
|
114
114
|
} else {
|
|
115
|
-
const routes = zRoute.ROUTES()
|
|
116
|
-
myCache.set(
|
|
117
|
-
return routes
|
|
115
|
+
const routes = zRoute.ROUTES();
|
|
116
|
+
myCache.set("ROUTES", routes);
|
|
117
|
+
return routes;
|
|
118
118
|
}
|
|
119
|
-
}
|
|
119
|
+
};
|
|
120
120
|
|
|
121
121
|
zCache.MYMODELS = () => {
|
|
122
|
-
if (myCache.has(
|
|
123
|
-
return myCache.get(
|
|
122
|
+
if (myCache.has("MYMODELS")) {
|
|
123
|
+
return myCache.get("MYMODELS");
|
|
124
124
|
} else {
|
|
125
|
-
const models = zRoute.MYMODELS()
|
|
126
|
-
myCache.set(
|
|
127
|
-
return models
|
|
125
|
+
const models = zRoute.MYMODELS();
|
|
126
|
+
myCache.set("MYMODELS", models);
|
|
127
|
+
return models;
|
|
128
128
|
}
|
|
129
|
-
}
|
|
129
|
+
};
|
|
130
130
|
|
|
131
131
|
zCache.ROLES = async () => {
|
|
132
|
-
let r = 0
|
|
133
|
-
let results = await connection.results({ table:
|
|
134
|
-
let obj = Util.arrayToObject(results,
|
|
135
|
-
await myCache.set(
|
|
132
|
+
let r = 0;
|
|
133
|
+
let results = await connection.results({ table: "zrole" });
|
|
134
|
+
let obj = Util.arrayToObject(results, "id");
|
|
135
|
+
await myCache.set("ROLES", obj);
|
|
136
136
|
//add to version after set
|
|
137
|
-
zCache.updateVersions(
|
|
138
|
-
r = 1
|
|
139
|
-
return r
|
|
140
|
-
}
|
|
137
|
+
zCache.updateVersions("ROLES");
|
|
138
|
+
r = 1;
|
|
139
|
+
return r;
|
|
140
|
+
};
|
|
141
141
|
|
|
142
142
|
zCache.ZFUNCTIONS = async () => {
|
|
143
143
|
let query = await connection.results({
|
|
144
|
-
table:
|
|
144
|
+
table: "zfunction",
|
|
145
145
|
where: {
|
|
146
146
|
active: 1,
|
|
147
147
|
},
|
|
148
|
-
})
|
|
148
|
+
});
|
|
149
149
|
if (query.length) {
|
|
150
|
-
let results = await connection.results({ table:
|
|
151
|
-
let obj = Util.arrayToObject(results,
|
|
152
|
-
await myCache.set(
|
|
150
|
+
let results = await connection.results({ table: "zfunction" });
|
|
151
|
+
let obj = Util.arrayToObject(results, "title");
|
|
152
|
+
await myCache.set("ZFUNCTIONS", obj);
|
|
153
153
|
//add to version after set
|
|
154
|
-
zCache.updateVersions(
|
|
154
|
+
zCache.updateVersions("ZFUNCTIONS");
|
|
155
155
|
}
|
|
156
|
-
}
|
|
156
|
+
};
|
|
157
157
|
|
|
158
158
|
zCache.VERSIONS = () => {
|
|
159
|
-
if (myCache.has(
|
|
159
|
+
if (myCache.has("VERSIONS")) {
|
|
160
160
|
//console.log("VERSIONS")
|
|
161
|
-
return myCache.get(
|
|
161
|
+
return myCache.get("VERSIONS");
|
|
162
162
|
} else {
|
|
163
|
-
let obj = {}
|
|
163
|
+
let obj = {};
|
|
164
164
|
for (let key in zCache.KEYS) {
|
|
165
|
-
obj[key] = 0
|
|
165
|
+
obj[key] = 0;
|
|
166
166
|
}
|
|
167
|
-
myCache.set(
|
|
167
|
+
myCache.set("VERSIONS", obj);
|
|
168
168
|
//console.log(JSON.stringify(myCache.get("VERSIONS")))
|
|
169
|
-
return obj
|
|
169
|
+
return obj;
|
|
170
170
|
}
|
|
171
|
-
}
|
|
171
|
+
};
|
|
172
172
|
|
|
173
173
|
zCache.MENU = async () => {
|
|
174
174
|
let results = async () => {
|
|
175
175
|
return await connection.results({
|
|
176
|
-
table:
|
|
176
|
+
table: "zmenu",
|
|
177
177
|
where: {
|
|
178
178
|
active: 1,
|
|
179
179
|
},
|
|
180
|
-
})
|
|
181
|
-
}
|
|
182
|
-
const r = await results()
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
const r = await results();
|
|
183
183
|
const builder = async () => {
|
|
184
|
-
let obj = {}
|
|
185
|
-
let arr = await results()
|
|
184
|
+
let obj = {};
|
|
185
|
+
let arr = await results();
|
|
186
186
|
arr.map((item) => {
|
|
187
|
-
obj[item.company_id] = item.json || []
|
|
188
|
-
})
|
|
189
|
-
myCache.set(
|
|
187
|
+
obj[item.company_id] = item.json || [];
|
|
188
|
+
});
|
|
189
|
+
myCache.set("MENU", obj);
|
|
190
190
|
//add to version after set
|
|
191
|
-
zCache.updateVersions(
|
|
192
|
-
}
|
|
191
|
+
zCache.updateVersions("MENU");
|
|
192
|
+
};
|
|
193
193
|
if (r.length) {
|
|
194
|
-
await builder()
|
|
194
|
+
await builder();
|
|
195
195
|
}
|
|
196
|
-
}
|
|
196
|
+
};
|
|
197
197
|
|
|
198
198
|
zCache.MENU_SYSTEMS = () => {
|
|
199
|
-
let obj = {}
|
|
199
|
+
let obj = {};
|
|
200
200
|
obj.users = [
|
|
201
201
|
{
|
|
202
|
-
text:
|
|
203
|
-
href:
|
|
202
|
+
text: "LANGUAGE.users",
|
|
203
|
+
href: "zuser",
|
|
204
204
|
},
|
|
205
205
|
{
|
|
206
|
-
text:
|
|
207
|
-
href:
|
|
206
|
+
text: "LANGUAGE.access_role",
|
|
207
|
+
href: "zrole",
|
|
208
208
|
},
|
|
209
209
|
{
|
|
210
|
-
text:
|
|
211
|
-
href:
|
|
210
|
+
text: "LANGUAGE.logs_uploads",
|
|
211
|
+
href: "zlogs_upload",
|
|
212
212
|
},
|
|
213
213
|
{
|
|
214
|
-
text:
|
|
215
|
-
href:
|
|
214
|
+
text: "LANGUAGE.user_access",
|
|
215
|
+
href: "zuser_company",
|
|
216
216
|
},
|
|
217
217
|
{
|
|
218
|
-
text:
|
|
219
|
-
href:
|
|
218
|
+
text: "LANGUAGE.company",
|
|
219
|
+
href: "zcompany",
|
|
220
220
|
},
|
|
221
|
-
]
|
|
221
|
+
];
|
|
222
222
|
obj.systems = [
|
|
223
223
|
{
|
|
224
|
-
text:
|
|
225
|
-
href:
|
|
224
|
+
text: "Generator",
|
|
225
|
+
href: "zgenerator",
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
text: "Menu Generator",
|
|
229
|
+
href: "zmenu",
|
|
226
230
|
},
|
|
227
231
|
{
|
|
228
|
-
text:
|
|
229
|
-
href:
|
|
232
|
+
text: "View Generator",
|
|
233
|
+
href: "zviewgenerator",
|
|
230
234
|
},
|
|
231
235
|
{
|
|
232
|
-
text:
|
|
233
|
-
href:
|
|
236
|
+
text: "Grid Default",
|
|
237
|
+
href: "zgrid_default",
|
|
234
238
|
},
|
|
235
239
|
{
|
|
236
|
-
text:
|
|
237
|
-
href:
|
|
240
|
+
text: "Routers",
|
|
241
|
+
href: "zpage",
|
|
238
242
|
},
|
|
239
243
|
{
|
|
240
|
-
text:
|
|
241
|
-
href:
|
|
244
|
+
text: "Functions",
|
|
245
|
+
href: "zfunction",
|
|
242
246
|
},
|
|
243
247
|
{
|
|
244
|
-
text:
|
|
245
|
-
href:
|
|
248
|
+
text: "Layouts",
|
|
249
|
+
href: "zlayout",
|
|
246
250
|
},
|
|
247
251
|
{
|
|
248
|
-
text:
|
|
249
|
-
href:
|
|
252
|
+
text: "Custom Report",
|
|
253
|
+
href: "zreports",
|
|
250
254
|
},
|
|
251
255
|
{
|
|
252
|
-
text:
|
|
253
|
-
href:
|
|
256
|
+
text: "Log Errors",
|
|
257
|
+
href: "zerror",
|
|
254
258
|
},
|
|
255
|
-
]
|
|
256
|
-
myCache.set(
|
|
257
|
-
}
|
|
259
|
+
];
|
|
260
|
+
myCache.set("MENU_SYSTEMS", obj);
|
|
261
|
+
};
|
|
258
262
|
|
|
259
263
|
zCache.updateVersions = (name) => {
|
|
260
|
-
let obj = myCache.get(
|
|
264
|
+
let obj = myCache.get("VERSIONS") || {};
|
|
261
265
|
if (!obj.ROLES) {
|
|
262
|
-
zCache.VERSIONS()
|
|
263
|
-
obj = myCache.get(
|
|
266
|
+
zCache.VERSIONS();
|
|
267
|
+
obj = myCache.get("VERSIONS");
|
|
264
268
|
}
|
|
265
|
-
let value = obj[name] || 0
|
|
266
|
-
value
|
|
267
|
-
obj[name] = value
|
|
268
|
-
myCache.set(
|
|
269
|
-
}
|
|
269
|
+
let value = obj[name] || 0;
|
|
270
|
+
value++;
|
|
271
|
+
obj[name] = value;
|
|
272
|
+
myCache.set("VERSIONS", obj);
|
|
273
|
+
};
|
|
270
274
|
|
|
271
275
|
zCache.MODELS_RELATIONS = async () => {
|
|
272
276
|
if (process.env.APP_IS_CACHE == 1) {
|
|
273
|
-
console.log(
|
|
274
|
-
if (myCache.has(
|
|
275
|
-
return myCache.get(
|
|
277
|
+
console.log("cache models");
|
|
278
|
+
if (myCache.has("MODELS_RELATIONS")) {
|
|
279
|
+
return myCache.get("MODELS_RELATIONS");
|
|
276
280
|
} else {
|
|
277
|
-
const obj = await zRoute.modelsCache()
|
|
278
|
-
myCache.set(
|
|
279
|
-
return obj
|
|
281
|
+
const obj = await zRoute.modelsCache();
|
|
282
|
+
myCache.set("MODELS_RELATIONS", obj);
|
|
283
|
+
return obj;
|
|
280
284
|
}
|
|
281
285
|
} else {
|
|
282
|
-
console.log(
|
|
283
|
-
return {}
|
|
286
|
+
console.log("not cache models");
|
|
287
|
+
return {};
|
|
284
288
|
}
|
|
285
|
-
}
|
|
289
|
+
};
|
|
286
290
|
|
|
287
291
|
zCache.APPROVAL_LEVELS = async () => {
|
|
288
|
-
if (myCache.has(
|
|
289
|
-
return myCache.get(
|
|
292
|
+
if (myCache.has("APPROVAL_LEVELS")) {
|
|
293
|
+
return myCache.get("APPROVAL_LEVELS");
|
|
290
294
|
} else {
|
|
291
295
|
let results = await connection.results({
|
|
292
|
-
select:
|
|
293
|
-
table:
|
|
294
|
-
})
|
|
295
|
-
let obj = Util.arrayToObject(results,
|
|
296
|
-
myCache.set(
|
|
297
|
-
return obj
|
|
296
|
+
select: "id,name,color",
|
|
297
|
+
table: "zapproval_type",
|
|
298
|
+
});
|
|
299
|
+
let obj = Util.arrayToObject(results, "id");
|
|
300
|
+
myCache.set("APPROVAL_LEVELS", obj);
|
|
301
|
+
return obj;
|
|
298
302
|
}
|
|
299
|
-
}
|
|
303
|
+
};
|
|
300
304
|
|
|
301
|
-
module.exports = zCache
|
|
305
|
+
module.exports = zCache;
|