mw-common-components 0.0.1-security → 99.99.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of mw-common-components might be problematic. Click here for more details.
- package/README.md +3 -3
- package/config/dev.env.js +37 -0
- package/config/hap.amend.js +29 -0
- package/config/index.js +8 -0
- package/config/prod.env.js +28 -0
- package/index.d.ts +130 -0
- package/index.js +1 -0
- package/package.json +44 -3
- package/src/index.html +12 -0
- package/src/lib/app.js +103 -0
- package/src/lib/axios/CHANGELOG.md +245 -0
- package/src/lib/axios/LICENSE +19 -0
- package/src/lib/axios/README.md +625 -0
- package/src/lib/axios/UPGRADE_GUIDE.md +162 -0
- package/src/lib/axios/dist/axios.js +1603 -0
- package/src/lib/axios/dist/axios.map +1 -0
- package/src/lib/axios/dist/axios.min.js +9 -0
- package/src/lib/axios/dist/axios.min.map +1 -0
- package/src/lib/axios/index.d.ts +131 -0
- package/src/lib/axios/index.js +1 -0
- package/src/lib/axios/lib/adapters/README.md +37 -0
- package/src/lib/axios/lib/adapters/http.js +237 -0
- package/src/lib/axios/lib/adapters/xhr.js +180 -0
- package/src/lib/axios/lib/axios.js +52 -0
- package/src/lib/axios/lib/cancel/Cancel.js +19 -0
- package/src/lib/axios/lib/cancel/CancelToken.js +57 -0
- package/src/lib/axios/lib/cancel/isCancel.js +5 -0
- package/src/lib/axios/lib/core/Axios.js +79 -0
- package/src/lib/axios/lib/core/InterceptorManager.js +52 -0
- package/src/lib/axios/lib/core/README.md +7 -0
- package/src/lib/axios/lib/core/createError.js +18 -0
- package/src/lib/axios/lib/core/dispatchRequest.js +86 -0
- package/src/lib/axios/lib/core/enhanceError.js +21 -0
- package/src/lib/axios/lib/core/settle.js +26 -0
- package/src/lib/axios/lib/core/transformData.js +20 -0
- package/src/lib/axios/lib/defaults.js +96 -0
- package/src/lib/axios/lib/helpers/README.md +7 -0
- package/src/lib/axios/lib/helpers/bind.js +11 -0
- package/src/lib/axios/lib/helpers/btoa.js +36 -0
- package/src/lib/axios/lib/helpers/buildURL.js +66 -0
- package/src/lib/axios/lib/helpers/combineURLs.js +14 -0
- package/src/lib/axios/lib/helpers/cookies.js +53 -0
- package/src/lib/axios/lib/helpers/deprecatedMethod.js +24 -0
- package/src/lib/axios/lib/helpers/isAbsoluteURL.js +14 -0
- package/src/lib/axios/lib/helpers/isURLSameOrigin.js +68 -0
- package/src/lib/axios/lib/helpers/normalizeHeaderName.js +12 -0
- package/src/lib/axios/lib/helpers/parseHeaders.js +53 -0
- package/src/lib/axios/lib/helpers/spread.js +27 -0
- package/src/lib/axios/lib/utils.js +303 -0
- package/src/lib/axios/modules/debug/CHANGELOG.md +395 -0
- package/src/lib/axios/modules/debug/LICENSE +19 -0
- package/src/lib/axios/modules/debug/README.md +437 -0
- package/src/lib/axios/modules/debug/dist/debug.js +886 -0
- package/src/lib/axios/modules/debug/node.js +1 -0
- package/src/lib/axios/modules/debug/package.json +96 -0
- package/src/lib/axios/modules/debug/src/browser.js +180 -0
- package/src/lib/axios/modules/debug/src/common.js +249 -0
- package/src/lib/axios/modules/debug/src/index.js +12 -0
- package/src/lib/axios/modules/debug/src/node.js +174 -0
- package/src/lib/axios/modules/follow-redirects/LICENSE +18 -0
- package/src/lib/axios/modules/follow-redirects/README.md +145 -0
- package/src/lib/axios/modules/follow-redirects/http.js +1 -0
- package/src/lib/axios/modules/follow-redirects/https.js +1 -0
- package/src/lib/axios/modules/follow-redirects/index.js +452 -0
- package/src/lib/axios/modules/follow-redirects/package.json +92 -0
- package/src/lib/axios/modules/is-buffer/LICENSE +21 -0
- package/src/lib/axios/modules/is-buffer/README.md +53 -0
- package/src/lib/axios/modules/is-buffer/index.js +21 -0
- package/src/lib/axios/modules/is-buffer/package.json +90 -0
- package/src/lib/axios/modules/is-buffer/test/basic.js +24 -0
- package/src/lib/axios/modules/ms/index.js +162 -0
- package/src/lib/axios/modules/ms/license.md +21 -0
- package/src/lib/axios/modules/ms/package.json +72 -0
- package/src/lib/axios/modules/ms/readme.md +60 -0
- package/src/lib/axios/package-lock.json +10437 -0
- package/src/lib/axios/package.json +111 -0
- package/src/lib/axios.min.js +10 -0
- package/src/lib/axiosRequest.js +84 -0
- package/src/lib/bmob.js +36 -0
- package/src/lib/common.js +227 -0
- package/src/lib/config.dev.js +62 -0
- package/src/lib/config.js +69 -0
- package/src/lib/dataType.js +17 -0
- package/src/lib/error.js +32 -0
- package/src/lib/file.js +250 -0
- package/src/lib/hapRequest.js +68 -0
- package/src/lib/hapStorage.js +62 -0
- package/src/lib/init.js +0 -0
- package/src/lib/nodestorage.js +14 -0
- package/src/lib/pay.js +19 -0
- package/src/lib/pointer.js +18 -0
- package/src/lib/query.js +689 -0
- package/src/lib/relation.js +45 -0
- package/src/lib/request.js +21 -0
- package/src/lib/sms.js +40 -0
- package/src/lib/socket.js +292 -0
- package/src/lib/storage.js +21 -0
- package/src/lib/user.js +225 -0
- package/src/lib/utf8md5.js +205 -0
- package/src/lib/utils.js +61 -0
- package/src/lib/webstorage.js +32 -0
- package/src/lib/wxRequest.js +88 -0
- package/src/lib/wxstorage.js +27 -0
- package/src/main.js +27 -0
- package/test/index.html +15 -0
- package/test/index.js +12 -0
- package/tsconfig.json +100 -0
package/src/lib/query.js
ADDED
@@ -0,0 +1,689 @@
|
|
1
|
+
let Bmob = require('./bmob')
|
2
|
+
const request = require('./request')
|
3
|
+
const {
|
4
|
+
isObject,
|
5
|
+
isString,
|
6
|
+
isNumber,
|
7
|
+
isUndefined,
|
8
|
+
isArray
|
9
|
+
} = require('./dataType')
|
10
|
+
const Error = require('./error')
|
11
|
+
const storage = require('./storage')
|
12
|
+
const query = class query {
|
13
|
+
constructor (parmas) {
|
14
|
+
this.tableName = `${Bmob._config.parameters.QUERY}/${parmas}`
|
15
|
+
this.className = parmas
|
16
|
+
this.init()
|
17
|
+
this.addArray = {}
|
18
|
+
this.setData = {}
|
19
|
+
}
|
20
|
+
init () {
|
21
|
+
this.queryData = {}
|
22
|
+
this.location = {}
|
23
|
+
this.andData = {}
|
24
|
+
this.orData = {}
|
25
|
+
this.stat = {}
|
26
|
+
this.limitNum = 100
|
27
|
+
this.skipNum = 0
|
28
|
+
this.includes = ''
|
29
|
+
this.queryReilation = {}
|
30
|
+
this.orders = null
|
31
|
+
this.keys = null
|
32
|
+
}
|
33
|
+
get (ObjectId) {
|
34
|
+
if (!isString(ObjectId)) {
|
35
|
+
throw new Error(415)
|
36
|
+
}
|
37
|
+
|
38
|
+
let oneData = {}
|
39
|
+
const incrementData = {}
|
40
|
+
const unsetData = {}
|
41
|
+
const addArray = {}
|
42
|
+
|
43
|
+
const add = (key, val) => {
|
44
|
+
if (!isString(key) || !isArray(val)) {
|
45
|
+
throw new Error(415)
|
46
|
+
}
|
47
|
+
addArray[key] = {
|
48
|
+
__op: 'Add',
|
49
|
+
objects: val
|
50
|
+
}
|
51
|
+
}
|
52
|
+
const addUnique = (key, val) => {
|
53
|
+
if (!isString(key) || !isArray(val)) {
|
54
|
+
throw new Error(415)
|
55
|
+
}
|
56
|
+
addArray[key] = {
|
57
|
+
__op: 'AddUnique',
|
58
|
+
objects: val
|
59
|
+
}
|
60
|
+
}
|
61
|
+
const remove = (key, val) => {
|
62
|
+
if (!isString(key) || !isArray(val)) {
|
63
|
+
throw new Error(415)
|
64
|
+
}
|
65
|
+
addArray[key] = {
|
66
|
+
__op: 'Remove',
|
67
|
+
objects: val
|
68
|
+
}
|
69
|
+
}
|
70
|
+
const increment = (key, val = 1) => {
|
71
|
+
if (!isString(key) || !isNumber(val)) {
|
72
|
+
throw new Error(415)
|
73
|
+
}
|
74
|
+
incrementData[key] = {
|
75
|
+
__op: 'Increment',
|
76
|
+
amount: val
|
77
|
+
}
|
78
|
+
}
|
79
|
+
const unset = key => {
|
80
|
+
if (!isString(ObjectId)) {
|
81
|
+
throw new Error(415)
|
82
|
+
}
|
83
|
+
unsetData[key] = {
|
84
|
+
__op: 'Delete'
|
85
|
+
}
|
86
|
+
}
|
87
|
+
const set = (key, val) => {
|
88
|
+
if (!isString(key) || isUndefined(val)) {
|
89
|
+
throw new Error(415)
|
90
|
+
}
|
91
|
+
const { filename, cdn, url } = val
|
92
|
+
if (!isUndefined(filename) && !isUndefined(cdn) && !isUndefined(url)) {
|
93
|
+
oneData[key] = {
|
94
|
+
__type: 'File',
|
95
|
+
group: cdn,
|
96
|
+
filename: filename,
|
97
|
+
url: url
|
98
|
+
}
|
99
|
+
} else {
|
100
|
+
oneData[key] = val
|
101
|
+
}
|
102
|
+
}
|
103
|
+
const save = () => {
|
104
|
+
const saveData = Object.assign(
|
105
|
+
unsetData,
|
106
|
+
oneData,
|
107
|
+
incrementData,
|
108
|
+
addArray
|
109
|
+
)
|
110
|
+
if (this.className === '_User') {
|
111
|
+
return new Promise((resolve, reject) => {
|
112
|
+
request(`${this.tableName}/${ObjectId}`, 'put', saveData)
|
113
|
+
.then(results => {
|
114
|
+
const current = this.current()
|
115
|
+
let newStorage = Object.assign(current, saveData)
|
116
|
+
storage.save('bmob', newStorage)
|
117
|
+
resolve(results)
|
118
|
+
})
|
119
|
+
.catch(err => {
|
120
|
+
reject(err)
|
121
|
+
})
|
122
|
+
})
|
123
|
+
}
|
124
|
+
return request(`${this.tableName}/${ObjectId}`, 'put', saveData)
|
125
|
+
}
|
126
|
+
|
127
|
+
const associated = {}
|
128
|
+
if (this.includes !== '') {
|
129
|
+
associated.include = this.includes
|
130
|
+
}
|
131
|
+
return new Promise((resolve, reject) => {
|
132
|
+
request(`${this.tableName}/${ObjectId}`, 'get', associated)
|
133
|
+
.then(results => {
|
134
|
+
Object.defineProperty(results, 'set', { value: set })
|
135
|
+
Object.defineProperty(results, 'unset', { value: unset })
|
136
|
+
Object.defineProperty(results, 'save', { value: save })
|
137
|
+
Object.defineProperty(results, 'increment', { value: increment })
|
138
|
+
Object.defineProperty(results, 'add', { value: add })
|
139
|
+
Object.defineProperty(results, 'remove', { value: remove })
|
140
|
+
Object.defineProperty(results, 'addUnique', { value: addUnique })
|
141
|
+
Object.defineProperty(results, 'destroy', {
|
142
|
+
value: () => this.destroy(ObjectId)
|
143
|
+
})
|
144
|
+
resolve(results)
|
145
|
+
})
|
146
|
+
.catch(err => {
|
147
|
+
reject(err)
|
148
|
+
})
|
149
|
+
})
|
150
|
+
}
|
151
|
+
destroy (ObjectId) {
|
152
|
+
if (!isString(ObjectId)) {
|
153
|
+
throw new Error(415)
|
154
|
+
}
|
155
|
+
return request(`${this.tableName}/${ObjectId}`, 'delete')
|
156
|
+
}
|
157
|
+
set (key, val) {
|
158
|
+
if (!isString(key) || isUndefined(val)) {
|
159
|
+
throw new Error(415, `${key}字段参数,类型不正确`)
|
160
|
+
}
|
161
|
+
const { filename, cdn, url } = val
|
162
|
+
if (!isUndefined(filename) && !isUndefined(cdn) && !isUndefined(url)) {
|
163
|
+
this.setData[key] = {
|
164
|
+
__type: 'File',
|
165
|
+
group: cdn,
|
166
|
+
filename: filename,
|
167
|
+
url: url
|
168
|
+
}
|
169
|
+
} else {
|
170
|
+
this.setData[key] = val
|
171
|
+
}
|
172
|
+
}
|
173
|
+
add (key, val) {
|
174
|
+
if (!isString(key) || !isArray(val)) {
|
175
|
+
throw new Error(415)
|
176
|
+
}
|
177
|
+
this.addArray[key] = {
|
178
|
+
__op: 'Add',
|
179
|
+
objects: val
|
180
|
+
}
|
181
|
+
}
|
182
|
+
addUnique (key, val) {
|
183
|
+
if (!isString(key) || !isArray(val)) {
|
184
|
+
throw new Error(415)
|
185
|
+
}
|
186
|
+
this.addArray[key] = {
|
187
|
+
__op: 'AddUnique',
|
188
|
+
objects: val
|
189
|
+
}
|
190
|
+
}
|
191
|
+
current () {
|
192
|
+
if (Bmob.type !== 'hap') {
|
193
|
+
const data = storage.fetch('bmob')
|
194
|
+
return typeof data === 'object' ? data : JSON.parse(data)
|
195
|
+
} else {
|
196
|
+
// 快应用功能
|
197
|
+
return new Promise((resolve, reject) => {
|
198
|
+
return storage
|
199
|
+
.fetch('bmob')
|
200
|
+
.then(res => {
|
201
|
+
resolve(res)
|
202
|
+
})
|
203
|
+
.catch(err => {
|
204
|
+
reject(err)
|
205
|
+
})
|
206
|
+
})
|
207
|
+
}
|
208
|
+
}
|
209
|
+
updateStorage (id) {
|
210
|
+
if (!isString(id)) {
|
211
|
+
throw new Error(415)
|
212
|
+
}
|
213
|
+
return new Promise((resolve, reject) => {
|
214
|
+
const current = this.current()
|
215
|
+
if (!current) {
|
216
|
+
throw new Error(415)
|
217
|
+
}
|
218
|
+
this.get(id)
|
219
|
+
.then(res => {
|
220
|
+
let newStorage = Object.assign(current, res)
|
221
|
+
storage.save('bmob', newStorage)
|
222
|
+
resolve(res)
|
223
|
+
})
|
224
|
+
.catch(err => {
|
225
|
+
console.log(err)
|
226
|
+
reject(err)
|
227
|
+
})
|
228
|
+
})
|
229
|
+
}
|
230
|
+
|
231
|
+
save (parmas = {}) {
|
232
|
+
if (!isObject(parmas)) {
|
233
|
+
throw new Error(415)
|
234
|
+
}
|
235
|
+
let method = this.setData.id ? 'PUT' : 'POST'
|
236
|
+
let objectId = this.setData.id ? this.setData.id : ''
|
237
|
+
delete this.setData.id
|
238
|
+
let saveData = Object.assign(parmas, this.setData, this.addArray)
|
239
|
+
return new Promise((resolve, reject) => {
|
240
|
+
request(`${this.tableName}/${objectId}`, method, saveData)
|
241
|
+
.then(results => {
|
242
|
+
this.addArray = {}
|
243
|
+
this.setData = {}
|
244
|
+
|
245
|
+
if (this.className === '_User') {
|
246
|
+
const current = this.current()
|
247
|
+
let newStorage = Object.assign(current, saveData)
|
248
|
+
storage.save('bmob', newStorage)
|
249
|
+
}
|
250
|
+
|
251
|
+
resolve(results)
|
252
|
+
})
|
253
|
+
.catch(err => {
|
254
|
+
reject(err)
|
255
|
+
})
|
256
|
+
})
|
257
|
+
}
|
258
|
+
saveAll (items) {
|
259
|
+
if (!isArray(items)) {
|
260
|
+
throw new Error(415)
|
261
|
+
}
|
262
|
+
if (items.length < 1) {
|
263
|
+
throw new Error(416)
|
264
|
+
}
|
265
|
+
|
266
|
+
let id
|
267
|
+
let p
|
268
|
+
let m = 'put'
|
269
|
+
let key = []
|
270
|
+
items.map(item => {
|
271
|
+
id = `/${item.objectId}`
|
272
|
+
if (id === '/undefined') {
|
273
|
+
id = ''
|
274
|
+
m = 'post'
|
275
|
+
}
|
276
|
+
|
277
|
+
p = {
|
278
|
+
method: m,
|
279
|
+
path: `${this.tableName}${id}`,
|
280
|
+
body: item.setData
|
281
|
+
}
|
282
|
+
key.push(p)
|
283
|
+
return item
|
284
|
+
})
|
285
|
+
|
286
|
+
let params = {
|
287
|
+
requests: key
|
288
|
+
}
|
289
|
+
let route = Bmob._config.parameters.BATCH
|
290
|
+
// 批量操作
|
291
|
+
return request(route, 'POST', params)
|
292
|
+
}
|
293
|
+
|
294
|
+
withinKilometers (field, { latitude, longitude }, km = 100) {
|
295
|
+
let newData = {}
|
296
|
+
newData[field] = {
|
297
|
+
$nearSphere: {
|
298
|
+
__type: 'GeoPoint',
|
299
|
+
latitude: latitude,
|
300
|
+
longitude: longitude
|
301
|
+
},
|
302
|
+
$maxDistanceInKilometers: km
|
303
|
+
}
|
304
|
+
this.location = newData
|
305
|
+
return newData
|
306
|
+
}
|
307
|
+
withinGeoBox (field, { latitude, longitude }, s) {
|
308
|
+
let newData = {}
|
309
|
+
newData[field] = {
|
310
|
+
$within: {
|
311
|
+
$box: [
|
312
|
+
{
|
313
|
+
__type: 'GeoPoint',
|
314
|
+
latitude: latitude,
|
315
|
+
longitude: longitude
|
316
|
+
},
|
317
|
+
{
|
318
|
+
__type: 'GeoPoint',
|
319
|
+
latitude: s.latitude,
|
320
|
+
longitude: s.longitude
|
321
|
+
}
|
322
|
+
]
|
323
|
+
}
|
324
|
+
}
|
325
|
+
this.location = newData
|
326
|
+
return newData
|
327
|
+
}
|
328
|
+
statTo (key, val) {
|
329
|
+
if (!isString(key)) {
|
330
|
+
throw new Error(415)
|
331
|
+
}
|
332
|
+
this.stat[key] = val
|
333
|
+
return this.stat
|
334
|
+
}
|
335
|
+
equalTo (key, operator, val) {
|
336
|
+
if (!isString(key)) {
|
337
|
+
throw new Error(415)
|
338
|
+
}
|
339
|
+
const judge = (key, operator, val) => {
|
340
|
+
let data = {}
|
341
|
+
let value = null
|
342
|
+
if (key === 'createdAt' || key === 'updateAt') {
|
343
|
+
value = {
|
344
|
+
__type: 'Date',
|
345
|
+
iso: val
|
346
|
+
}
|
347
|
+
} else {
|
348
|
+
value = val
|
349
|
+
}
|
350
|
+
switch (operator) {
|
351
|
+
case '==':
|
352
|
+
data[key] = value
|
353
|
+
break
|
354
|
+
case '===':
|
355
|
+
data[key] = value
|
356
|
+
break
|
357
|
+
case '!=':
|
358
|
+
data[key] = {
|
359
|
+
$ne: value
|
360
|
+
}
|
361
|
+
break
|
362
|
+
case '<':
|
363
|
+
data[key] = {
|
364
|
+
$lt: value
|
365
|
+
}
|
366
|
+
break
|
367
|
+
case '<=':
|
368
|
+
data[key] = {
|
369
|
+
$lte: value
|
370
|
+
}
|
371
|
+
break
|
372
|
+
case '>':
|
373
|
+
data[key] = {
|
374
|
+
$gt: value
|
375
|
+
}
|
376
|
+
break
|
377
|
+
case '>=':
|
378
|
+
data[key] = {
|
379
|
+
$gte: value
|
380
|
+
}
|
381
|
+
break
|
382
|
+
default:
|
383
|
+
throw new Error(415)
|
384
|
+
}
|
385
|
+
return data
|
386
|
+
}
|
387
|
+
const newData = judge(key, operator, val)
|
388
|
+
if (Object.keys(this.queryData).length) {
|
389
|
+
if (!isUndefined(this.queryData.$and)) {
|
390
|
+
this.queryData.$and.push(newData)
|
391
|
+
} else {
|
392
|
+
this.queryData = {
|
393
|
+
$and: [this.queryData, newData]
|
394
|
+
}
|
395
|
+
}
|
396
|
+
} else {
|
397
|
+
this.queryData = newData
|
398
|
+
}
|
399
|
+
|
400
|
+
return newData
|
401
|
+
}
|
402
|
+
containedIn (key, val) {
|
403
|
+
if (!isString(key) || !isArray(val)) {
|
404
|
+
throw new Error(415)
|
405
|
+
}
|
406
|
+
return queryData.call(this, key, '$in', val)
|
407
|
+
}
|
408
|
+
notContainedIn (key, val) {
|
409
|
+
if (!isString(key) || !isArray(val)) {
|
410
|
+
throw new Error(415)
|
411
|
+
}
|
412
|
+
return queryData.call(this, key, '$nin', val)
|
413
|
+
}
|
414
|
+
exists (key) {
|
415
|
+
if (!isString(key)) {
|
416
|
+
throw new Error(415)
|
417
|
+
}
|
418
|
+
return queryData.call(this, key, '$exists', true)
|
419
|
+
}
|
420
|
+
doesNotExist (key) {
|
421
|
+
if (!isString(key)) {
|
422
|
+
throw new Error(415)
|
423
|
+
}
|
424
|
+
return queryData.call(this, key, '$exists', false)
|
425
|
+
}
|
426
|
+
or (...querys) {
|
427
|
+
querys.map((item, i) => {
|
428
|
+
if (!isObject(item)) {
|
429
|
+
throw new Error(415)
|
430
|
+
}
|
431
|
+
})
|
432
|
+
const queryData = this.queryData.$and
|
433
|
+
if (!isUndefined(queryData)) {
|
434
|
+
for (let i = 0; i < queryData.length; i++) {
|
435
|
+
for (let k = 0; k < querys.length; k++) {
|
436
|
+
if (JSON.stringify(queryData[i]) === JSON.stringify(querys[k])) {
|
437
|
+
this.queryData.$and.splice(i, 1)
|
438
|
+
}
|
439
|
+
}
|
440
|
+
}
|
441
|
+
if (!queryData.length) {
|
442
|
+
delete this.queryData.$and
|
443
|
+
}
|
444
|
+
}
|
445
|
+
this.orData = {
|
446
|
+
$or: querys
|
447
|
+
}
|
448
|
+
}
|
449
|
+
and (...querys) {
|
450
|
+
querys.map((item, i) => {
|
451
|
+
if (!isObject(item)) {
|
452
|
+
throw new Error(415)
|
453
|
+
}
|
454
|
+
})
|
455
|
+
this.andData = {
|
456
|
+
$and: querys
|
457
|
+
}
|
458
|
+
}
|
459
|
+
limit (parmas) {
|
460
|
+
if (!isNumber(parmas)) {
|
461
|
+
throw new Error(415)
|
462
|
+
}
|
463
|
+
if (parmas > 1000) {
|
464
|
+
parmas = 1000
|
465
|
+
}
|
466
|
+
this.limitNum = parmas
|
467
|
+
}
|
468
|
+
skip (parmas) {
|
469
|
+
if (!isNumber(parmas)) {
|
470
|
+
throw new Error(415)
|
471
|
+
}
|
472
|
+
this.skipNum = parmas
|
473
|
+
}
|
474
|
+
order (...key) {
|
475
|
+
key.map(item => {
|
476
|
+
if (!isString(item)) {
|
477
|
+
throw new Error(415)
|
478
|
+
}
|
479
|
+
})
|
480
|
+
this.orders = key.join(',')
|
481
|
+
}
|
482
|
+
include (...key) {
|
483
|
+
key.map(item => {
|
484
|
+
if (!isString(item)) {
|
485
|
+
throw new Error(415)
|
486
|
+
}
|
487
|
+
})
|
488
|
+
this.includes = key.join(',')
|
489
|
+
}
|
490
|
+
select (...key) {
|
491
|
+
key.map(item => {
|
492
|
+
if (!isString(item)) {
|
493
|
+
throw new Error(415)
|
494
|
+
}
|
495
|
+
})
|
496
|
+
this.keys = key.join(',')
|
497
|
+
}
|
498
|
+
field (key, objectId) {
|
499
|
+
if (!isString(key) || !isString(objectId)) {
|
500
|
+
throw new Error(415)
|
501
|
+
}
|
502
|
+
this.queryReilation.where = {
|
503
|
+
$relatedTo: {
|
504
|
+
object: {
|
505
|
+
__type: 'Pointer',
|
506
|
+
className: this.className,
|
507
|
+
objectId: objectId
|
508
|
+
},
|
509
|
+
key: key
|
510
|
+
}
|
511
|
+
}
|
512
|
+
}
|
513
|
+
relation (tableName) {
|
514
|
+
if (!isString(tableName)) {
|
515
|
+
throw new Error(415)
|
516
|
+
}
|
517
|
+
if (tableName === '_User') {
|
518
|
+
tableName = 'users'
|
519
|
+
} else {
|
520
|
+
tableName = `classes/${tableName}`
|
521
|
+
}
|
522
|
+
this.queryReilation.count = 1
|
523
|
+
let parmas = Object.assign(this.getParams(), this.queryReilation)
|
524
|
+
|
525
|
+
return new Promise((resolve, reject) => {
|
526
|
+
request(`/1/${tableName}`, 'get', parmas)
|
527
|
+
.then((res) => {
|
528
|
+
resolve(res)
|
529
|
+
})
|
530
|
+
.catch(err => {
|
531
|
+
reject(err)
|
532
|
+
})
|
533
|
+
})
|
534
|
+
}
|
535
|
+
getParams () {
|
536
|
+
let parmas = {}
|
537
|
+
if (Object.keys(this.queryData).length) {
|
538
|
+
parmas.where = this.queryData
|
539
|
+
}
|
540
|
+
if (Object.keys(this.location).length) {
|
541
|
+
parmas.where = Object.assign(this.location, this.queryData)
|
542
|
+
}
|
543
|
+
if (Object.keys(this.andData).length) {
|
544
|
+
parmas.where = Object.assign(this.andData, this.queryData)
|
545
|
+
}
|
546
|
+
if (Object.keys(this.orData).length) {
|
547
|
+
parmas.where = Object.assign(this.orData, this.queryData)
|
548
|
+
}
|
549
|
+
parmas.limit = this.limitNum
|
550
|
+
parmas.skip = this.skipNum
|
551
|
+
parmas.include = this.includes
|
552
|
+
parmas.order = this.orders
|
553
|
+
parmas.keys = this.keys
|
554
|
+
if (Object.keys(this.stat).length) {
|
555
|
+
parmas = Object.assign(parmas, this.stat)
|
556
|
+
}
|
557
|
+
for (const key in parmas) {
|
558
|
+
if (
|
559
|
+
(parmas.hasOwnProperty(key) && parmas[key] === null) ||
|
560
|
+
parmas[key] === 0 || parmas[key] === ''
|
561
|
+
) {
|
562
|
+
delete parmas[key]
|
563
|
+
}
|
564
|
+
}
|
565
|
+
return parmas
|
566
|
+
}
|
567
|
+
find () {
|
568
|
+
let oneData = {}
|
569
|
+
let items = {}
|
570
|
+
const parmas = this.getParams()
|
571
|
+
const set = (key, val) => {
|
572
|
+
if (!key || isUndefined(val)) {
|
573
|
+
throw new Error(415)
|
574
|
+
}
|
575
|
+
oneData[key] = val
|
576
|
+
}
|
577
|
+
|
578
|
+
const batch = (method = 'updata') => {
|
579
|
+
console.log(method)
|
580
|
+
if (items.length < 1) {
|
581
|
+
throw new Error(416)
|
582
|
+
}
|
583
|
+
|
584
|
+
let id
|
585
|
+
let p
|
586
|
+
let m = 'put'
|
587
|
+
let key = []
|
588
|
+
items.map(item => {
|
589
|
+
id = `/${item.objectId}`
|
590
|
+
if (id === '/undefined') {
|
591
|
+
id = ''
|
592
|
+
m = 'post'
|
593
|
+
}
|
594
|
+
|
595
|
+
p = {
|
596
|
+
method: m,
|
597
|
+
path: `${this.tableName}${id}`,
|
598
|
+
body: oneData
|
599
|
+
}
|
600
|
+
if (method === 'delete') {
|
601
|
+
p = {
|
602
|
+
method: 'DELETE',
|
603
|
+
path: `${this.tableName}${id}`
|
604
|
+
}
|
605
|
+
}
|
606
|
+
key.push(p)
|
607
|
+
return item
|
608
|
+
})
|
609
|
+
|
610
|
+
let params = {
|
611
|
+
requests: key
|
612
|
+
}
|
613
|
+
// 批量操作
|
614
|
+
let route = Bmob._config.parameters.BATCH
|
615
|
+
return request(route, 'POST', params)
|
616
|
+
}
|
617
|
+
return new Promise((resolve, reject) => {
|
618
|
+
request(`${this.tableName}`, 'get', parmas)
|
619
|
+
.then((res) => {
|
620
|
+
let results = res.results
|
621
|
+
if (parmas.hasOwnProperty('count')) {
|
622
|
+
results = res
|
623
|
+
}
|
624
|
+
this.init()
|
625
|
+
Object.defineProperty(results, 'set', { value: set })
|
626
|
+
Object.defineProperty(results, 'saveAll', {
|
627
|
+
value: () => {
|
628
|
+
return batch()
|
629
|
+
}
|
630
|
+
})
|
631
|
+
Object.defineProperty(results, 'destroyAll', {
|
632
|
+
value: () => {
|
633
|
+
return batch('delete')
|
634
|
+
}
|
635
|
+
})
|
636
|
+
items = results
|
637
|
+
resolve(results)
|
638
|
+
})
|
639
|
+
.catch(err => {
|
640
|
+
reject(err)
|
641
|
+
})
|
642
|
+
})
|
643
|
+
}
|
644
|
+
count (limit = 0) {
|
645
|
+
const parmas = {}
|
646
|
+
if (Object.keys(this.queryData).length) {
|
647
|
+
parmas.where = this.queryData
|
648
|
+
}
|
649
|
+
if (Object.keys(this.andData).length) {
|
650
|
+
parmas.where = Object.assign(this.andData, this.queryData)
|
651
|
+
}
|
652
|
+
if (Object.keys(this.orData).length) {
|
653
|
+
parmas.where = Object.assign(this.orData, this.queryData)
|
654
|
+
}
|
655
|
+
parmas.count = 1
|
656
|
+
parmas.limit = limit
|
657
|
+
return new Promise((resolve, reject) => {
|
658
|
+
request(this.tableName, 'get', parmas)
|
659
|
+
.then(({ count }) => {
|
660
|
+
resolve(count)
|
661
|
+
})
|
662
|
+
.catch(err => {
|
663
|
+
reject(err)
|
664
|
+
})
|
665
|
+
})
|
666
|
+
}
|
667
|
+
}
|
668
|
+
|
669
|
+
function queryData (key, operator, val) {
|
670
|
+
let parent = {}
|
671
|
+
let child = {}
|
672
|
+
child[operator] = val
|
673
|
+
parent[key] = child
|
674
|
+
let newData = parent
|
675
|
+
if (Object.keys(this.queryData).length) {
|
676
|
+
if (!isUndefined(this.queryData.$and)) {
|
677
|
+
this.queryData.$and.push(newData)
|
678
|
+
} else {
|
679
|
+
this.queryData = {
|
680
|
+
$and: [this.queryData, newData]
|
681
|
+
}
|
682
|
+
}
|
683
|
+
} else {
|
684
|
+
this.queryData = newData
|
685
|
+
}
|
686
|
+
return newData
|
687
|
+
}
|
688
|
+
|
689
|
+
module.exports = query
|
@@ -0,0 +1,45 @@
|
|
1
|
+
const { isString, isArray } = require('./dataType')
|
2
|
+
const Error = require('./error')
|
3
|
+
|
4
|
+
const relation = class Relation {
|
5
|
+
constructor (tableName) {
|
6
|
+
if (!isString(tableName)) {
|
7
|
+
throw new Error(415)
|
8
|
+
}
|
9
|
+
this.tableName = tableName
|
10
|
+
}
|
11
|
+
add (parmas) {
|
12
|
+
return operation.call(this, parmas, 'AddRelation')
|
13
|
+
}
|
14
|
+
remove (parmas) {
|
15
|
+
return operation.call(this, parmas, 'RemoveRelation')
|
16
|
+
}
|
17
|
+
}
|
18
|
+
|
19
|
+
function operation (parmas, op) {
|
20
|
+
if (isString(parmas)) {
|
21
|
+
return {
|
22
|
+
'__op': op,
|
23
|
+
'objects': [
|
24
|
+
{
|
25
|
+
'__type': 'Pointer',
|
26
|
+
'className': this.tableName,
|
27
|
+
'objectId': parmas
|
28
|
+
}
|
29
|
+
]
|
30
|
+
}
|
31
|
+
} else if (isArray(parmas)) {
|
32
|
+
const data = []
|
33
|
+
parmas.map(item => {
|
34
|
+
if (!isString(item)) {
|
35
|
+
throw new Error(415)
|
36
|
+
}
|
37
|
+
data.push({ '__type': 'Pointer', 'className': this.tableName, 'objectId': item })
|
38
|
+
})
|
39
|
+
return { '__op': op, 'objects': data }
|
40
|
+
} else {
|
41
|
+
throw new Error(415)
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
module.exports = relation
|