velocious 1.0.485 → 1.0.486

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.
@@ -46,6 +46,7 @@ import singularizeModelName from "../../utils/singularize-model-name.js"
46
46
  import {defineModelScope} from "../../utils/model-scope.js"
47
47
  import { normalizeDateStringForWrite, normalizeDateValueForRead, normalizeDateValueForWrite } from "../datetime-storage.js"
48
48
  import {formatValue} from "../../utils/format-value.js"
49
+ import {captureCreateAuditChanges, captureUpdateAuditChanges, createAudit, createCreateAudit, createDestroyAudit, createUpdateAudit, registerAuditCallback, registerAuditing, withoutAudit} from "./auditing.js"
49
50
  import ValidatorsFormat from "./validators/format.js"
50
51
  import ValidatorsPresence from "./validators/presence.js"
51
52
  import ValidatorsUniqueness from "./validators/uniqueness.js"
@@ -247,6 +248,16 @@ class VelociousDatabaseRecord {
247
248
  * @type {boolean | undefined} */
248
249
  static _eagerLoadRecordMetadata
249
250
 
251
+ /**
252
+ * Narrows the runtime value to the documented type.
253
+ * @type {Record<string, import("./auditing.js").AuditCallback[]> | undefined} */
254
+ static _auditCallbacks
255
+
256
+ /**
257
+ * Narrows the runtime value to the documented type.
258
+ * @type {boolean | undefined} */
259
+ static _auditLifecycleCallbacksRegistered
260
+
250
261
  /**
251
262
  * Returns the model name, preferring an explicit `static modelName` declaration
252
263
  * over the JavaScript class `.name` property. This allows minified builds to
@@ -444,6 +455,16 @@ class VelociousDatabaseRecord {
444
455
  * @type {Record<string, ?>} */
445
456
  _changes = {}
446
457
 
458
+ /**
459
+ * Changes captured before a create audit is written.
460
+ * @type {import("./auditing.js").AuditChanges | undefined} */
461
+ _pendingCreateAuditChanges = undefined
462
+
463
+ /**
464
+ * Changes captured before an update audit is written.
465
+ * @type {import("./auditing.js").AuditChanges | undefined} */
466
+ _pendingUpdateAuditChanges = undefined
467
+
447
468
  /**
448
469
  * Attribute names explicitly assigned in the current update call.
449
470
  * @type {Set<string> | undefined}
@@ -613,6 +634,37 @@ class VelociousDatabaseRecord {
613
634
  VelociousDatabaseRecord.registerLifecycleCallback.call(this, "afterDestroy", /** @type {LifecycleCallbackType} */ (callback))
614
635
  }
615
636
 
637
+ /**
638
+ * Enables automatic create/update/destroy auditing for this model.
639
+ * @returns {void}
640
+ */
641
+ static audited() {
642
+ registerAuditing(this)
643
+ }
644
+
645
+ /**
646
+ * Registers a callback invoked after this model writes an audit row for the action.
647
+ * @template {typeof VelociousDatabaseRecord} MC
648
+ * @this {MC}
649
+ * @param {string} action - Audit action name.
650
+ * @param {import("./auditing.js").AuditCallback} callback - Callback to run after audit creation.
651
+ * @returns {() => void} Unsubscribe function.
652
+ */
653
+ static onAudit(action, callback) {
654
+ return registerAuditCallback(this, action, callback)
655
+ }
656
+
657
+ /**
658
+ * Returns records that do not have an audit row for the given action.
659
+ * @template {typeof VelociousDatabaseRecord} MC
660
+ * @this {MC}
661
+ * @param {string} action - Audit action name.
662
+ * @returns {ModelClassQuery<MC>} Query scoped to records without that audit action.
663
+ */
664
+ static withoutAudit(action) {
665
+ return withoutAudit(this, action)
666
+ }
667
+
616
668
  /**
617
669
  * Runs get validator type.
618
670
  * @param {string} validatorName - Validator name.
@@ -3597,6 +3649,55 @@ class VelociousDatabaseRecord {
3597
3649
  await this._runLifecycleCallbacks("afterDestroy")
3598
3650
  }
3599
3651
 
3652
+ /**
3653
+ * Stores an audit row for this record.
3654
+ * @param {import("./auditing.js").CreateAuditArgs} args - Audit row options.
3655
+ * @returns {Promise<number | string>} Created audit row id.
3656
+ */
3657
+ async createAudit(args) {
3658
+ return await createAudit(this, args)
3659
+ }
3660
+
3661
+ /**
3662
+ * Captures create changes before persistence clears the change set.
3663
+ * @returns {void}
3664
+ */
3665
+ captureCreateAuditChanges() {
3666
+ captureCreateAuditChanges(this)
3667
+ }
3668
+
3669
+ /**
3670
+ * Writes the create audit row.
3671
+ * @returns {Promise<void>}
3672
+ */
3673
+ async createCreateAudit() {
3674
+ await createCreateAudit(this)
3675
+ }
3676
+
3677
+ /**
3678
+ * Captures update changes before persistence clears the change set.
3679
+ * @returns {void}
3680
+ */
3681
+ captureUpdateAuditChanges() {
3682
+ captureUpdateAuditChanges(this)
3683
+ }
3684
+
3685
+ /**
3686
+ * Writes the update audit row.
3687
+ * @returns {Promise<void>}
3688
+ */
3689
+ async createUpdateAudit() {
3690
+ await createUpdateAudit(this)
3691
+ }
3692
+
3693
+ /**
3694
+ * Writes the destroy audit row.
3695
+ * @returns {Promise<void>}
3696
+ */
3697
+ async createDestroyAudit() {
3698
+ await createDestroyAudit(this)
3699
+ }
3700
+
3600
3701
  /**
3601
3702
  * Runs run lifecycle callbacks.
3602
3703
  * @param {"afterCreate" | "afterDestroy" | "afterSave" | "afterUpdate" | "beforeCreate" | "beforeDestroy" | "beforeSave" | "beforeUpdate" | "beforeValidation"} callbackName - Callback type.