velocious 1.0.74 → 1.0.76

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.74",
6
+ "version": "1.0.76",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "VELOCIOUS_TEST_DIR=../ cd spec/dummy && npx velocious test",
@@ -106,6 +106,13 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
106
106
  return false
107
107
  }
108
108
 
109
+ supportsInsertIntoReturning() {
110
+ if (this.versionMajor >= 4) return true
111
+ if (this.versionMajor == 3 && this.versionMinor >= 35) return true
112
+
113
+ return false
114
+ }
115
+
109
116
  async insertMultipleWithSingleInsert(tableName, columns, rows) {
110
117
  const sql = new Insert({columns, driver: this, rows, tableName}).toSql()
111
118
 
@@ -1,7 +1,7 @@
1
1
  import restArgsError from "../../utils/rest-args-error.js"
2
2
 
3
3
  export default class VelociousDatabaseQueryInsertBase {
4
- constructor({columns, data, driver, multiple, tableName, returnLastInsertedColumnName, rows, ...restArgs}) {
4
+ constructor({columns, data, driver, multiple, tableName, returnLastInsertedColumnNames, rows, ...restArgs}) {
5
5
  if (!driver) throw new Error("No driver given to insert base")
6
6
  if (!tableName) throw new Error(`Invalid table name given to insert base: ${tableName}`)
7
7
 
@@ -11,7 +11,7 @@ export default class VelociousDatabaseQueryInsertBase {
11
11
  this.data = data
12
12
  this.driver = driver
13
13
  this.multiple = multiple
14
- this.returnLastInsertedColumnName = returnLastInsertedColumnName
14
+ this.returnLastInsertedColumnNames = returnLastInsertedColumnNames
15
15
  this.rows = rows
16
16
  this.tableName = tableName
17
17
  }
@@ -27,15 +27,35 @@ export default class VelociousDatabaseQueryInsertBase {
27
27
  let count = 0
28
28
  let columns, lastInsertedSQL
29
29
 
30
- if (this.returnLastInsertedColumnName) {
30
+ if (this.returnLastInsertedColumnNames) {
31
31
  if (driver.getType() == "mssql") {
32
- lastInsertedSQL = ` OUTPUT INSERTED.${driver.quoteColumn(this.returnLastInsertedColumnName)} AS lastInsertID`
32
+ lastInsertedSQL = ` OUTPUT `
33
+
34
+ for (let i = 0; i < this.returnLastInsertedColumnNames.length; i++) {
35
+ const columnName = this.returnLastInsertedColumnNames[i]
36
+
37
+ if (i > 0) {
38
+ lastInsertedSQL += ", "
39
+ }
40
+
41
+ lastInsertedSQL += ` INSERTED.${driver.quoteColumn(columnName)}`
42
+ }
33
43
 
34
44
  if (Object.keys(this.data).length <= 0) {
35
45
  sql += lastInsertedSQL
36
46
  }
37
- } else if (driver.getType() == "mysql" || driver.getType() == "pgsql") {
38
- lastInsertedSQL = ` RETURNING ${driver.quoteColumn(this.returnLastInsertedColumnName)} AS lastInsertID`
47
+ } else if (driver.getType() == "mysql" || driver.getType() == "pgsql" || (driver.getType() == "sqlite" && driver.supportsInsertIntoReturning())) {
48
+ lastInsertedSQL = " RETURNING "
49
+
50
+ for (let i = 0; i < this.returnLastInsertedColumnNames.length; i++) {
51
+ const columnName = this.returnLastInsertedColumnNames[i]
52
+
53
+ if (i > 0) {
54
+ lastInsertedSQL += ", "
55
+ }
56
+
57
+ lastInsertedSQL += ` ${driver.quoteColumn(columnName)}`
58
+ }
39
59
  }
40
60
  }
41
61
 
@@ -60,7 +80,7 @@ export default class VelociousDatabaseQueryInsertBase {
60
80
  sql += ")"
61
81
  }
62
82
 
63
- if (this.returnLastInsertedColumnName && driver.getType() == "mssql" && Object.keys(this.data).length > 0) {
83
+ if (this.returnLastInsertedColumnNames && driver.getType() == "mssql" && Object.keys(this.data).length > 0) {
64
84
  sql += lastInsertedSQL
65
85
  }
66
86
 
@@ -88,8 +108,8 @@ export default class VelociousDatabaseQueryInsertBase {
88
108
  }
89
109
  }
90
110
 
91
- if (this.returnLastInsertedColumnName) {
92
- if (driver.getType() == "mysql") {
111
+ if (this.returnLastInsertedColumnNames) {
112
+ if (driver.getType() == "pgsql" || driver.getType() == "mysql" || (driver.getType() == "sqlite" && driver.supportsInsertIntoReturning())) {
93
113
  sql += lastInsertedSQL
94
114
  }
95
115
  }
@@ -398,6 +398,14 @@ class VelociousDatabaseRecord {
398
398
  return this._columns
399
399
  }
400
400
 
401
+ static getColumnNames() {
402
+ if (!this._columnNames) {
403
+ this._columnNames = this.getColumns().map((column) => column.getName())
404
+ }
405
+
406
+ return this._columnNames
407
+ }
408
+
401
409
  static _getTable() {
402
410
  if (!this._table) throw new Error(`${this.name} hasn't been initialized yet`)
403
411
 
@@ -959,21 +967,24 @@ class VelociousDatabaseRecord {
959
967
  if (createdAtColumn) data.created_at = currentDate
960
968
  if (updatedAtColumn) data.updated_at = currentDate
961
969
 
970
+ const columnNames = this.constructor.getColumnNames()
962
971
  const sql = this._connection().insertSql({
963
- returnLastInsertedColumnName: this.constructor.primaryKey(),
972
+ returnLastInsertedColumnNames: columnNames,
964
973
  tableName: this._tableName(),
965
974
  data
966
975
  })
967
976
  const insertResult = await this._connection().query(sql)
968
- let id
977
+ const primaryKey = this.constructor.primaryKey()
969
978
 
970
- if (insertResult && insertResult[0]?.lastInsertID) {
971
- id = insertResult[0]?.lastInsertID
979
+ if (insertResult && insertResult[0][primaryKey]) {
980
+ this._attributes = insertResult[0]
981
+ this._changes = {}
972
982
  } else {
973
- id = await this._connection().lastInsertID()
983
+ const id = await this._connection().lastInsertID()
984
+
985
+ await this._reloadWithId(id)
974
986
  }
975
987
 
976
- await this._reloadWithId(id)
977
988
  this.setIsNewRecord(false)
978
989
 
979
990
  // Mark all relationships as preloaded, since we don't expect anything to have magically appeared since we created the record.