velocious 1.0.79 → 1.0.80

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.79",
6
+ "version": "1.0.80",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "VELOCIOUS_TEST_DIR=../ cd spec/dummy && npx velocious test",
@@ -40,7 +40,7 @@
40
40
  "strftime": "^0.10.2"
41
41
  },
42
42
  "devDependencies": {
43
- "mssql": "^11.0.1",
43
+ "mssql": "^12.0.0",
44
44
  "mysql": "^2.18.1",
45
45
  "node-fetch": "^3.3.1",
46
46
  "pg": "^8.16.3",
@@ -0,0 +1,29 @@
1
+ import Dummy from "../../dummy/index.js"
2
+ import Project from "../../dummy/src/models/project.js"
3
+ import Task from "../../dummy/src/models/task.js"
4
+
5
+ describe("Record - last", () => {
6
+ it("finds the last record", async () => {
7
+ await Dummy.run(async () => {
8
+ const project1 = await Project.create()
9
+
10
+ await Task.create({name: "Test task 1", project: project1})
11
+ await Task.create({name: "Test task 2", project: project1})
12
+ await Task.create({name: "Test task 3", project: project1})
13
+
14
+ const project2 = await Project.create()
15
+
16
+ await Task.create({name: "Test task 4", project: project2})
17
+ await Task.create({name: "Test task 5", project: project2})
18
+ await Task.create({name: "Test task 6", project: project2})
19
+
20
+ const foundTask = await Task.last()
21
+
22
+ expect(foundTask.name()).toEqual("Test task 6")
23
+
24
+ const foundTaskWithFilter = await Task.where({project_id: project1.id()}).last()
25
+
26
+ expect(foundTaskWithFilter.name()).toEqual("Test task 3")
27
+ })
28
+ })
29
+ })
@@ -174,7 +174,7 @@ export default class VelociousDatabaseQuery {
174
174
  }
175
175
 
176
176
  async first() {
177
- const newQuery = this.clone().limit(1).reorder(this.modelClass.orderableColumn())
177
+ const newQuery = this.clone().limit(1).reorder(`${this.driver.quoteTable(this.modelClass.tableName())}.${this.driver.quoteColumn(this.modelClass.orderableColumn())}`)
178
178
  const results = await newQuery.toArray()
179
179
 
180
180
  return results[0]
@@ -16,19 +16,18 @@ export default class VelociousDatabaseQueryWhereHash extends WhereBase {
16
16
  return sql
17
17
  }
18
18
 
19
- _whereSQLFromHash(hash, tableName) {
19
+ _whereSQLFromHash(hash, tableName, index = 0) {
20
20
  const options = this.getOptions()
21
21
  let sql = ""
22
- let index = 0
23
22
 
24
23
  for (const whereKey in hash) {
25
24
  const whereValue = hash[whereKey]
26
25
 
27
- if (index > 0) sql += " AND "
28
-
29
- if (!Array.isArray(whereValue) && typeof whereValue == "object") {
30
- sql += this._whereSQLFromHash(whereValue, whereKey)
26
+ if (!Array.isArray(whereValue) && whereValue !== null && typeof whereValue == "object") {
27
+ sql += this._whereSQLFromHash(whereValue, whereKey, index)
31
28
  } else {
29
+ if (index > 0) sql += " AND "
30
+
32
31
  if (tableName) {
33
32
  sql += `${options.quoteTableName(tableName)}.`
34
33
  }
@@ -37,6 +36,8 @@ export default class VelociousDatabaseQueryWhereHash extends WhereBase {
37
36
 
38
37
  if (Array.isArray(whereValue)) {
39
38
  sql += ` IN (${whereValue.map((value) => options.quote(value)).join(", ")})`
39
+ } else if (whereValue === null) {
40
+ sql += " IS NULL"
40
41
  } else {
41
42
  sql += ` = ${options.quote(whereValue)}`
42
43
  }
@@ -426,13 +426,6 @@ class VelociousDatabaseRecord {
426
426
  return await this.connection().insertMultiple(this.tableName(), columns, rows)
427
427
  }
428
428
 
429
- static async last() {
430
- const query = this._newQuery().order(this.primaryKey())
431
- const record = await query.last()
432
-
433
- return record
434
- }
435
-
436
429
  static async nextPrimaryKey() {
437
430
  const primaryKey = this.primaryKey()
438
431
  const tableName = this.tableName()
@@ -768,6 +761,10 @@ class VelociousDatabaseRecord {
768
761
  return this._newQuery().joins(...args)
769
762
  }
770
763
 
764
+ static async last(...args) {
765
+ return await this._newQuery().last(...args)
766
+ }
767
+
771
768
  static limit(...args) {
772
769
  return this._newQuery().limit(...args)
773
770
  }
@@ -1031,7 +1028,17 @@ class VelociousDatabaseRecord {
1031
1028
  }
1032
1029
  }
1033
1030
 
1034
- id() { return this.readAttribute(this.constructor._columnNameToAttributeName[this.constructor.primaryKey()]) }
1031
+ id() {
1032
+ const primaryKey = this.constructor.primaryKey()
1033
+ const attributeName = this.constructor._columnNameToAttributeName[primaryKey]
1034
+
1035
+ if (attributeName === undefined) {
1036
+ throw new Error(`Primary key ${primaryKey} doesn't exist in columns: ${Object.keys(this.constructor._columnNameToAttributeName).join(", ")}`)
1037
+ }
1038
+
1039
+ return this.readAttribute(attributeName)
1040
+ }
1041
+
1035
1042
  isPersisted() { return !this._isNewRecord }
1036
1043
  isNewRecord() { return this._isNewRecord }
1037
1044