velocious 1.0.77 → 1.0.78
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.
package/package.json
CHANGED
|
@@ -9,12 +9,15 @@ import RecordNotFoundError from "../record/record-not-found-error.js"
|
|
|
9
9
|
import SelectPlain from "./select-plain.js"
|
|
10
10
|
import WhereHash from "./where-hash.js"
|
|
11
11
|
import WherePlain from "./where-plain.js"
|
|
12
|
+
import restArgsError from "../../utils/rest-args-error.js"
|
|
12
13
|
|
|
13
14
|
export default class VelociousDatabaseQuery {
|
|
14
|
-
constructor({driver, froms = [], groups = [], joins = [], handler,
|
|
15
|
+
constructor({driver, froms = [], groups = [], joins = [], handler, limit = null, modelClass, offset = null, orders = [], page = null, perPage, preload = {}, selects = [], wheres = [], ...restArgs}) {
|
|
15
16
|
if (!driver) throw new Error("No driver given to query")
|
|
16
17
|
if (!handler) throw new Error("No handler given to query")
|
|
17
18
|
|
|
19
|
+
restArgsError(restArgs)
|
|
20
|
+
|
|
18
21
|
this.driver = driver
|
|
19
22
|
this.handler = handler
|
|
20
23
|
this.logger = new Logger(this)
|
|
@@ -22,8 +25,11 @@ export default class VelociousDatabaseQuery {
|
|
|
22
25
|
this._froms = froms
|
|
23
26
|
this._groups = groups
|
|
24
27
|
this._joins = joins
|
|
25
|
-
this.
|
|
28
|
+
this._limit = limit
|
|
29
|
+
this._offset = offset
|
|
26
30
|
this._orders = orders
|
|
31
|
+
this._page = page
|
|
32
|
+
this._perPage = perPage
|
|
27
33
|
this._preload = preload
|
|
28
34
|
this._selects = selects
|
|
29
35
|
this._wheres = wheres
|
|
@@ -36,9 +42,12 @@ export default class VelociousDatabaseQuery {
|
|
|
36
42
|
handler: this.handler.clone(),
|
|
37
43
|
groups: [...this._groups],
|
|
38
44
|
joins: [...this._joins],
|
|
39
|
-
|
|
45
|
+
limit: this._limit,
|
|
40
46
|
modelClass: this.modelClass,
|
|
47
|
+
offset: this._offset,
|
|
41
48
|
orders: [...this._orders],
|
|
49
|
+
page: this._page,
|
|
50
|
+
perPage: this._perPage,
|
|
42
51
|
preload: {...this._preload},
|
|
43
52
|
selects: [...this._selects],
|
|
44
53
|
wheres: [...this._wheres]
|
|
@@ -64,11 +73,25 @@ export default class VelociousDatabaseQuery {
|
|
|
64
73
|
|
|
65
74
|
const results = await countQuery._executeQuery()
|
|
66
75
|
|
|
76
|
+
|
|
77
|
+
// The query isn't grouped and a single result has been given
|
|
67
78
|
if (results.length == 1) {
|
|
68
79
|
return results[0].count
|
|
69
80
|
}
|
|
70
81
|
|
|
71
|
-
|
|
82
|
+
|
|
83
|
+
// The query may be grouped and a lot of different counts a given
|
|
84
|
+
let countResult = 0
|
|
85
|
+
|
|
86
|
+
for (const result of results) {
|
|
87
|
+
if (!("count" in result)) {
|
|
88
|
+
throw new Error("Invalid count result")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
countResult += result.count
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return countResult
|
|
72
95
|
}
|
|
73
96
|
|
|
74
97
|
getOptions() { return this.driver.options() }
|
|
@@ -193,7 +216,12 @@ export default class VelociousDatabaseQuery {
|
|
|
193
216
|
}
|
|
194
217
|
|
|
195
218
|
limit(value) {
|
|
196
|
-
this.
|
|
219
|
+
this._limit = value
|
|
220
|
+
return this
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
offset(value) {
|
|
224
|
+
this._offset = value
|
|
197
225
|
return this
|
|
198
226
|
}
|
|
199
227
|
|
|
@@ -206,6 +234,22 @@ export default class VelociousDatabaseQuery {
|
|
|
206
234
|
return this
|
|
207
235
|
}
|
|
208
236
|
|
|
237
|
+
page(pageNumber) {
|
|
238
|
+
const perPage = this._perPage || 30
|
|
239
|
+
const offset = (pageNumber - 1) * perPage
|
|
240
|
+
const limit = perPage
|
|
241
|
+
|
|
242
|
+
this._page = pageNumber
|
|
243
|
+
this.limit(limit)
|
|
244
|
+
this.offset(offset)
|
|
245
|
+
return this
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
perPage(perPage) {
|
|
249
|
+
this._perPage = perPage
|
|
250
|
+
return this
|
|
251
|
+
}
|
|
252
|
+
|
|
209
253
|
preload(data) {
|
|
210
254
|
incorporate(this._preload, data)
|
|
211
255
|
return this
|
|
@@ -15,6 +15,10 @@ export default class VelociousDatabaseQueryPreloaderHasMany {
|
|
|
15
15
|
const primaryKey = this.relationship.getPrimaryKey()
|
|
16
16
|
const preloadCollections = {}
|
|
17
17
|
|
|
18
|
+
if (!primaryKey) {
|
|
19
|
+
throw new Error(`${this.relationship.getModelClass().name}#${this.relationship.getRelationshipName()} doesn't have a primary key`)
|
|
20
|
+
}
|
|
21
|
+
|
|
18
22
|
for (const model of this.models) {
|
|
19
23
|
const primaryKeyValue = model.readColumn(primaryKey)
|
|
20
24
|
|
|
@@ -9,10 +9,10 @@ export default class VelocuiousDatabaseQueryParserLimitParser {
|
|
|
9
9
|
toSql() {
|
|
10
10
|
const {pretty, query} = digs(this, "pretty", "query")
|
|
11
11
|
const driver = query.driver
|
|
12
|
+
const options = this.query.getOptions()
|
|
12
13
|
let sql = ""
|
|
13
14
|
|
|
14
|
-
if (query.
|
|
15
|
-
if (query._limits.length >= 2) throw new Error(`Multiple limits found: ${query._limits.join(", ")}`)
|
|
15
|
+
if (query._limit === null) return sql
|
|
16
16
|
|
|
17
17
|
if (pretty) {
|
|
18
18
|
sql += "\n\n"
|
|
@@ -26,21 +26,28 @@ export default class VelocuiousDatabaseQueryParserLimitParser {
|
|
|
26
26
|
sql += "LIMIT"
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const limit = query._limit
|
|
30
|
+
const offset = query._offset
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
if (pretty) {
|
|
33
|
+
sql += "\n "
|
|
34
|
+
} else {
|
|
35
|
+
sql += " "
|
|
36
|
+
}
|
|
33
37
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
if (driver.getType() == "mssql") {
|
|
39
|
+
sql += `OFFSET ${options.quote(offset === null ? 0 : offset)} ROWS FETCH FIRST ${options.quote(limit)} ROWS ONLY`
|
|
40
|
+
} else {
|
|
41
|
+
sql += options.quote(limit)
|
|
42
|
+
|
|
43
|
+
if (offset !== null) {
|
|
44
|
+
if (pretty) {
|
|
45
|
+
sql += "\n "
|
|
46
|
+
} else {
|
|
47
|
+
sql += " "
|
|
48
|
+
}
|
|
39
49
|
|
|
40
|
-
|
|
41
|
-
sql += `OFFSET 0 ROWS FETCH FIRST ${this.query.getOptions().quote(limit)} ROWS ONLY`
|
|
42
|
-
} else {
|
|
43
|
-
sql += this.query.getOptions().quote(limit)
|
|
50
|
+
sql += `OFFSET ${options.quote(offset)}`
|
|
44
51
|
}
|
|
45
52
|
}
|
|
46
53
|
|
|
@@ -21,6 +21,7 @@ export default class VelociousDatabaseRecordBaseRelationship {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
getDependent() { return this._dependent }
|
|
24
|
+
getModelClass() { return this.modelClass }
|
|
24
25
|
getRelationshipName() { return this.relationshipName }
|
|
25
26
|
getPrimaryKey() { return this._primaryKey }
|
|
26
27
|
getType() { return this.type }
|