velocious 1.0.507 → 1.0.509

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.
@@ -15,6 +15,7 @@ import {digg} from "diggerize"
15
15
  import gettextConfig from "gettext-universal/build/src/config.js"
16
16
  import translate from "gettext-universal/build/src/translate.js"
17
17
  import Ability from "./authorization/ability.js"
18
+ import {initializeAuditedModelRelationships} from "./database/record/auditing.js"
18
19
  import EventEmitter from "./utils/event-emitter.js"
19
20
  import VelociousWebsocketChannelSubscribers from "./http-server/websocket-channel-subscribers.js"
20
21
  import {CurrentConfigurationNotSetError, currentConfiguration, setCurrentConfiguration} from "./current-configuration.js"
@@ -1807,6 +1808,7 @@ export default class VelociousConfiguration {
1807
1808
  }
1808
1809
 
1809
1810
  await this.getEnvironmentHandler().initializePackageModels(this)
1811
+ await initializeAuditedModelRelationships(this)
1810
1812
 
1811
1813
  await this.getEnvironmentHandler().initializeFrontendModelWebsocketPublishers(this)
1812
1814
  }
@@ -608,4 +608,71 @@ export default class VelociousDatabaseMigration {
608
608
 
609
609
  return exists
610
610
  }
611
+
612
+ /**
613
+ * Helper: creates the shared audit tables (`audit_actions`,
614
+ * `audit_auditable_types`, `audits`). Call from `up()` in a migration.
615
+ * @param {{id?: {type?: string}}} [options] - ID column options.
616
+ * @returns {Promise<void>}
617
+ */
618
+ async createSharedAuditTables(options = {}) {
619
+ const id = options.id || {}
620
+
621
+ await this.createTable("audit_actions", {id}, (table) => {
622
+ table.string("action", {index: {unique: true}, null: false})
623
+ table.timestamps()
624
+ })
625
+
626
+ await this.createTable("audit_auditable_types", {id}, (table) => {
627
+ table.string("name", {index: {unique: true}, null: false})
628
+ table.timestamps()
629
+ })
630
+
631
+ await this.createTable("audits", {id}, (table) => {
632
+ table.references("audit_action", {foreignKey: true, null: false})
633
+ table.references("audit_auditable_type", {foreignKey: true, null: false})
634
+ table.references("auditable", {null: false, polymorphic: true})
635
+ table.json("audited_changes")
636
+ table.json("params")
637
+ table.timestamps()
638
+ })
639
+ }
640
+
641
+ /**
642
+ * Helper: creates a dedicated audit table for a model (e.g.
643
+ * `project_audits` for the `projects` table). Call from `up()`
644
+ * in a migration.
645
+ * @param {string} modelTableName - Model table name (e.g. "projects").
646
+ * @param {{id?: {type?: string}}} [options] - ID column options.
647
+ * @returns {Promise<string>} The created audit table name.
648
+ */
649
+ async createDedicatedAuditTable(modelTableName, options = {}) {
650
+ const auditTable = dedicatedAuditTableName(modelTableName)
651
+ const id = options.id || {}
652
+
653
+ await this.createTable(auditTable, {id}, (table) => {
654
+ const refKey = inflection.singularize(modelTableName)
655
+
656
+ table.references(refKey, {foreignKey: true, null: false})
657
+ table.references("audit_action", {foreignKey: true, null: false})
658
+ table.json("audited_changes")
659
+ table.json("params")
660
+ table.timestamps()
661
+ })
662
+
663
+ return auditTable
664
+ }
665
+ }
666
+
667
+ /**
668
+ * Returns the dedicated audit table name for a model table.
669
+ * @param {string} modelTableName - Model table name (e.g. "projects").
670
+ * @returns {string} Dedicated audit table name (e.g. "project_audits").
671
+ */
672
+ export function dedicatedAuditTableName(modelTableName) {
673
+ if (modelTableName.endsWith("s")) {
674
+ return `${modelTableName.slice(0, -1)}_audits`
675
+ }
676
+
677
+ return `${modelTableName}_audits`
611
678
  }