Skip to content

Commit 31baf61

Browse files
committed
- some minor refactoring Data Service
1 parent c1d4726 commit 31baf61

File tree

8 files changed

+71
-76
lines changed

8 files changed

+71
-76
lines changed

src/data/persistence.js

Whitespace-only changes.

src/data/store/bulk.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export function bulkCreate(objectsArray, asyncHandler) {
1212
throw new Error(MSG_ERROR)
1313
}
1414

15-
for (let i = 0; i < objectsArray.length; i++) {
16-
if (!Utils.isObject(objectsArray[i])) {
15+
objectsArray.forEach(obj => {
16+
if (!Utils.isObject(obj)) {
1717
throw new Error(MSG_ERROR)
1818
}
19-
}
19+
})
2020

2121
return Request.post({
2222
url : Urls.dataBulkTable(this.className),
@@ -26,17 +26,18 @@ export function bulkCreate(objectsArray, asyncHandler) {
2626
})
2727
}
2828

29-
export function bulkUpdate(templateObject, whereClause, asyncHandler) {
29+
export function bulkUpdate(templateObject, where, asyncHandler) {
3030
if (!templateObject || !Utils.isObject(templateObject)) {
3131
throw new Error('Invalid templateObject argument. The first argument must contain object')
3232
}
3333

34-
if (!whereClause || !Utils.isString(whereClause)) {
34+
if (!where || !Utils.isString(where)) {
3535
throw new Error('Invalid whereClause argument. The first argument must contain "whereClause" string.')
3636
}
3737

3838
return Request.put({
39-
url : Urls.dataBulkTable(this.className) + '?' + Utils.toQueryParams({ where: whereClause }),
39+
url : Urls.dataBulkTable(this.className),
40+
query : { where },
4041
data : templateObject,
4142
isAsync : !!asyncHandler,
4243
asyncHandler: asyncHandler
@@ -49,28 +50,31 @@ export function bulkDelete(objectsArray, asyncHandler) {
4950
'The first argument must contain array of objects or array of id or "whereClause" string'
5051
)
5152

52-
if (!objectsArray || (!Utils.isArray(objectsArray) && !Utils.isString(objectsArray))) {
53+
if (!Utils.isArray(objectsArray) && !Utils.isString(objectsArray)) {
5354
throw new Error(MSG_ERROR)
5455
}
5556

56-
for (let i = 0; i < objectsArray.length; i++) {
57-
if (!Utils.isObject(objectsArray[i]) && !Utils.isString(objectsArray[i])) {
58-
throw new Error(MSG_ERROR)
59-
}
60-
}
61-
62-
let whereClause
57+
let where
6358

6459
if (Utils.isString(objectsArray)) {
65-
whereClause = objectsArray
60+
where = objectsArray
61+
6662
} else if (Utils.isArray(objectsArray)) {
67-
const objects = objectsArray.map(obj => Utils.isString(obj) ? obj : obj.objectId)
6863

69-
whereClause = 'objectId in (\'' + objects.join('\', \'') + '\')'
64+
const objects = objectsArray.map(obj => {
65+
if (!Utils.isObject(obj) && !Utils.isString(obj)) {
66+
throw new Error(MSG_ERROR)
67+
}
68+
69+
return `'${Utils.isString(obj) ? obj : obj.objectId}'`
70+
})
71+
72+
where = `objectId in (${objects.join(',')})`
7073
}
7174

7275
return Request.delete({
73-
url : Urls.dataBulkTable(this.className) + '?' + Utils.toQueryParams({ where: whereClause }),
76+
url : Urls.dataBulkTable(this.className),
77+
query : { where },
7478
isAsync : !!asyncHandler,
7579
asyncHandler: asyncHandler
7680
})

src/data/store/count.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,16 @@ import DataQueryBuilder from '../query-builder'
77
export function getObjectCount(condition, asyncHandler) {
88
if (condition instanceof Async) {
99
asyncHandler = condition
10-
condition = null
10+
condition = undefined
1111
}
1212

1313
if (condition instanceof DataQueryBuilder) {
1414
condition = condition.build().condition
1515
}
1616

17-
let url = Urls.dataTableCount(this.className)
18-
19-
if (condition) {
20-
url += '?where=' + encodeURIComponent(condition)
21-
}
22-
2317
return Request.get({
24-
url : url,
18+
url : Urls.dataTableCount(this.className),
19+
query : { where: condition },
2520
isAsync : !!asyncHandler,
2621
asyncHandler: asyncHandler
2722
})

src/data/store/extract-query-options.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import Utils from '../../utils'
22

3+
4+
//TODO: refactor me
5+
//TODO: does make sense to move this logic into QueryBuilder?
6+
37
export function extractQueryOptions(options) {
48
const params = []
59

src/data/store/find.js

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,32 @@ import QueryBuilder from '../query-builder'
88
import { parseFindResponse } from './parse'
99
import { extractQueryOptions } from './extract-query-options'
1010

11+
12+
//TODO: refactor me
13+
1114
export function findUtil(className, Model, dataQuery, asyncHandler) {
1215
dataQuery = dataQuery || {}
1316

14-
let props
15-
let whereClause
16-
let options
1717
const query = []
18-
let url = Urls.dataTable(className)
19-
20-
if (dataQuery.properties && dataQuery.properties.length) {
21-
props = 'props=' + Utils.encodeArrayToUriComponent(dataQuery.properties)
22-
}
23-
24-
if (dataQuery.condition) {
25-
whereClause = 'where=' + encodeURIComponent(dataQuery.condition)
26-
}
27-
28-
if (dataQuery.options) {
29-
options = extractQueryOptions(dataQuery.options)
30-
}
3118

3219
if (asyncHandler) {
3320
asyncHandler = Utils.wrapAsync(asyncHandler, resp => parseFindResponse(resp, Model))
3421
}
3522

36-
if (options) {
37-
query.push(options)
23+
if (dataQuery.options) {
24+
query.push(extractQueryOptions(dataQuery.options))
3825
}
3926

40-
if (whereClause) {
41-
query.push(whereClause)
27+
if (dataQuery.condition) {
28+
query.push('where=' + encodeURIComponent(dataQuery.condition))
4229
}
4330

44-
if (props) {
45-
query.push(props)
31+
if (dataQuery.properties && dataQuery.properties.length) {
32+
query.push('props=' + Utils.encodeArrayToUriComponent(dataQuery.properties))
4633
}
4734

35+
let url = Urls.dataTable(className)
36+
4837
if (dataQuery.url) {
4938
url += '/' + dataQuery.url
5039
}
@@ -129,8 +118,6 @@ export function findById() {
129118
}
130119

131120
return isAsync ? result : parseFindResponse(result, this.model)
132-
} else {
133-
throw new Error('Invalid value for the "value" argument. The argument must contain only string or object values')
134121
}
135122
}
136123

src/data/store/parse.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Utils from '../../utils'
22

3+
//TODO: refactor me
4+
35
function formCircDeps(obj) {
46
const result = new obj.constructor()
57
const circDepsIDs = {}

src/data/store/relations.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import LoadRelationsQueryBuilder from '../load-relations-query-builder'
77
import { extractQueryOptions } from './extract-query-options'
88
import { parseFindResponse } from './parse'
99

10+
//TODO: refactor me
11+
1012
function collectRelationObject(parent, columnName, children) {
1113
const relation = {
1214
columnName
@@ -73,12 +75,12 @@ function buildRelationUrl(className, relation) {
7375
return url
7476
}
7577

76-
export function loadRelations(parentObjectId, queryBuilder/**, async */) {
78+
export function loadRelations(parentObjectId, queryBuilder, asyncHandler) {
7779
if (!parentObjectId || !Utils.isString(parentObjectId)) {
7880
throw new Error('The parentObjectId is required argument and must be a nonempty string')
7981
}
8082

81-
if (!queryBuilder || !(queryBuilder instanceof LoadRelationsQueryBuilder)) {
83+
if (!(queryBuilder instanceof LoadRelationsQueryBuilder)) {
8284
throw new Error(
8385
'Invalid queryBuilder object.' +
8486
'The queryBuilder is required and must be instance of the Backendless.LoadRelationsQueryBuilder'
@@ -93,27 +95,25 @@ export function loadRelations(parentObjectId, queryBuilder/**, async */) {
9395
throw new Error('The options relationName is required and must contain string value')
9496
}
9597

96-
let whereClause
9798
let options
9899
const query = []
99100

100-
if (dataQuery.condition) {
101-
whereClause = 'where=' + encodeURIComponent(dataQuery.condition)
102-
}
103-
104101
if (dataQuery.options) {
105102
options = extractQueryOptions(dataQuery.options)
106103
}
107104

108-
options && query.push(options)
109-
whereClause && query.push(whereClause)
105+
if (options) {
106+
query.push(options)
107+
}
108+
109+
if (dataQuery.condition) {
110+
query.push('where=' + encodeURIComponent(dataQuery.condition))
111+
}
110112

111-
const relationModel = dataQuery.relationModel || null
112-
let responder = Utils.extractResponder(arguments)
113113
let url = Urls.dataTable(this.className) + Utils.toUri(parentObjectId, relationName)
114114

115-
if (responder) {
116-
responder = Utils.wrapAsync(responder, resp => parseFindResponse(resp, relationModel))
115+
if (asyncHandler) {
116+
asyncHandler = Utils.wrapAsync(asyncHandler, resp => parseFindResponse(resp, dataQuery.relationModel))
117117
}
118118

119119
if (query.length) {
@@ -122,11 +122,15 @@ export function loadRelations(parentObjectId, queryBuilder/**, async */) {
122122

123123
const result = Request.get({
124124
url : url,
125-
isAsync : !!responder,
126-
asyncHandler: responder
125+
isAsync : !!asyncHandler,
126+
asyncHandler: asyncHandler
127127
})
128128

129-
return !!responder ? result : parseFindResponse(result, relationModel)
129+
if (asyncHandler) {
130+
return result
131+
}
132+
133+
return parseFindResponse(result, dataQuery.relationModel)
130134
}
131135

132136
export function setRelation(parent, columnName, children, asyncHandler) {

src/logging/collector.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ let lastFlushListeners
1010
const LoggingCollector = {
1111
loggers : {},
1212
pool : [],
13-
messagesCount: 0,
1413
numOfMessages: 10,
1514
timeFrequency: 1,
1615

1716
reset() {
1817
this.loggers = {}
1918
this.pool = []
20-
this.messagesCount = 0
2119
this.numOfMessages = 10
2220
this.timeFrequency = 1
2321
},
@@ -40,16 +38,20 @@ const LoggingCollector = {
4038
}
4139

4240
this.pool.push(messageObj)
43-
this.messagesCount++
41+
4442
this.checkMessagesLen()
4543
},
4644

45+
checkMessagesLen: function() {
46+
if (this.pool.length >= this.numOfMessages) {
47+
this.sendRequest()
48+
}
49+
},
50+
4751
flush : Utils.promisified('_flush'),
4852
flushSync: Utils.synchronized('_flush'),
4953

50-
_flush: function() {
51-
const asyncHandler = Utils.extractResponder(arguments)
52-
54+
_flush: function(asyncHandler) {
5355
if (this.pool.length) {
5456
if (this.flushInterval) {
5557
clearTimeout(this.flushInterval)
@@ -80,7 +82,6 @@ const LoggingCollector = {
8082
})
8183

8284
this.pool = []
83-
this.messagesCount = 0
8485

8586
} else if (asyncHandler) {
8687
if (lastFlushListeners) {
@@ -100,9 +101,7 @@ const LoggingCollector = {
100101
this.timeFrequency = timeFrequency
101102

102103
//TODO: check when set new timeFrequency
103-
if (this.messagesCount > (this.numOfMessages - 1)) {
104-
this.sendRequest()
105-
}
104+
this.checkMessagesLen()
106105
}
107106
}
108107

0 commit comments

Comments
 (0)