velocious 1.0.77 → 1.0.79

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.77",
6
+ "version": "1.0.79",
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
 
@@ -290,15 +290,7 @@ class VelociousDatabaseRecord {
290
290
  this.prototype[`has${camelizedColumnNameBigFirst}`] = function() {
291
291
  let value = this[camelizedColumnName]()
292
292
 
293
- if (typeof value == "string") {
294
- value = value.trim()
295
- }
296
-
297
- if (value) {
298
- return true
299
- }
300
-
301
- return false
293
+ return this._hasAttribute(value)
302
294
  }
303
295
  }
304
296
 
@@ -306,6 +298,18 @@ class VelociousDatabaseRecord {
306
298
  this._initialized = true
307
299
  }
308
300
 
301
+ _hasAttribute(value) {
302
+ if (typeof value == "string") {
303
+ value = value.trim()
304
+ }
305
+
306
+ if (value) {
307
+ return true
308
+ }
309
+
310
+ return false
311
+ }
312
+
309
313
  static isInitialized() {
310
314
  if (this._initialized) return true
311
315
 
@@ -324,13 +328,19 @@ class VelociousDatabaseRecord {
324
328
  const nameCamelized = inflection.camelize(name)
325
329
  const setterMethodName = `set${nameCamelized}`
326
330
 
327
- this.prototype[name] = function() {
331
+ this.prototype[name] = function getTranslatedAttribute() {
328
332
  const locale = this._getConfiguration().getLocale()
329
333
 
330
334
  return this._getTranslatedAttributeWithFallback(name, locale)
331
335
  }
332
336
 
333
- this.prototype[setterMethodName] = function(newValue) {
337
+ this.prototype[`has${nameCamelized}`] = function hasTranslatedAttribute() {
338
+ const value = this[name]()
339
+
340
+ return this._hasAttribute(value)
341
+ }
342
+
343
+ this.prototype[setterMethodName] = function setTranslatedAttribute(newValue) {
334
344
  const locale = this._getConfiguration().getLocale()
335
345
 
336
346
  return this._setTranslatedAttribute(name, locale, newValue)
@@ -341,11 +351,11 @@ class VelociousDatabaseRecord {
341
351
  const getterMethodNameLocalized = `${name}${localeCamelized}`
342
352
  const setterMethodNameLocalized = `${setterMethodName}${localeCamelized}`
343
353
 
344
- this.prototype[getterMethodNameLocalized] = function() {
354
+ this.prototype[getterMethodNameLocalized] = function getTranslatedAttributeWithLocale() {
345
355
  return this._getTranslatedAttribute(name, locale)
346
356
  }
347
357
 
348
- this.prototype[setterMethodNameLocalized] = function(newValue) {
358
+ this.prototype[setterMethodNameLocalized] = function setTranslatedAttributeWithLocale(newValue) {
349
359
  return this._setTranslatedAttribute(name, locale, newValue)
350
360
  }
351
361
  }
@@ -930,7 +940,7 @@ class VelociousDatabaseRecord {
930
940
  }
931
941
 
932
942
  if (column && this.constructor.getDatabaseType() == "sqlite") {
933
- if (column.getType() == "date" || column.getType() == "datetime") {
943
+ if (result && (column.getType() == "date" || column.getType() == "datetime")) {
934
944
  result = new Date(Date.parse(result))
935
945
  }
936
946
  }
@@ -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 }