tablevu 0.1.3 → 0.2.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.
package/src/odata.js CHANGED
@@ -1,99 +1,141 @@
1
- /*
2
- Props: {
3
- url: '',
4
-
5
- pageSize
6
- currPage
7
- select
8
- expand
9
- filter
10
- search
11
-
12
- }
13
- */
14
- function filter(q) {
15
-
16
- function fuval(q) {
17
- if (typeof(q) === 'string') {
18
- return '\'' + q + '\'';
19
- } else {
20
- if (typeof(q) === 'number')
21
- return q;
22
- else
23
- return q.func + '(' + q.params.join(',') + ')';
24
- }
25
- }
26
-
27
- let s = '';
28
-
29
- if (q && q.filters)
30
- for (let i = 0; i < q.filters.length; i++) {
31
- if (i > 0)
32
- s = s + ` ${q.type || 'and'} `;
33
-
34
- if (q.filters[i].column) {
35
- if (q.filters[i].operator == 'in')
36
- s = s + fuval(q.filters[i].value) + ' ' + q.filters[i].operator + ' ' + q.filters[i].column;
37
- else
38
- s = s + q.filters[i].column + ' ' + q.filters[i].operator + ' ' + fuval(q.filters[i].value);
39
- } else {
40
- if (q.filters[i].func)
41
- s = s + q.filters[i].func + '(' + q.filters[i].params.map(m => fuval(m)).join(',') + ')';
42
- else
43
- if (q.filters[i])
44
- s = s + '(' + filter(q.filters[i]) + ')';
45
- }
46
- }
47
-
48
- return s;
49
- }
50
-
51
- function odata(options, props) {
52
- const url = new URL(options.url);
53
-
54
- // Paging //
55
- if (props.pageSize) {
56
- url.searchParams.append('$skip', props.pageSize * (props.currPage - 1));
57
- url.searchParams.append('$top', props.pageSize);
58
- }
59
-
60
- // Select //
61
- if (options.select && options.select.length > 0)
62
- url.searchParams.append('$select', options.select.join(','));
63
-
64
- // Expand //
65
- if (options.expand && options.expand.length > 0)
66
- url.searchParams.append('$expand', options.expand.map(m => m.name + ((m.select && m.select.length > 0) ? '($select=' + m.select.join(',') + ')' : '')).join(','));
67
-
68
- // Filter //
69
- let f = [];
70
-
71
- if (options.filter && Object.prototype.hasOwnProperty.call(options.filter, 'filters')) {
72
- let l = filter(options.filter);
73
- f.push(l);
74
- }
75
-
76
- if (props.filter) {
77
- let l = filter(props.filter);
78
- f.push(l);
79
- }
80
-
81
- // Search //
82
- if (props.search) {
83
- let l = props.cols.filter(q => q.searchable == undefined || q.searchable == true).map(q => "contains(" + q.name + ",'" + props.search + "')").join(" or ");
84
-
85
- if (l)
86
- f.push('(' + l + ')');
87
- }
88
-
89
- if (f && f.length > 0)
90
- url.searchParams.append('$filter', f.join(' and '));
91
-
92
- // Sort //
93
- if (props.sort && props.sort.length > 0)
94
- url.searchParams.append('$orderby', props.sort.map(m => { let n = { index: -1, name: '', dir: null }; if (Object.prototype.hasOwnProperty.call(m, 'index')) { n.index = m.index } else { n.name = m.name }; n.dir = m.dir; return n; }).map(m => { return (m.index > -1 ? props.cols[m.index].name : m.name) + (m.dir ? (' ' + m.dir): '') }).join(','));
95
-
96
- return new Request(url, options.header);
97
- }
98
-
99
- export default odata;
1
+ import get from './get.js';
2
+
3
+ function filter(q) {
4
+
5
+ function fuval(q) {
6
+ if (q === null) {
7
+ return 'null'
8
+ } else if (typeof(q) === 'string') {
9
+ if (q.length == 10 && q[4] == '-' && q[7] == '-' && Date.parse(q)) { // input date return iso string
10
+ return q;
11
+ } else
12
+ return '\'' + q + '\'';
13
+ } else {
14
+ if (typeof(q) === 'number')
15
+ return q;
16
+ else {
17
+ if (q.getMonth && typeof q.getMonth === "function")
18
+ return q.toISOString()
19
+ else
20
+ return q.func + '(' + q.params.join(',') + ')';
21
+ }
22
+ }
23
+ }
24
+
25
+ let s = '';
26
+
27
+ if (q && q.filters)
28
+ for (let i = 0; i < q.filters.length; i++) {
29
+ if (i > 0)
30
+ s = s + ` ${q.type || 'and'} `;
31
+
32
+ if (q.filters[i].column) {
33
+ if (q.filters[i].operator == 'in')
34
+ s = s + fuval(q.filters[i].value) + ' ' + q.filters[i].operator + ' ' + q.filters[i].column;
35
+ else
36
+ s = s + q.filters[i].column + ' ' + q.filters[i].operator + ' ' + fuval(q.filters[i].value);
37
+ } else {
38
+ if (q.filters[i].func)
39
+ s = s + q.filters[i].func + '(' + q.filters[i].params.map(m => fuval(m)).join(',') + ')';
40
+ else
41
+ if (q.filters[i])
42
+ s = s + '(' + filter(q.filters[i]) + ')';
43
+ }
44
+ }
45
+
46
+ return s;
47
+ }
48
+
49
+ function sefs(url, query, seperator) {
50
+ let r = '';
51
+
52
+ if (query.select && query.select.length > 0) {
53
+ if (seperator)
54
+ r += '$select=' + query.select.join(',');
55
+ else
56
+ url.searchParams.append('$select', query.select.join(','));
57
+ };
58
+
59
+ if (query.expand && query.expand.length > 0) {
60
+ let a = [];
61
+
62
+ query.expand.forEach(e => {
63
+ let q = sefs(null, e, ';');
64
+
65
+ if (q)
66
+ a.push(e.name + '(' + q + ')');
67
+ else
68
+ a.push(e.name);
69
+ });
70
+
71
+ if (seperator)
72
+ r += (r.length > 0 ? seperator: '') + '$expand=' + a.join(',');
73
+ else
74
+ url.searchParams.append('$expand', a.join(','));
75
+ };
76
+
77
+ return r;
78
+ }
79
+
80
+ export default class odata {
81
+ constructor(req, data, events) {
82
+ this.adapter = (props) => {
83
+ const u = new URL(req.url);
84
+
85
+ // Paging //
86
+ if (props.pageSize) {
87
+ u.searchParams.append('$skip', props.pageSize * (props.currPage - 1));
88
+ u.searchParams.append('$top', props.pageSize);
89
+ }
90
+
91
+ sefs(u, data);
92
+ /*
93
+ // Select //
94
+ if (data.select && data.select.length > 0)
95
+ u.searchParams.append('$select', data.select.join(','));
96
+
97
+ // Expand //
98
+ if (data.expand && data.expand.length > 0) {
99
+ const a =
100
+ u.searchParams.append('$expand', data.expand.map(m => m.name + ((m.select && m.select.length > 0) ? '($select=' + m.select.join(',') + ')' : '')).join(','));
101
+ }*/
102
+ // Filter //
103
+ let f = [];
104
+
105
+ if (props.filter && Object.prototype.hasOwnProperty.call(props.filter, 'filters')) {
106
+ let l = filter(props.filter);
107
+ if (l)
108
+ f.push(l);
109
+ }
110
+
111
+ if (props.filter) {
112
+ let l = filter(props.filter);
113
+ if (l)
114
+ f.push(l);
115
+ }
116
+
117
+ // Search //
118
+ if (props.search) {
119
+ let l = props.cols.filter(q => q.searchable == undefined || q.searchable == true).map(q => "contains(" + q.name.replaceAll('.', '/') + ",'" + props.search + "')").join(" or ");
120
+
121
+ if (l)
122
+ f.push('(' + l + ')');
123
+ }
124
+
125
+ if (f && f.length > 0)
126
+ u.searchParams.append('$filter', f.join(' and '));
127
+
128
+ // Sort //
129
+ if (props.sort && props.sort.length > 0)
130
+ u.searchParams.append('$orderby', props.sort.map(m => { let n = { index: -1, name: '', dir: null }; if (Object.prototype.hasOwnProperty.call(m, 'index')) { n.index = m.index } else { n.name = m.name }; n.dir = m.dir; return n; }).map(m => { return (m.index > -1 ? props.cols[m.index].name : m.name).replaceAll('.', '/') + (m.dir ? (' ' + m.dir): '') }).join(','));
131
+
132
+ let r = null;
133
+ if (events.onfetch && typeof events.onfetch == 'function')
134
+ r = events.onfetch(new Request(u, req))
135
+ else
136
+ r = new Request(u, req);
137
+
138
+ return get(r);
139
+ }
140
+ }
141
+ }