velocious 1.0.76 → 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
@@ -3,7 +3,7 @@
3
3
  "velocious": "bin/velocious.js"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.76",
6
+ "version": "1.0.78",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "VELOCIOUS_TEST_DIR=../ cd spec/dummy && npx velocious test",
@@ -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, limits = [], modelClass, orders = [], preload = {}, selects = [], wheres = []}) {
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._limits = limits
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
- limits: [...this._limits],
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
- throw new Error("count multiple stub")
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._limits.push(value)
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._limits.length == 0) return sql
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
- for (const limitKey in query._limits) {
30
- const limit = query._limits[limitKey]
29
+ const limit = query._limit
30
+ const offset = query._offset
31
31
 
32
- if (limitKey > 0) sql += ","
32
+ if (pretty) {
33
+ sql += "\n "
34
+ } else {
35
+ sql += " "
36
+ }
33
37
 
34
- if (pretty) {
35
- sql += "\n "
36
- } else {
37
- sql += " "
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
- if (driver.getType() == "mssql") {
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
 
@@ -976,7 +976,7 @@ class VelociousDatabaseRecord {
976
976
  const insertResult = await this._connection().query(sql)
977
977
  const primaryKey = this.constructor.primaryKey()
978
978
 
979
- if (insertResult && insertResult[0][primaryKey]) {
979
+ if (Array.isArray(insertResult) && insertResult[0] && insertResult[0][primaryKey]) {
980
980
  this._attributes = insertResult[0]
981
981
  this._changes = {}
982
982
  } else {
@@ -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 }