telos-oql 1.0.4 → 1.0.5

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/oql.js +119 -105
  2. package/package.json +1 -1
package/oql.js CHANGED
@@ -5,6 +5,12 @@ let utils = {
5
5
  context.operation = context.operation != null ? context.operation : {};
6
6
  context.filters = context.filters != null ? context.filters : [];
7
7
 
8
+ context.operation.type =
9
+ context.operation.type != null ? context.operation.type : "read";
10
+
11
+ context.operation.data =
12
+ context.operation.data != null ? context.operation.data : [];
13
+
8
14
  return context;
9
15
  },
10
16
  normalizeValue: (value) => {
@@ -25,147 +31,155 @@ module.exports = [
25
31
  {
26
32
  process: (context, args) => {
27
33
 
28
- if(context.local.operator != "access")
29
- return null;
30
-
31
- return JSON.stringify(utils.normalizeContext({
32
- access: {
33
- url: utils.normalizeValue(args[0]),
34
- options: args[1] != null ?
35
- JSON.parse("" + args[1]) :
36
- null
37
- },
38
- operation: {
39
- type: "read"
40
- }
41
- }));
34
+ return context.local.operator == "access" ?
35
+ JSON.stringify(utils.normalizeContext({
36
+ access: {
37
+ url: utils.normalizeValue(args[0]),
38
+ options: args[1] != null ?
39
+ JSON.parse("" + args[1]) :
40
+ null
41
+ }
42
+ })) :
43
+ null;
42
44
  },
43
45
  tags: ["oql", "access"]
44
46
  },
45
47
  {
46
48
  process: (context, args) => {
47
49
 
48
- if(context.local.operator != "append")
49
- return null;
50
-
51
- if(!args[0].startsWith("{"))
52
- return null; // STUB
53
-
54
- let data = utils.normalizeContext(JSON.parse(args[0]));
55
-
56
- data.operation.type = "create"
57
- data.operation.data = args.slice(1).map(item => JSON.parse(item));
58
-
59
- return JSON.stringify(data);
50
+ return context.local.operator == "append" ?
51
+ `(Array.isArray(${
52
+ args[0]
53
+ })?([${
54
+ args.slice(1).map(item => `(${item})`).join(",")
55
+ }].forEach(item=>{(${
56
+ args[0]
57
+ }).push(item);}):((context)=>{
58
+ context.operation.type="create";
59
+ ${
60
+ args.slice(1).map(
61
+ item => `context.operation.data.push(${item});`
62
+ ).join("")
63
+ }return context;})(${
64
+ args[0]
65
+ }))` :
66
+ null;
60
67
  },
61
68
  tags: ["oql", "append"]
62
69
  },
63
70
  {
64
71
  process: (context, args) => {
65
72
 
66
- if(context.local.operator != "at")
67
- return null;
68
-
69
- if(!args[0].startsWith("{"))
70
- return `${args[0]}[${args[1]}]`;
71
-
72
- let data = utils.normalizeContext(JSON.parse(args[0]));
73
-
74
- args.slice(1).forEach(item => {
75
-
76
- data.filters.push({
77
- type: "at",
78
- options: {
79
- value: utils.normalizeValue(item)
80
- }
81
- });
82
- });
83
-
84
- return JSON.stringify(data);
73
+ return context.local.operator == "at" ?
74
+ `(Array.isArray(${
75
+ args[0]
76
+ })?((${
77
+ args[0]
78
+ })${
79
+ args.slice(1).map(item => `[${item}]`).join("")
80
+ }):((context)=>{${
81
+ args.slice(1).map(item =>
82
+ `context.filters.push({type:"at",options:{value:(${
83
+ item
84
+ })}});`
85
+ ).join("")
86
+ }return context;})(${
87
+ args[0]
88
+ }))` :
89
+ null;
85
90
  },
86
91
  tags: ["oql", "at"]
87
92
  },
88
93
  {
89
94
  process: (context, args) => {
90
95
 
91
- if(context.local.operator != "filter")
92
- return null;
93
-
94
- let data = utils.normalizeContext(JSON.parse(args[0]));
95
-
96
- data.filters.push({
97
- type: "filter",
98
- options: {
99
- value: context.local.list[2]
100
- }
101
- });
102
-
103
- return JSON.stringify(data);
96
+ return context.local.operator == "filter" ?
97
+ `(((context)=>{context.filters.push(
98
+ {type:"filter",options:{value:(
99
+ ${
100
+ JSON.stringify(context.local.list[2])
101
+ })}});return context;})(${args[0]}))` :
102
+ null;
104
103
  },
105
104
  tags: ["oql", "filter"]
106
105
  },
107
106
  {
108
107
  process: (context, args) => {
109
108
 
110
- if(context.local.operator != "remove")
111
- return null;
112
-
113
- if(!args[0].startsWith("{"))
114
- return null; // STUB
115
-
116
- let data = utils.normalizeContext(JSON.parse(args[0]));
117
- data.operation.type = "delete"
118
-
119
- return JSON.stringify(data);
109
+ return context.local.operator == "remove" ?
110
+ `(Array.isArray(${
111
+ args[0]
112
+ })?((${args[0]}).splice(${args[1], 1}))${
113
+ args.slice(1).map(item => `[${item}]`).join("")
114
+ }):(((context)=>{
115
+ context.operation.type="delete";return context;
116
+ })(${
117
+ args[0]
118
+ })))` :
119
+ null;
120
120
  },
121
121
  tags: ["oql", "remove"]
122
122
  },
123
123
  {
124
124
  process: (context, args) => {
125
125
 
126
- if(context.local.operator != "set")
127
- return null;
128
-
129
- if(!args[0].startsWith("{")) {
130
-
131
- return context.state[args[0]] != null ?
132
- `${args[0]}=context.state[${args[0]}];` :
133
- `${
134
- args[0]
135
- }=${
136
- args[1]
137
- },context.state["${
138
- args[0]
139
- }"]=${
140
- args[0]
141
- };`
142
- }
143
-
144
- let data = utils.normalizeContext(JSON.parse(args[0]));
145
-
146
- data.operation.type = "update"
147
- data.operation.data = args.slice(1).map(item => JSON.parse(item));
148
-
149
- return JSON.stringify(data);
126
+ return context.local.operator == "set" ?
127
+ `((((item)=>{
128
+ if(item==null)return true;
129
+ return !(typeof item =="object"&&!Array.isArray(item));
130
+ })(typeof (${
131
+ args[0]
132
+ })=="undefined"?null:(${
133
+ args[0]
134
+ })))?(()=>{${
135
+ context.state[args[0]] != null ?
136
+ `${args[0]}=context.state[${args[0]}];` :
137
+ ""
138
+ }(1, eval)(${
139
+ JSON.stringify(
140
+ context.state[args[0]] == null ?
141
+ `${
142
+ args[0]
143
+ }=${
144
+ args[1]
145
+ };` :
146
+ ""
147
+ )
148
+ });${
149
+ /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(args[0]) ?
150
+ `context.state["${
151
+ args[0]
152
+ }"]=${
153
+ args[0]
154
+ };` :
155
+ ""
156
+ }})():((context)=>{
157
+ context.operation.type="update";
158
+ ${
159
+ args.slice(1).map(
160
+ item => `context.operation.data.push(${item});`
161
+ ).join("")
162
+ }return context;})(${
163
+ args[0]
164
+ }))\n` :
165
+ null;
150
166
  },
151
167
  tags: ["oql", "set"]
152
168
  },
153
169
  {
154
170
  process: (context, args) => {
155
171
 
156
- if(context.local.operator != "sort")
157
- return null;
158
-
159
- let data = utils.normalizeContext(JSON.parse(args[0]));
160
-
161
- data.filters.push({
162
- type: "sort",
163
- options: {
164
- value: JSON.parse(args[1])
165
- }
166
- });
167
-
168
- return JSON.stringify(data);
172
+ return context.local.operator == "sort" ?
173
+ `(Array.isArray(${
174
+ args[0]
175
+ })?((${args[0]}).sort()):(((context)=>{
176
+ context.filters.push({type:"at",options:{value:(${
177
+ args[1]
178
+ })}});return context;
179
+ })(${
180
+ args[0]
181
+ })))` :
182
+ null;
169
183
  },
170
184
  tags: ["oql", "sort"]
171
185
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telos-oql",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "A LISP dialect for database-agnostic querying",
5
5
  "main": "oql.json",
6
6
  "dependencies": {