-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJsonRegExpGraphQueryLanguage.js
More file actions
260 lines (250 loc) · 7.06 KB
/
JsonRegExpGraphQueryLanguage.js
File metadata and controls
260 lines (250 loc) · 7.06 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"use strict"
exports.JsonRegExpGraphQueryLanguage = class JsonRegExpGraphQueryLanguage {
constructor(db, actions={}) {
this.db = db
this.actions = actions
this.operators = [
{ suffix: "?", test: (objectValue, value) => new RegExp(value).test(objectValue) },
{ suffix: ">", test: (objectValue, value) => objectValue > value },
{ suffix: "<", test: (objectValue, value) => objectValue < value },
]
this.indentation = " "
this.iterators = {}
}
copy(q) {
return JSON.parse(JSON.stringify(q))
}
copyQueryAndExtendWithIdMember(q) {
let qc = this.copy(q)
qc[`?${this.db.idMember}`] = ""
return qc
}
copyQueryAndRemoveKeys(q, keys) {
if (keys.length == 0) {
return q
}
let qc = this.copy(q)
for(let key of keys) {
delete qc[key]
}
return qc
}
async query(type, q, keyToRemove) {
if (q["!indentation"]) {
this.indentation = q["!indentation"]
}
if (q["!type"]) {
this.defaultType = q["!type"]
}
if (!type) {
type = q["!type"] || this.defaultType
}
let results = []
// multiple queries
if (q instanceof Array) {
for(let qi of q) {
results.push(this.copy(await this.query(type, qi, keyToRemove)))
}
return results
}
if (!this.db.isTypeValid(type)) {
console.info(`jrGQL.query: type (${type}) is invalid`)
return []
}
// action
if (q["!"]) {
if (q["!"] instanceof Array) {
for(let op of q["!"]) {
results.push(await this.query(type, { "!": op }, keyToRemove))
}
return results
}
for(let actionName in q["!"]) {
let parameters = q["!"][actionName]
let action = this.actions[actionName]
results.push(action? action(type, parameters) : `jrGQL.Error: action not found ${actionName}`)
}
return results
}
// create
if (q["+"]) {
return [await this.db.create(type, q["+"])]
}
// delete
let deleteResultsFromDb = false
if (q["-"]) {
q = this.copyQueryAndExtendWithIdMember(q["-"])
deleteResultsFromDb = true
}
// retrieve
if (type == "Object") {
let qc = this.copy(q)
let object = qc[this.db.idMember]
delete qc[this.db.idMember]
let ret = await this.doQuery(type, qc, object, keyToRemove, deleteResultsFromDb)
if (ret) {
results.push(ret)
} else {
return null
}
} else {
let iterator
let from = 0
if (q["!from"]) {
if (typeof q["!from"] == "string" && q["!from"].startsWith("#")) {
iterator = this.iterators[q["!from"]]
} else {
from = q["!from"]
iterator = this.db.getValues(type)
}
} else {
iterator = this.db.getValues(type)
}
let i = 0
for(let object = iterator.next(); !object.done; object = iterator.next()) {
const objectValue = await object.value
if (from instanceof Object) {
if (!await this.doQuery(type, from, objectValue)) {
i++
continue
}
} else if (i < from) {
i++
continue
}
let ret = await this.doQuery(type, q, objectValue, keyToRemove, deleteResultsFromDb)
if (!ret) {
continue
}
results.push(ret)
i++
if (q["!limit"] == i) {
let iteratorKey = "#" + Object.keys(this.iterators).length
this.iterators[iteratorKey] = iterator
ret["_cursor"] = iteratorKey
break
}
}
}
return results
}
async doQuery(type, q, object, keyToRemove, deleteResultsFromDb) {
let match = this.match(q, object)
if (!match.match) {
return null
}
q = this.copyQueryAndRemoveKeys(q, match.keysToRemove)
if (match.update) {
q = this.copyQueryAndExtendWithIdMember(q)
}
let ret = await this.filter(type, q, object, keyToRemove)
if (Object.keys(ret.result).length == 0) {
return null
}
if (deleteResultsFromDb) {
await this.db.delete(type, ret.result)
}
// update
if (match.update) {
for(let key in ret.updates) {
ret.result[key] = ret.updates[key]
}
ret.updates[this.db.idMember] = ret.result[this.db.idMember]
await this.db.update(type, ret.updates)
}
return ret.result
}
async filter(type, query, object, keyToRemove) {
let result = {}
let updates = {}
for(let queryKey in query) {
let queryValue = query[queryKey]
if (queryKey.startsWith("*")) {
updates[queryKey.substr(1)] = queryValue
continue
}
// field match
if (!queryKey.startsWith("?")) {
if (queryKey.endsWith("?") || queryKey.endsWith(">") || queryKey.endsWith("<")) {
let searchKey = queryKey.substr(0, queryKey.length-1)
result[searchKey] = object[searchKey]
continue
}
result[queryKey] = queryValue
continue
}
let search = queryKey.substr(1)
let regexp = new RegExp(`^${search}$`)
let isValueObject = queryValue instanceof Object
let isValueArray = queryValue instanceof Array
if (!isValueObject || (isValueArray && queryValue.length == 0)) {
for(let oKey in object) {
if (regexp.test(oKey)) {
result[oKey] = object[oKey]
if (oKey == keyToRemove) {
keyToRemove = null
}
}
}
continue
}
// subquery of an object
if (!isValueArray) {
let subQuery = this.copy(queryValue)
subQuery[this.db.idMember] = object[search]
let ret = await this.query(this.db.getTypeOf(type, search), subQuery, this.db.idMember)
if (!ret) {
continue
}
result[search] = ret[0]
continue
}
// subquery of an array
if (queryValue[0] instanceof Object) {
let subQuery = this.copy(queryValue[0])
subQuery[this.db.idMember + "?"] = object[search].join("|")
result[search] = await this.query(this.db.getTypeOf(type, search), subQuery, this.db.idMember)
continue
}
// matchArray
regexp = new RegExp(queryValue[0])
result[search] = []
for(let o of object[search]) {
if (regexp.test(o)) {
result[search].push(o)
}
}
}
if (keyToRemove) {
delete result[keyToRemove]
}
return { result, updates }
}
match(q, object) {
let match = true
let update = false
let keysToRemove = []
outer: for(let key in q) {
if (key.startsWith("//") || key.startsWith("!")) {
keysToRemove.push(key)
continue
}
if (key.startsWith("*")) {
update = true
continue
}
let value = q[key]
for(let op of this.operators) {
if (key.endsWith(op.suffix)) {
match = op.test(object[key.substr(0, key.length-1)], value)
break outer
}
}
if (!key.startsWith("?") && object[key] !== value) {
match = false
break
}
}
return { match, update, keysToRemove }
}
}