tablevu 0.2.6 → 0.2.7
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/dist/odata.js +1 -1
- package/package.json +1 -1
- package/src/odata.js +56 -14
package/dist/odata.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import get from"./get.js";function filter(q){function fuval(q){if(q===null){return"null"}else if(typeof q==="string"){return"'"+q+"'"}else{if(typeof q==="number")return q;else{
|
|
1
|
+
import get from"./get.js";function filter(q){function fuval(q){if(q===null){return"null"}else if(typeof q==="string"){if(q.length==10&&q[4]=="-"&&q[7]=="-"&&Date.parse(q)){return q}else return"'"+q+"'"}else{if(typeof q==="number")return q;else{if(q.getMonth&&typeof q.getMonth==="function")return q.toISOString();else return q.func+"("+q.params.join(",")+")"}}}let s="";if(q&&q.filters)for(let i=0;i<q.filters.length;i++){if(i>0)s=s+` ${q.type||"and"} `;if(q.filters[i].column){if(q.filters[i].operator=="in")s=s+fuval(q.filters[i].value)+" "+q.filters[i].operator+" "+q.filters[i].column;else s=s+q.filters[i].column+" "+q.filters[i].operator+" "+fuval(q.filters[i].value)}else{if(q.filters[i].func)s=s+q.filters[i].func+"("+q.filters[i].params.map(m=>fuval(m)).join(",")+")";else if(q.filters[i])s=s+"("+filter(q.filters[i])+")"}}return s}function sefs(url,query,seperator){let r="";if(query.select&&query.select.length>0){if(seperator)r+="$select="+query.select.join(",");else url.searchParams.append("$select",query.select.join(","))}if(query.expand&&query.expand.length>0){let a=[];query.expand.forEach(e=>{let q=sefs(null,e,";");if(q)a.push(e.name+"("+q+")");else a.push(e.name)});if(seperator)r+=(r.length>0?seperator:"")+"$expand="+a.join(",");else url.searchParams.append("$expand",a.join(","))}return r}export default class odata{constructor(req,data,events){this.adapter=props=>{const u=new URL(req.url);if(props.pageSize){u.searchParams.append("$skip",props.pageSize*(props.currPage-1));u.searchParams.append("$top",props.pageSize)}sefs(u,data);let f=[];if(props.filter&&Object.prototype.hasOwnProperty.call(props.filter,"filters")){let l=filter(props.filter);if(l)f.push(l)}if(props.filter){let l=filter(props.filter);if(l)f.push(l)}if(props.search){let l=props.cols.filter(q=>q.searchable==undefined||q.searchable==true).map(q=>"contains("+q.name.replaceAll(".","/")+",'"+props.search+"')").join(" or ");if(l)f.push("("+l+")")}if(f&&f.length>0)u.searchParams.append("$filter",f.join(" and "));if(props.sort&&props.sort.length>0)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(","));if(props.count)u.searchParams.append("$count",true);let r=null;if(events&&events.onfetch&&typeof events.onfetch=="function")r=events.onfetch(new Request(u,req));else r=new Request(u,req);return get(r)}}}
|
package/package.json
CHANGED
package/src/odata.js
CHANGED
|
@@ -6,12 +6,14 @@ function filter(q) {
|
|
|
6
6
|
if (q === null) {
|
|
7
7
|
return 'null'
|
|
8
8
|
} else if (typeof(q) === 'string') {
|
|
9
|
-
|
|
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 + '\'';
|
|
10
13
|
} else {
|
|
11
14
|
if (typeof(q) === 'number')
|
|
12
15
|
return q;
|
|
13
16
|
else {
|
|
14
|
-
debugger;
|
|
15
17
|
if (q.getMonth && typeof q.getMonth === "function")
|
|
16
18
|
return q.toISOString()
|
|
17
19
|
else
|
|
@@ -44,25 +46,59 @@ function filter(q) {
|
|
|
44
46
|
return s;
|
|
45
47
|
}
|
|
46
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
|
+
|
|
47
80
|
export default class odata {
|
|
48
|
-
constructor(
|
|
81
|
+
constructor(req, data, events) {
|
|
49
82
|
this.adapter = (props) => {
|
|
50
|
-
const u = new URL(url);
|
|
83
|
+
const u = new URL(req.url);
|
|
51
84
|
|
|
52
85
|
// Paging //
|
|
53
86
|
if (props.pageSize) {
|
|
54
87
|
u.searchParams.append('$skip', props.pageSize * (props.currPage - 1));
|
|
55
88
|
u.searchParams.append('$top', props.pageSize);
|
|
56
89
|
}
|
|
57
|
-
|
|
90
|
+
|
|
91
|
+
sefs(u, data);
|
|
92
|
+
/*
|
|
58
93
|
// Select //
|
|
59
|
-
if (
|
|
60
|
-
u.searchParams.append('$select',
|
|
94
|
+
if (data.select && data.select.length > 0)
|
|
95
|
+
u.searchParams.append('$select', data.select.join(','));
|
|
61
96
|
|
|
62
97
|
// Expand //
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
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
|
+
}*/
|
|
66
102
|
// Filter //
|
|
67
103
|
let f = [];
|
|
68
104
|
|
|
@@ -88,16 +124,22 @@ export default class odata {
|
|
|
88
124
|
|
|
89
125
|
if (f && f.length > 0)
|
|
90
126
|
u.searchParams.append('$filter', f.join(' and '));
|
|
91
|
-
|
|
127
|
+
|
|
92
128
|
// Sort //
|
|
93
129
|
if (props.sort && props.sort.length > 0)
|
|
94
|
-
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) + (m.dir ? (' ' + m.dir): '') }).join(','));
|
|
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(','));
|
|
95
131
|
|
|
96
132
|
// Count //
|
|
97
133
|
if (props.count)
|
|
98
134
|
u.searchParams.append('$count', true);
|
|
99
135
|
|
|
100
|
-
|
|
136
|
+
let r = null;
|
|
137
|
+
if (events && events.onfetch && typeof events.onfetch == 'function')
|
|
138
|
+
r = events.onfetch(new Request(u, req))
|
|
139
|
+
else
|
|
140
|
+
r = new Request(u, req);
|
|
141
|
+
|
|
142
|
+
return get(r);
|
|
101
143
|
}
|
|
102
144
|
}
|
|
103
|
-
}
|
|
145
|
+
}
|