@xuda.io/xuda-datasource-db-adapter-module 1.0.0

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.
Files changed (2) hide show
  1. package/index.mjs +519 -0
  2. package/package.json +8 -0
package/index.mjs ADDED
@@ -0,0 +1,519 @@
1
+ const _this = {};
2
+
3
+ export const init_module = (e) => {
4
+ _this.func = e.func;
5
+ _this.glb = e.glb;
6
+ _this.SESSION_OBJ = e.SESSION_OBJ;
7
+ _this.APP_OBJ = e.APP_OBJ;
8
+ _this.IS_DOCKER = e.IS_DOCKER;
9
+ _this.IS_API_SERVER = e.IS_API_SERVER;
10
+ _this.IS_PROCESS_SERVER = e.IS_PROCESS_SERVER;
11
+ };
12
+
13
+ export const build_filter = async function (
14
+ SESSION_ID,
15
+ dataSourceSession,
16
+ v,
17
+ _ds
18
+ ) {
19
+ let file_ret2 = await _this.func.utils.FILES_OBJ.get(
20
+ SESSION_ID,
21
+ _ds._dataSourceTableId
22
+ );
23
+ if (!file_ret2) {
24
+ _this.func.utils.debug_report(
25
+ SESSION_ID,
26
+ "datasource build query",
27
+ "Table reference not exist",
28
+ "E"
29
+ );
30
+ return;
31
+ }
32
+ let view_ret = await _this.func.utils.VIEWS_OBJ.get(
33
+ SESSION_ID,
34
+ _ds.args.prog_id
35
+ );
36
+ v.dataSourceIndexId = view_ret.progDataSource.dataSourceIndexId; // get index id
37
+ v.dataSourceIndexIdExp = view_ret.progDataSource.dataSourceIndexIdExp; // get index exp
38
+ if (v.dataSourceIndexIdExp) {
39
+ let ret_dataSourceIndexIdExp = await _this.func.expression.get(
40
+ SESSION_ID,
41
+ v.dataSourceIndexIdExp,
42
+ dataSourceSession,
43
+ "index",
44
+ _ds.args.rowIdP
45
+ );
46
+ if (ret_dataSourceIndexIdExp.result) {
47
+ v.dataSourceIndexId = ret_dataSourceIndexIdExp.result;
48
+ }
49
+ if (!v.dataSourceIndexId) {
50
+ // issue alert expression returned empty value
51
+ _this.func.utils.debug_report(
52
+ SESSION_ID,
53
+ "BUILD FILTER QUERY",
54
+ "Index expression return empty result",
55
+ "W"
56
+ );
57
+ }
58
+ v.dataSourceIndexesObj = view_ret.progDataSource.dataSourceIndexesObj;
59
+
60
+ const index_obj = _this.func.common.find_item_by_key_root(
61
+ file_ret2.tableIndexes,
62
+ "id",
63
+ v.dataSourceIndexId
64
+ );
65
+
66
+ if (!index_obj) {
67
+ return _this.func.utils.debug_report(
68
+ SESSION_ID,
69
+ "BUILD FILTER QUERY",
70
+ "Index not found in table: " + _ds._dataSourceTableId,
71
+ "W"
72
+ );
73
+ }
74
+
75
+ // var index_keys = index_obj.data.keys;
76
+
77
+ if (!v.dataSourceIndexesObj?.[v.dataSourceIndexId]) {
78
+ _this.func.utils.debug_report(
79
+ SESSION_ID,
80
+ "BUILD FILTER QUERY",
81
+ "Index not found",
82
+ "W"
83
+ );
84
+ }
85
+
86
+ // v.viewIndex = {};
87
+ // _.forEach(index_keys, function (val, key) {
88
+ // var item = v.dataSourceIndexesObj[v.dataSourceIndexId][val];
89
+ // v.viewIndex[key] = {
90
+ // [val]: {
91
+ // from: item.from,
92
+ // to: item.to,
93
+ // locate_from: item.locate_from,
94
+ // locate_to: item.locate_to,
95
+ // },
96
+ // };
97
+ // });
98
+ }
99
+ var filter_from = {};
100
+ var filter_to = {};
101
+ let ret = await _this.func.utils.VIEWS_OBJ.get(SESSION_ID, _ds.args.prog_id);
102
+ var sortOrder = ret.progDataSource.dataSourceSort; // get ascending/descending
103
+ var sortOrderTypeExp = ret.progDataSource.dataSourceSortExp; // get ascending/descending
104
+ if (_ds.sortOrder) {
105
+ sortOrder = _ds.sortOrder;
106
+ }
107
+ if (_ds.sortOrderTypeExp) {
108
+ sortOrderTypeExp = _ds.sortOrderTypeExp;
109
+ }
110
+ v.sortOrder = sortOrder;
111
+ v.sortOrderTypeExp = sortOrderTypeExp;
112
+ if (sortOrderTypeExp) {
113
+ var sort_orderExp = await _this.func.expression.get(
114
+ SESSION_ID,
115
+ v.sortOrderTypeExp,
116
+ dataSourceSession,
117
+ "sortorder",
118
+ _ds.args.rowIdP
119
+ );
120
+ if (sort_orderExp.result) {
121
+ v.sortOrder = sort_orderExp.result;
122
+ sortOrder = v.sortOrder;
123
+ }
124
+ }
125
+ let file_ret = await _this.func.utils.FILES_OBJ.get(
126
+ SESSION_ID,
127
+ _ds._dataSourceTableId
128
+ );
129
+
130
+ if (v.dataSourceIndexId) {
131
+ // index definition exist
132
+ _ds.filter_from = {};
133
+ _ds.filter_to = {};
134
+ _ds.locate_from = {};
135
+ _ds.locate_to = {};
136
+ var locate_exist;
137
+ var locate_error;
138
+ if (v.viewIndex) {
139
+ for await (const [keySegment, valSegment] of Object.entries(
140
+ v.viewIndex[v.dataSourceIndexId]
141
+ )) {
142
+ // run on segments rows
143
+ var fieldId = keySegment; // key segment
144
+
145
+ if (
146
+ !_this.func.common.find_item_by_key(
147
+ file_ret.tableFields,
148
+ "field_id",
149
+ fieldId
150
+ )
151
+ ) {
152
+ _this.func.utils.debug_report(
153
+ SESSION_ID,
154
+ "datasource build query",
155
+ `Wrong index fields for ${fieldId} at field range ${v.viewSourceDesc}`,
156
+ "E",
157
+ null,
158
+ _ds
159
+ );
160
+ break;
161
+ }
162
+
163
+ var fieldProp = _this.func.common.find_item_by_key(
164
+ file_ret.tableFields,
165
+ "field_id",
166
+ fieldId
167
+ );
168
+
169
+ var fieldType = fieldProp.props.fieldType;
170
+ var fieldValFrom = valSegment.from; // key val or fieldID from
171
+ var fieldValTo = valSegment.to; // key val or fieldID to
172
+ const get_val = async function (valP, actionP, typeP) {
173
+ var ret = {};
174
+ ret.result = valP;
175
+ ret.fields = {};
176
+ ret.fields[fieldId] = valP;
177
+ if (!ret.result) {
178
+ ret.result = 0;
179
+ if (fieldType !== "number") {
180
+ // ret.result = '""'; // removed Aug 27 2024
181
+ ret.result = "";
182
+ ret.fields[fieldId] = ret.result;
183
+ }
184
+ } else {
185
+ if (fieldType === "number") {
186
+ if (!valP.includes("@")) {
187
+ if (typeof valP === "boolean") {
188
+ ret.result = valP ? 1 : 0;
189
+ } else {
190
+ ret.result = Number(valP);
191
+ }
192
+
193
+ ret.fields[fieldId] = ret.result;
194
+ }
195
+ } else {
196
+ if (!valP.includes("@")) {
197
+ ret.result = valP;
198
+ ret.fields[fieldId] = ret.result;
199
+ }
200
+ }
201
+ ret = await _this.func.expression.get(
202
+ SESSION_ID,
203
+ ret.result,
204
+ dataSourceSession,
205
+ "query",
206
+ _ds.args.rowIdP
207
+ );
208
+ }
209
+ _this.func.utils.debug.log(
210
+ SESSION_ID,
211
+ _ds.args.prog_id + "_" + valSegment.id,
212
+ {
213
+ module: v.viewModule,
214
+ action: actionP,
215
+ prop: typeP,
216
+ details: ret.result,
217
+ result: ret.result,
218
+ error: ret.error,
219
+ warning: ret.warning,
220
+ source: v.viewSourceDesc,
221
+ fields: ret.fields,
222
+ type: "query",
223
+ prog_id: _ds.args.prog_id,
224
+ dsSession: dataSourceSession,
225
+ }
226
+ );
227
+ return ret;
228
+ };
229
+ var value_from = await get_val(fieldValFrom, "range", "from");
230
+ var value_to = await get_val(fieldValTo, "range", "to");
231
+ v.segFrom.push(value_from.result);
232
+ v.segTo.push(value_to.result);
233
+ var add_filters_to_datasource = function (objP, typeP) {
234
+ // later use for refresh purpose
235
+ for (let [key, val] of Object.entries(objP.fields)) {
236
+ if (!val) val = null;
237
+ _ds[typeP][key] = val; //"@" +
238
+ }
239
+ };
240
+ add_filters_to_datasource(value_from, "filter_from");
241
+ add_filters_to_datasource(value_to, "filter_to");
242
+ filter_from[fieldId] = value_from.result;
243
+ filter_to[fieldId] = value_to.result;
244
+ if (_ds.tree_obj.menuType === "component") {
245
+ // locate
246
+ v.segLocateFrom[keySegment] = v.segFrom[keySegment]; // put defaults from range
247
+ v.segLocateTo[keySegment] = v.segTo[keySegment]; // put defaults from range
248
+ // linkTypeLocate = valSegment.data[6]; // value/field
249
+ v.fieldLocateValFrom = valSegment.locate_from; // key val or fieldID from
250
+ v.fieldLocateValTo = valSegment.locate_to; // key val or fieldID to
251
+ if (valSegment.locate_from || valSegment.locate_to)
252
+ locate_exist = true;
253
+ var value_from_locate = await get_val(
254
+ v.fieldLocateValFrom,
255
+ "locate",
256
+ "from"
257
+ );
258
+ var value_to_locate = await get_val(
259
+ v.fieldLocateValTo,
260
+ "locate",
261
+ "to"
262
+ );
263
+ //
264
+ v.segLocateFrom[keySegment] = value_from_locate.result;
265
+ v.segLocateTo[keySegment] = value_to_locate.result;
266
+ add_filters_to_datasource(value_from_locate, "locate_from");
267
+ add_filters_to_datasource(value_to_locate, "locate_to");
268
+ }
269
+ }
270
+ }
271
+ const query_string_helper = function (
272
+ fileIdP,
273
+ indexIdP,
274
+ segFromP,
275
+ segToP,
276
+ sortOrderP,
277
+ filter_from,
278
+ filter_to
279
+ ) {
280
+ var query = {};
281
+ var from = [fileIdP, indexIdP, segFromP];
282
+ var to = [fileIdP, indexIdP, segToP];
283
+ query.desc = sortOrderP === "des";
284
+ query.view = "db_index";
285
+ query.table_id = fileIdP;
286
+ query.indexId = indexIdP;
287
+ if (sortOrderP === "des") {
288
+ query.startkey = to;
289
+ query.endkey = from;
290
+ } else {
291
+ query.startkey = from;
292
+ query.endkey = to;
293
+ }
294
+ query.filter_from = filter_from;
295
+ query.filter_to = filter_to;
296
+ query.sortOrder = sortOrderP;
297
+ return query;
298
+ };
299
+
300
+ {
301
+ // regular filter
302
+ v.couchView = query_string_helper(
303
+ _ds._dataSourceTableId,
304
+ v.dataSourceIndexId,
305
+ v.segFrom,
306
+ v.segTo,
307
+ sortOrder,
308
+ filter_from,
309
+ filter_to
310
+ );
311
+ }
312
+ // locate
313
+ if (
314
+ _ds.tree_obj.menuType === "component" &&
315
+ locate_exist &&
316
+ !locate_error
317
+ ) {
318
+ v.locateQuery = query_string_helper(
319
+ _ds._dataSourceTableId,
320
+ v.dataSourceIndexId,
321
+ v.segLocateFrom,
322
+ v.segLocateTo,
323
+ sortOrder,
324
+ _ds.locate_from,
325
+ _ds.locate_to
326
+ );
327
+ }
328
+ } else {
329
+ // index definition missing
330
+ v.couchView = {
331
+ view: "db_index_all",
332
+ table_id: _ds._dataSourceTableId,
333
+ };
334
+ }
335
+ };
336
+
337
+ export const process_view_dataset = async function (
338
+ SESSION_ID,
339
+ dataSourceSession,
340
+ _ds
341
+ ) {
342
+ const tree_obj = await _this.func.utils.TREE_OBJ.get(SESSION_ID, _ds.prog_id);
343
+ var rows_count = _ds?.v.raw_data?.rows?.length || 0;
344
+ // var skip;
345
+ var args = _ds.args;
346
+ const finish_dataset = async function () {
347
+ _ds.rows_found = rows_count;
348
+ // new condition "rows" added to prevent after record if no rows found 2021 09 17
349
+ if (
350
+ tree_obj.menuType === "get_data" &&
351
+ rows_count &&
352
+ (await _this.func.datasource.get_view_events_count(
353
+ SESSION_ID,
354
+ dataSourceSession,
355
+ "after_record"
356
+ )) > 0
357
+ ) {
358
+ await _this.func.datasource.execute_view_events(
359
+ SESSION_ID,
360
+ dataSourceSession,
361
+ "after_record"
362
+ );
363
+ }
364
+ return _this.func.datasource.callback(
365
+ SESSION_ID,
366
+ dataSourceSession,
367
+ args.dataSourceNoP,
368
+ args.rowIdP,
369
+ args.jobNoP,
370
+ null,
371
+ _ds.nodeId
372
+ );
373
+ };
374
+ const iterate_rows = async function () {
375
+ // _ds.currentRecordId = "dataset";
376
+ for await (const [key, raw_data_row] of Object.entries(
377
+ _ds.v.raw_data.rows
378
+ )) {
379
+ _ds.currentRecordId = raw_data_row.id;
380
+ await _this.func.datasource.render_fields_dataset(
381
+ SESSION_ID,
382
+ dataSourceSession,
383
+ raw_data_row
384
+ );
385
+
386
+ break;
387
+ }
388
+ return await finish_dataset();
389
+ };
390
+
391
+ if (rows_count) {
392
+ return await iterate_rows();
393
+ }
394
+
395
+ // Alert record not found
396
+
397
+ _ds.currentRecordId = "dataset";
398
+ _ds.set_mode = "C";
399
+ await _this.func.datasource.render_fields_dataset(
400
+ SESSION_ID,
401
+ dataSourceSession
402
+ );
403
+ await _this.func.events.validate(
404
+ SESSION_ID,
405
+ "record_not_found",
406
+ dataSourceSession
407
+ );
408
+ _ds.record_not_found = true;
409
+ return await finish_dataset();
410
+ };
411
+
412
+ export const set_dataset_key_data = function (
413
+ SESSION_ID,
414
+ dataSourceSession,
415
+ valP
416
+ ) {
417
+ var _ds = _this.SESSION_OBJ[SESSION_ID].DS_GLB[dataSourceSession];
418
+ var v = _ds.v;
419
+ v.udfData = valP.value.udfData;
420
+ var keyData = {
421
+ _id: valP.value._id,
422
+ _rev: valP.value._rev,
423
+ table_id: _ds._dataSourceTableId,
424
+ };
425
+ _ds.key = keyData;
426
+ };
427
+
428
+ export const get_locate_from_db = async function (
429
+ SESSION_ID,
430
+ dataSourceSession
431
+ ) {
432
+ var _ds = _this.SESSION_OBJ[SESSION_ID].DS_GLB[dataSourceSession];
433
+ var v = _ds.v;
434
+
435
+ if (!v.locateQuery) {
436
+ return;
437
+ }
438
+ // from DB
439
+ const json = await _this.func.db.get_query(
440
+ SESSION_ID,
441
+ _ds._dataSourceTableId,
442
+ v.locateQuery,
443
+ dataSourceSession,
444
+ v.viewSourceDesc,
445
+ "locate record"
446
+ );
447
+ var rows = json.rows.length;
448
+
449
+ if (!rows) {
450
+ return await _this.func.events.validate(
451
+ SESSION_ID,
452
+ "locate_not_found",
453
+ dataSourceSession
454
+ );
455
+ }
456
+ _ds.locatedRecordId = json.rows[0].id;
457
+ return;
458
+ };
459
+
460
+ export const find_fieldId_in_index = function (
461
+ SESSION_ID,
462
+ dataSourceSession,
463
+ fieldId
464
+ ) {
465
+ var _ds = _this.SESSION_OBJ[SESSION_ID].DS_GLB[dataSourceSession];
466
+ var v = _ds.v;
467
+ var ret;
468
+
469
+ _.forEach(v.viewIndex, function (val, key) {
470
+ if (val.data[0] === fieldId) {
471
+ ret = key;
472
+ return false;
473
+ }
474
+ });
475
+ return ret;
476
+ };
477
+
478
+ export const dataset_save_before_exit = async function (
479
+ SESSION_ID,
480
+ dataSourceSession,
481
+ reEnterP
482
+ ) {
483
+ var exit_event_count = 0;
484
+ if (!reEnterP) {
485
+ exit_event_count = await _this.func.datasource.get_view_events_count(
486
+ SESSION_ID,
487
+ dataSourceSession,
488
+ "on_exit"
489
+ );
490
+ }
491
+ var _ds = _this.SESSION_OBJ[SESSION_ID].DS_GLB[dataSourceSession];
492
+ var v = _ds.v;
493
+ // var save_before_exit = async function () {
494
+ // if (
495
+ // v.screenInfo &&
496
+ // v.screenInfo.menuType === "set_data" &&
497
+ // _.size(v.jsonP.rows)
498
+ // ) {
499
+ // await _this.func.db.save_data(SESSION_ID, dataSourceSession, callback_dataset);
500
+ // } else {
501
+ // callback_dataset();
502
+ // }
503
+ // };
504
+ if (exit_event_count) {
505
+ await _this.func.datasource.execute_view_events(
506
+ SESSION_ID,
507
+ dataSourceSession,
508
+ "on_exit"
509
+ );
510
+
511
+ if (
512
+ v?.screenInfo?.properties?.menuType === "set_data" &&
513
+ _.size(v.jsonP.rows)
514
+ ) {
515
+ return await _this.func.db.save_data(SESSION_ID, dataSourceSession);
516
+ }
517
+ }
518
+ return;
519
+ };
package/package.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "@xuda.io/xuda-datasource-db-adapter-module",
3
+ "version": "1.0.0",
4
+ "main": "index.mjs",
5
+ "type": "module",
6
+ "description": "Auto-generated build for xuda-datasource-db-adapter-module.mjs",
7
+ "author": "Auto Publisher"
8
+ }