isite 2023.1.10 → 2023.8.10

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/app.js ADDED
@@ -0,0 +1,379 @@
1
+ module.exports = function init(____0) {
2
+ ____0.connectApp = function (_app) {
3
+ if (typeof _app === 'string') {
4
+ _app = {
5
+ name: _app,
6
+ };
7
+ }
8
+ let app = {
9
+ name: _app.name,
10
+ dir: _app.dir,
11
+ page: _app.page,
12
+ collectionName: _app.collectionName || _app.name,
13
+ title: _app.title || _app.name,
14
+ images: _app.images ?? false,
15
+ allowMemory: _app.allowMemory ?? false,
16
+ memoryList: [],
17
+ allowCache: false,
18
+ cacheList: [],
19
+ allowRoute: true,
20
+ allowRouteGet: true,
21
+ allowRouteAdd: true,
22
+ allowRouteUpdate: true,
23
+ allowRouteDelete: true,
24
+ allowRouteView: true,
25
+ allowRouteAll: true,
26
+ };
27
+ if (!app.page && app.dir) {
28
+ app.page = _app.dir + '/site_files/html/index.html';
29
+ }
30
+
31
+ app.$collection = ____0.connectCollection(app.collectionName);
32
+
33
+ app.init = function () {
34
+ if (app.allowMemory) {
35
+ app.$collection.findMany({}, (err, docs) => {
36
+ if (!err) {
37
+ if (docs.length == 0) {
38
+ app.cacheList.forEach((_item, i) => {
39
+ app.$collection.add(_item, (err, doc) => {
40
+ if (!err && doc) {
41
+ app.memoryList.push(doc);
42
+ }
43
+ });
44
+ });
45
+ } else {
46
+ docs.forEach((doc) => {
47
+ app.memoryList.push(doc);
48
+ });
49
+ }
50
+ }
51
+ });
52
+ }
53
+ };
54
+ app.add = function (_item, callback) {
55
+ app.$collection.add(_item, (err, doc) => {
56
+ if (callback) {
57
+ callback(err, doc);
58
+ }
59
+ if (app.allowMemory && !err && doc) {
60
+ app.memoryList.push(doc);
61
+ }
62
+ });
63
+ };
64
+ app.update = function (_item, callback) {
65
+ app.$collection.edit(
66
+ {
67
+ where: {
68
+ id: _item.id,
69
+ },
70
+ set: _item,
71
+ },
72
+ (err, result) => {
73
+ if (callback) {
74
+ callback(err, result);
75
+ }
76
+ if (app.allowMemory && !err && result) {
77
+ let index = app.memoryList.findIndex((itm) => itm.id === result.doc.id);
78
+ if (index !== -1) {
79
+ app.memoryList[index] = result.doc;
80
+ } else {
81
+ app.memoryList.push(result.doc);
82
+ }
83
+ } else if (app.allowCache && !err && result) {
84
+ let index = app.cacheList.findIndex((itm) => itm.id === result.doc.id);
85
+ if (index !== -1) {
86
+ app.cacheList[index] = result.doc;
87
+ } else {
88
+ app.cacheList.push(result.doc);
89
+ }
90
+ }
91
+ }
92
+ );
93
+ };
94
+ app.delete = function (_item, callback) {
95
+ app.$collection.delete(
96
+ {
97
+ id: _item.id,
98
+ },
99
+ (err, result) => {
100
+ if (callback) {
101
+ callback(err, result);
102
+ }
103
+ if (app.allowMemory && !err && result.count === 1) {
104
+ let index = app.memoryList.findIndex((a) => a.id === _item.id);
105
+ if (index !== -1) {
106
+ app.memoryList.splice(index, 1);
107
+ }
108
+ } else if (app.allowCache && !err && result.count === 1) {
109
+ let index = app.cacheList.findIndex((a) => a.id === _item.id);
110
+ if (index !== -1) {
111
+ app.cacheList.splice(index, 1);
112
+ }
113
+ }
114
+ }
115
+ );
116
+ };
117
+ app.view = function (_item, callback) {
118
+ if (callback) {
119
+ if (app.allowMemory) {
120
+ if ((item = app.memoryList.find((itm) => itm.id == _item.id))) {
121
+ callback(null, item);
122
+ return;
123
+ }
124
+ } else if (app.allowCache) {
125
+ if ((item = app.cacheList.find((itm) => itm.id == _item.id))) {
126
+ callback(null, item);
127
+ return;
128
+ }
129
+ }
130
+
131
+ app.$collection.find({ id: _item.id }, (err, doc) => {
132
+ callback(err, doc);
133
+
134
+ if (!err && doc) {
135
+ if (app.allowMemory) {
136
+ app.memoryList.push(doc);
137
+ } else if (app.allowCache) {
138
+ app.cacheList.push(doc);
139
+ }
140
+ }
141
+ });
142
+ }
143
+ };
144
+ app.all = function (_options, callback) {
145
+ if (callback) {
146
+ if (app.allowMemory) {
147
+ callback(null, app.memoryList);
148
+ } else {
149
+ app.$collection.findMany(_options, callback);
150
+ }
151
+ }
152
+ };
153
+
154
+ app.handleRequest = function (req, res, callback) {
155
+ if (callback) {
156
+ callback({
157
+ data: {
158
+ appName: req.word(app.title),
159
+ setting: ____0.setting,
160
+ },
161
+ });
162
+ }
163
+ };
164
+
165
+ app.api = function (_api, callback) {
166
+ _api.name = _api.name || 'test';
167
+ _api.url = _api.url || `/api/${app.name}/${_api.name}`;
168
+ _api.type = (_api.type || 'POST').toLowerCase();
169
+ _api.permissions = _api.permissions || ['login'];
170
+
171
+ _api.callback =
172
+ _api.callback ||
173
+ callback ||
174
+ function (req, res) {
175
+ res.json({
176
+ done: true,
177
+ data: {
178
+ ...req.data,
179
+ UserInfo: req.getUserFinger(),
180
+ },
181
+ });
182
+ };
183
+ if (_api.type == 'post') {
184
+ if (_api.path) {
185
+ ____0.onPOST({ name: _api.url, path: _api.path, overwrite: true, require: { permissions: _api.permissions } });
186
+ } else {
187
+ ____0.onPOST({ name: _api.url, overwrite: true, require: { permissions: _api.permissions } }, _api.callback);
188
+ }
189
+ } else {
190
+ if (_api.path) {
191
+ ____0.onGET({ name: _api.url, path: _api.path, overwrite: true, require: { permissions: _api.permissions } });
192
+ } else {
193
+ ____0.onGET({ name: _api.url, overwrite: true, require: { permissions: _api.permissions } }, _api.callback);
194
+ }
195
+ }
196
+ };
197
+
198
+ if (app.allowRoute) {
199
+ if (app.allowRouteGet) {
200
+ if (app.dir && app.images) {
201
+ app.api({
202
+ type: 'get',
203
+ url: 'images',
204
+ path: app.dir + '/site_files/images',
205
+ });
206
+ }
207
+ if (app.page) {
208
+ app.api(
209
+ {
210
+ type: 'get',
211
+ url: app.name,
212
+ },
213
+ (req, res) => {
214
+ app.handleRequest(req, res, (handle) => {
215
+ res.render(app.page, handle.data, { parser: 'html', compres: true });
216
+ });
217
+ }
218
+ );
219
+ }
220
+ }
221
+
222
+ if (app.allowRouteAdd) {
223
+ app.api(
224
+ {
225
+ name: 'add',
226
+ },
227
+ (req, res) => {
228
+ let response = {
229
+ done: false,
230
+ };
231
+
232
+ let _data = req.data;
233
+
234
+ _data.addUserInfo = req.getUserFinger();
235
+
236
+ app.add(_data, (err, doc) => {
237
+ if (!err && doc) {
238
+ response.done = true;
239
+ response.doc = doc;
240
+ } else {
241
+ response.error = err.mesage;
242
+ }
243
+ res.json(response);
244
+ });
245
+ }
246
+ );
247
+ }
248
+
249
+ if (app.allowRouteUpdate) {
250
+ ____0.post({ name: `/api/${app.name}/update`, require: { permissions: ['login'] } }, (req, res) => {
251
+ let response = {
252
+ done: false,
253
+ };
254
+
255
+ let _data = req.data;
256
+ _data.editUserInfo = req.getUserFinger();
257
+
258
+ app.update(_data, (err, result) => {
259
+ if (!err) {
260
+ response.done = true;
261
+ response.result = result;
262
+ } else {
263
+ response.error = err.message;
264
+ }
265
+ res.json(response);
266
+ });
267
+ });
268
+ }
269
+
270
+ if (app.allowRouteDelete) {
271
+ ____0.post({ name: `/api/${app.name}/delete`, require: { permissions: ['login'] } }, (req, res) => {
272
+ let response = {
273
+ done: false,
274
+ };
275
+ let _data = req.data;
276
+
277
+ app.delete(_data, (err, result) => {
278
+ if (!err && result.count === 1) {
279
+ response.done = true;
280
+ response.result = result;
281
+ } else {
282
+ response.error = err?.message || 'Deleted Not Exists';
283
+ }
284
+ res.json(response);
285
+ });
286
+ });
287
+ }
288
+
289
+ if (app.allowRouteView) {
290
+ ____0.post({ name: `/api/${app.name}/view`, public: true }, (req, res) => {
291
+ let response = {
292
+ done: false,
293
+ };
294
+
295
+ let _data = req.data;
296
+ app.view(_data, (err, doc) => {
297
+ if (!err && doc) {
298
+ response.done = true;
299
+ response.doc = doc;
300
+ } else {
301
+ response.error = err?.message || 'Not Exists';
302
+ }
303
+ res.json(response);
304
+ });
305
+ });
306
+ }
307
+
308
+ if (app.allowRouteAll) {
309
+ ____0.post({ name: `/api/${app.name}/all`, public: true }, (req, res) => {
310
+ let where = req.body.where || {};
311
+ let search = req.body.search || '';
312
+ let limit = req.body.limit || 50;
313
+ let select = req.body.select || {};
314
+
315
+ if (search) {
316
+ where.$or = [];
317
+
318
+ where.$or.push({
319
+ id: ____0.get_RegExp(search, 'i'),
320
+ });
321
+
322
+ where.$or.push({
323
+ code: ____0.get_RegExp(search, 'i'),
324
+ });
325
+
326
+ where.$or.push({
327
+ nameAr: ____0.get_RegExp(search, 'i'),
328
+ });
329
+
330
+ where.$or.push({
331
+ nameEn: ____0.get_RegExp(search, 'i'),
332
+ });
333
+ }
334
+
335
+ if (app.allowMemory) {
336
+ if (!search) {
337
+ search = 'id';
338
+ }
339
+ let docs = [];
340
+ let list = app.memoryList.filter((g) => JSON.stringify(g).contains(search)).slice(0, limit);
341
+ list.forEach((doc) => {
342
+ if (doc) {
343
+ let obj = {
344
+ ...doc,
345
+ $memory: true,
346
+ };
347
+ if (Object.keys(select).length > 0) {
348
+ for (const p in obj) {
349
+ if (!Object.hasOwnProperty.call(select, p)) {
350
+ delete obj[p];
351
+ }
352
+ }
353
+ }
354
+
355
+ docs.push(obj);
356
+ }
357
+ });
358
+ res.json({
359
+ done: true,
360
+ list: docs,
361
+ count: docs.length,
362
+ });
363
+ } else {
364
+ app.all({ where, select, limit }, (err, docs) => {
365
+ res.json({
366
+ done: true,
367
+ list: docs,
368
+ });
369
+ });
370
+ }
371
+ });
372
+ }
373
+ }
374
+
375
+ app.init();
376
+ ____0.addApp(app);
377
+ return app;
378
+ };
379
+ };
package/lib/collection.js CHANGED
@@ -12,11 +12,18 @@ module.exports = function init(____0, option, db) {
12
12
  $collection.collection = option;
13
13
  $collection.db = db || ____0.options.mongodb.db;
14
14
  }
15
+
15
16
  $collection.options = { ...____0.options.mongodb, ...option };
17
+ $collection.options.db = $collection.options.db.trim().replace(' ', '');
18
+ $collection.options.collection = $collection.options.collection.trim().replace(' ', '');
16
19
  $collection.identityEnabled = $collection.options.identity.enabled;
17
-
18
- $collection.db = $collection.options.db.trim().replace(' ', '');
19
- $collection.collection = $collection.options.collection.trim().replace(' ', '');
20
+ $collection.name = $collection.options.db + '.' + $collection.options.collection;
21
+ $collection.guid = ____0.hide($collection.options);
22
+ if ((co = ____0.collectionList.find((c) => c.guid == $collection.guid))) {
23
+ return co;
24
+ }
25
+ $collection.db = $collection.options.db;
26
+ $collection.collection = $collection.options.collection;
20
27
  $collection.docs = [];
21
28
 
22
29
  $collection.busy = !1;
@@ -232,7 +239,7 @@ module.exports = function init(____0, option, db) {
232
239
 
233
240
  delete options.where.$req;
234
241
  delete options.where.$res;
235
-
242
+
236
243
  ____0.mongodb.deleteOne(
237
244
  {
238
245
  collectionName: $collection.collection,
@@ -860,5 +867,6 @@ module.exports = function init(____0, option, db) {
860
867
  ____0.mongodb.collections_indexed[$collection.collection] = { nextID: 1 };
861
868
  }
862
869
 
870
+ ____0.collectionList.push($collection);
863
871
  return $collection;
864
872
  };
package/lib/data.js CHANGED
@@ -104,6 +104,7 @@ module.exports = function init(____0) {
104
104
  wav: 'audio/x-wav',
105
105
  wiz: 'application/msword',
106
106
  wsdl: 'application/xml',
107
+ webp: 'image/webp',
107
108
  xbm: 'image/x-xbitmap',
108
109
  xlb: 'application/vnd.ms-excel',
109
110
  xls: 'application/vnd.ms-excel',