velocious 1.0.72 → 1.0.73

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.72",
6
+ "version": "1.0.73",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "VELOCIOUS_TEST_DIR=../ cd spec/dummy && npx velocious test",
@@ -118,6 +118,24 @@ export default class VelociousDatabaseQuery {
118
118
  return record
119
119
  }
120
120
 
121
+ async findByOrFail(conditions) {
122
+ const newConditions = {}
123
+
124
+ for (const key in conditions) {
125
+ const keyUnderscore = inflection.underscore(key)
126
+
127
+ newConditions[keyUnderscore] = conditions[key]
128
+ }
129
+
130
+ const model = await this.clone().where(newConditions).first()
131
+
132
+ if (!model) {
133
+ throw new Error("Record not found")
134
+ }
135
+
136
+ return model
137
+ }
138
+
121
139
  async findOrInitializeBy(conditions, callback) {
122
140
  const record = await this.findBy(conditions)
123
141
 
@@ -166,8 +184,10 @@ export default class VelociousDatabaseQuery {
166
184
  return this
167
185
  }
168
186
 
169
- last = async () => {
170
- const results = await this.clone().reorder("id DESC").limit(1).toArray()
187
+ async last() {
188
+ const primaryKey = this.modelClass.primaryKey()
189
+ const tableName = this.modelClass.tableName()
190
+ const results = await this.clone().reorder(`${this.driver.quoteTable(tableName)}.${this.driver.quoteColumn(primaryKey)} DESC`).limit(1).toArray()
171
191
 
172
192
  return results[0]
173
193
  }
@@ -271,6 +291,7 @@ export default class VelociousDatabaseQuery {
271
291
  }
272
292
 
273
293
  this._wheres.push(where)
294
+
274
295
  return this
275
296
  }
276
297
  }
@@ -415,6 +415,19 @@ class VelociousDatabaseRecord {
415
415
  return record
416
416
  }
417
417
 
418
+ static async nextPrimaryKey() {
419
+ const primaryKey = this.primaryKey()
420
+ const tableName = this.tableName()
421
+ const connection = this.connection()
422
+ const newestRecord = await this.order(`${connection.quoteTable(tableName)}.${connection.quoteColumn(primaryKey)}`).last()
423
+
424
+ if (newestRecord) {
425
+ return newestRecord.id() + 1
426
+ } else {
427
+ return 1
428
+ }
429
+ }
430
+
418
431
  static setPrimaryKey(primaryKey) {
419
432
  this._primaryKey = primaryKey
420
433
  }
@@ -717,6 +730,10 @@ class VelociousDatabaseRecord {
717
730
  return this._newQuery().findBy(...args)
718
731
  }
719
732
 
733
+ static async findByOrFail(...args) {
734
+ return this._newQuery().findByOrFail(...args)
735
+ }
736
+
720
737
  static async findOrCreateBy(...args) {
721
738
  return this._newQuery().findOrCreateBy(...args)
722
739
  }
@@ -1010,7 +1027,7 @@ class VelociousDatabaseRecord {
1010
1027
  const query = this.constructor.where(whereObject)
1011
1028
  const reloadedModel = await query.first()
1012
1029
 
1013
- if (!reloadedModel) throw new Error(`${this.constructor.name}#${this.id()} couldn't be reloaded - record didn't exist`)
1030
+ if (!reloadedModel) throw new Error(`${this.constructor.name}#${id} couldn't be reloaded - record didn't exist`)
1014
1031
 
1015
1032
  this._attributes = reloadedModel.attributes()
1016
1033
  this._changes = {}
@@ -1,7 +1,7 @@
1
1
  import {EventEmitter} from "events"
2
2
  import FormDataPart from "./form-data-part.js"
3
3
  import Header from "./header.js"
4
- import Incorporator from "incorporator"
4
+ import {incorporate} from "incorporator"
5
5
  import {Logger} from "../../../logger.js"
6
6
  import ParamsToObject from "../params-to-object.js"
7
7
  import querystring from "querystring"
@@ -228,7 +228,6 @@ export default class RequestBuffer {
228
228
  delete this.postBodyChars
229
229
  delete this.postBodyBuffer
230
230
 
231
- this.parseQueryStringPostParams()
232
231
  this.completeRequest()
233
232
  }
234
233
 
@@ -244,6 +243,8 @@ export default class RequestBuffer {
244
243
  this.parseApplicationJsonParams()
245
244
  } else if (this.multiPartyFormData) {
246
245
  // Done after each new form data part
246
+ } else {
247
+ this.parseQueryStringPostParams()
247
248
  }
248
249
 
249
250
  this.events.emit("completed")
@@ -251,17 +252,15 @@ export default class RequestBuffer {
251
252
 
252
253
  parseApplicationJsonParams() {
253
254
  const newParams = JSON.parse(this.postBody)
254
- const incorporator = new Incorporator({objects: [this.params, newParams]})
255
255
 
256
- incorporator.merge()
256
+ incorporate(this.params, newParams)
257
257
  }
258
258
 
259
259
  parseQueryStringPostParams() {
260
260
  const unparsedParams = querystring.parse(this.postBody)
261
261
  const paramsToObject = new ParamsToObject(unparsedParams)
262
262
  const newParams = paramsToObject.toObject()
263
- const incorporator = new Incorporator({objects: [this.params, newParams]})
264
263
 
265
- incorporator.merge()
264
+ incorporate(this.params, newParams)
266
265
  }
267
266
  }