velocious 1.0.433 → 1.0.434

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": "build/bin/velocious.js"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.433",
6
+ "version": "1.0.434",
7
7
  "main": "build/index.js",
8
8
  "types": "build/index.d.ts",
9
9
  "files": [
@@ -3698,9 +3698,12 @@ class VelociousDatabaseRecord {
3698
3698
  */
3699
3699
  readColumn(attributeName) {
3700
3700
  this.getModelClass()._assertHasBeenInitialized()
3701
+ const belongsToChanges = this._belongsToChanges()
3701
3702
  let result
3702
3703
 
3703
- if (attributeName in this._changes) {
3704
+ if (attributeName in belongsToChanges) {
3705
+ result = belongsToChanges[attributeName]
3706
+ } else if (attributeName in this._changes) {
3704
3707
  result = this._changes[attributeName]
3705
3708
  } else if (attributeName in this._attributes) {
3706
3709
  result = this._attributes[attributeName]
@@ -3844,8 +3847,6 @@ class VelociousDatabaseRecord {
3844
3847
  throw new Error(`No insertSql on ${this.getModelClass().connection().constructor.name}`)
3845
3848
  }
3846
3849
 
3847
- const createdAtColumn = this.getModelClass().getColumns().find((column) => column.getName() == "created_at")
3848
- const updatedAtColumn = this.getModelClass().getColumns().find((column) => column.getName() == "updated_at")
3849
3850
  const data = Object.assign({}, this._belongsToChanges(), this.rawAttributes())
3850
3851
  const primaryKey = this.getModelClass().primaryKey()
3851
3852
  const primaryKeyColumn = this.getModelClass().getColumns().find((column) => column.getName() == primaryKey)
@@ -3853,14 +3854,7 @@ class VelociousDatabaseRecord {
3853
3854
  const driverSupportsDefaultUUID = typeof this._connection().supportsDefaultPrimaryKeyUUID == "function" && this._connection().supportsDefaultPrimaryKeyUUID()
3854
3855
  const isUUIDPrimaryKey = primaryKeyType?.includes("uuid")
3855
3856
  const shouldAssignUUIDPrimaryKey = isUUIDPrimaryKey && !driverSupportsDefaultUUID
3856
- const currentDate = new Date()
3857
-
3858
- if (createdAtColumn && (data.created_at === undefined || data.created_at === null || data.created_at === "")) {
3859
- data.created_at = currentDate
3860
- }
3861
- if (updatedAtColumn && (data.updated_at === undefined || data.updated_at === null || data.updated_at === "")) {
3862
- data.updated_at = currentDate
3863
- }
3857
+ this._setDefaultTimestampValues(data)
3864
3858
 
3865
3859
  const columnNames = this.getModelClass().getColumnNames()
3866
3860
  const hasUserProvidedPrimaryKey = data[primaryKey] !== undefined && data[primaryKey] !== null && data[primaryKey] !== ""
@@ -3878,6 +3872,31 @@ class VelociousDatabaseRecord {
3878
3872
  })
3879
3873
  const insertResult = await this._connection().query(sql, {logName: `${this.getModelClass().name} Create`})
3880
3874
 
3875
+ await this._applyInsertResult({data, insertResult, primaryKey, primaryKeyType})
3876
+ this.setIsNewRecord(false)
3877
+
3878
+ // Mark all relationships as preloaded, since we don't expect anything to have magically appeared since we created the record.
3879
+ for (const relationship of this.getModelClass().getRelationships()) {
3880
+ const instanceRelationship = this.getRelationshipByName(relationship.getRelationshipName())
3881
+
3882
+ if (instanceRelationship.getType() == "hasMany" && instanceRelationship.getLoadedOrUndefined() === null) {
3883
+ instanceRelationship.setLoaded([])
3884
+ }
3885
+
3886
+ instanceRelationship.setPreloaded(true)
3887
+ }
3888
+ }
3889
+
3890
+ /**
3891
+ * Applies the database insert response to this record.
3892
+ * @param {object} options - Insert result options.
3893
+ * @param {Record<string, ?>} options.data - Inserted data.
3894
+ * @param {?} options.insertResult - Result returned from the connection.
3895
+ * @param {string} options.primaryKey - Primary key column name.
3896
+ * @param {string | undefined} options.primaryKeyType - Primary key column type.
3897
+ * @returns {Promise<void>} - Resolves when complete.
3898
+ */
3899
+ async _applyInsertResult({data, insertResult, primaryKey, primaryKeyType}) {
3881
3900
  if (Array.isArray(insertResult) && insertResult[0] && insertResult[0][primaryKey]) {
3882
3901
  this._attributes = insertResult[0]
3883
3902
  this._changes = {}
@@ -3889,18 +3908,23 @@ class VelociousDatabaseRecord {
3889
3908
 
3890
3909
  await this._reloadWithId(id)
3891
3910
  }
3911
+ }
3892
3912
 
3893
- this.setIsNewRecord(false)
3894
-
3895
- // Mark all relationships as preloaded, since we don't expect anything to have magically appeared since we created the record.
3896
- for (const relationship of this.getModelClass().getRelationships()) {
3897
- const instanceRelationship = this.getRelationshipByName(relationship.getRelationshipName())
3898
-
3899
- if (instanceRelationship.getType() == "hasMany" && instanceRelationship.getLoadedOrUndefined() === null) {
3900
- instanceRelationship.setLoaded([])
3901
- }
3913
+ /**
3914
+ * Sets timestamp defaults for a new record insert.
3915
+ * @param {Record<string, ?>} data - Column-keyed data.
3916
+ * @returns {void} - No return value.
3917
+ */
3918
+ _setDefaultTimestampValues(data) {
3919
+ const createdAtColumn = this.getModelClass().getColumns().find((column) => column.getName() == "created_at")
3920
+ const updatedAtColumn = this.getModelClass().getColumns().find((column) => column.getName() == "updated_at")
3921
+ const currentDate = new Date()
3902
3922
 
3903
- instanceRelationship.setPreloaded(true)
3923
+ if (createdAtColumn && (data.created_at === undefined || data.created_at === null || data.created_at === "")) {
3924
+ data.created_at = currentDate
3925
+ }
3926
+ if (updatedAtColumn && (data.updated_at === undefined || data.updated_at === null || data.updated_at === "")) {
3927
+ data.updated_at = currentDate
3904
3928
  }
3905
3929
  }
3906
3930