-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_sql.ts
More file actions
123 lines (112 loc) · 3.11 KB
/
node_sql.ts
File metadata and controls
123 lines (112 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import * as fs from "fs";
export function FROM(data: any) {
if (typeof data === "object") {
return data;
} else if (typeof data === "string") {
let item = fs.readFileSync(data);
item = JSON.parse(item.toString());
return item;
} else {
return new Promise(async (resolve) => {
await data().then((_data) => {
console.log(_data);
resolve(_data);
});
});
}
}
export function SELECT(
args: Array<string | Function> | string,
FromFunc: any,
whereFunc: any = undefined,
groupByFunc: any = undefined
) {
let obj = {};
let temp = null;
if (!Array.isArray(args)) {
args = args.split(",").map((x) => x.trim());
}
if (typeof FromFunc === "undefined") {
console.error("Need to pass a from function");
} else if (typeof FromFunc === "object") {
temp = FromFunc;
} else {
temp = FromFunc();
}
if (typeof whereFunc !== "undefined" && typeof whereFunc === "function") {
temp = whereFunc(temp);
}
if (temp != undefined && (args.length === 0 || args[0] === "*")) {
if (Array.isArray(temp) && temp.length > 0) args = Object.keys(temp[0]);
else args = Object.keys(temp);
}
if (typeof temp === undefined) {
return null;
} else if (Array.isArray(temp)) {
let arr = [];
for (const __item of temp) {
let tobj = {};
for (const key of args) {
const split = typeof key === 'function' ? key.name : key.split(" ");
let newKey = split.length > 1 ? split[1] : split[0];
if(typeof key === 'string')
tobj[newKey] = GetChildValue(split[0], __item);
else {
const [res , name] = GetEvalValue({...__item ,...tobj} , key)
tobj[name] = res;
}
}
arr.push(tobj);
}
obj = arr;
} else {
//Single Object
for (const key of args) {
const split = typeof key === 'function' ? key.name : key.split(" ");
let newKey = split.length > 1 ? split[1] : split[0];
// obj[newKey] = GetChildValue(split[0], temp);
if(typeof key === 'string')
obj[newKey] = GetChildValue(split[0], temp);
else {
const [res , name] = GetEvalValue({...temp , ...obj} , key)
obj[name] = res
}
}
}
if (typeof groupByFunc !== "undefined" && typeof groupByFunc === "function") {
obj = groupByFunc(obj);
}
return obj;
}
export function WHERE(predicate: any) {
return function (data) {
return data.filter(predicate);
};
}
function GetKeys(str: string) {
if (str.indexOf(" ") > -1) {
return [str, str];
} else {
return [str.split(" ")[0], str.split(" ")[1]];
}
}
export function GROUPBY(reducerFunc: any, accumulator: any) {
return function (data) {
return data.reduce(reducerFunc, accumulator);
};
}
function GetChildValue(key: string, item) {
let retItem = item;
const keys = key.split(".");
for (const _key of keys) {
const split = _key.split(" ");
let newKey = split.length > 1 ? split[1] : split[0];
retItem = retItem[newKey];
}
return retItem;
}
function GetEvalValue(row:any, func:Function){
const name = func.name;
const res = func(row);
return [res , name];
}