-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathBuilder.js
172 lines (133 loc) · 3.8 KB
/
Builder.js
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
/**
* Prepare attributes to be parsed
*/
import Parser from './Parser';
import setProp from 'dset'
export default class Builder {
constructor(model) {
this.model = model
this.includes = []
this.appends = []
this.sorts = []
this.pageValue = null
this.limitValue = null
this.payload = null
this.fields = {}
this.filters = {}
this.parser = new Parser(this)
}
// query string parsed
query() {
return this.parser.query()
}
/**
* Helpers
*/
/**
* Nested filter via array-type keys.
*
* @example
* const [_key, _value] = this._nestedFilter(keys, value)
* this.filters[_key] = _value
*
* @param {string[]} keys - Array-type keys, like `['a', 'b', 'c']`.
* @param {*} value - The value to be set.
*
* @return {[]} - An array containing the first key, which is the index to be used in `filters`
* object, and a value, which is the nested filter.
*
*/
_nestedFilter (keys, value) {
// Get first key from `keys` array, then remove it from array
const _key = keys.shift()
// Initialize an empty object
const _value = {}
// Convert the keys into a deeply nested object, which the value of the deepest key is
// the `value` property.
// Then assign the object to `_value` property.
setProp(_value, keys, value)
return [_key, _value]
}
/**
* Query builder
*/
include(...relationships) {
relationships = Array.isArray(relationships[0]) ? relationships[0] : relationships
this.includes = relationships
return this
}
append(...attributes) {
attributes = Array.isArray(attributes[0]) ? attributes[0] : attributes
this.appends = attributes
return this
}
select(...fields) {
if (fields.length === 0) {
throw new Error('You must specify the fields on select() method.')
}
// single entity .select(['age', 'firstname'])
if (fields[0].constructor === String || Array.isArray(fields[0])) {
this.fields[this.model.resource()] = fields.join(',')
}
// related entities .select({ posts: ['title', 'content'], user: ['age', 'firstname']} )
if (fields[0].constructor === Object) {
Object.entries(fields[0]).forEach(([key, value]) => {
this.fields[key] = value.join(',')
})
}
return this
}
where(key, value) {
if (key === undefined || value === undefined) {
throw new Error('The KEY and VALUE are required on where() method.')
}
if (Array.isArray(value) || value instanceof Object) {
throw new Error('The VALUE must be primitive on where() method.')
}
if (Array.isArray(key)) {
const [_key, _value] = this._nestedFilter(key, value)
this.filters[_key] = _value
} else {
this.filters[key] = value
}
return this
}
whereIn(key, array) {
if (!Array.isArray(array)) {
throw new Error('The second argument on whereIn() method must be an array.')
}
if (Array.isArray(key)) {
const [_key, _value] = this._nestedFilter(key, array.join(','))
this.filters[_key] = _value
} else {
this.filters[key] = array.join(',')
}
return this
}
orderBy(...fields) {
fields = Array.isArray(fields[0]) ? fields[0] : fields
this.sorts = fields
return this
}
page(value) {
if (!Number.isInteger(value)) {
throw new Error('The VALUE must be an integer on page() method.')
}
this.pageValue = value
return this
}
limit(value) {
if (!Number.isInteger(value)) {
throw new Error('The VALUE must be an integer on limit() method.')
}
this.limitValue = value
return this
}
params(payload) {
if (payload === undefined || payload.constructor !== Object) {
throw new Error('You must pass a payload/object as param.')
}
this.payload = payload
return this
}
}