@avonjs/avonjs
Version:
A fluent Node.js API generator.
135 lines (134 loc) • 4.98 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const node_crypto_1 = require("node:crypto");
const Models_1 = require("../Models");
const Repositories_1 = require("../Repositories");
exports.default = (Parent) => {
class RecordsResourceEvents extends Parent {
/**
* Indicates activating record events for the resource.
*/
recordEvents = true;
/**
* Create an action event for the resource creation.
*/
async recordCreationEvent(payload = {}, transaction, userId) {
if (this.isRecordable()) {
await this.makeActionRepository(transaction).store(this.actionRepository().forResourceStore({
resourceName: this.resourceName(),
resource: this.resource,
payload: this.sanitizePayload(payload),
userId,
}));
}
}
/**
* Create an action event for the resource updates.
*/
async recordUpdateEvent(previous, payload = {}, transaction, userId) {
if (this.isRecordable()) {
await this.makeActionRepository(transaction).store(this.actionRepository().forResourceUpdate({
resourceName: this.resourceName(),
resource: this.resource,
payload: this.sanitizePayload(payload),
previous,
userId,
}));
}
}
/**
* Create an action event for the resource delete.
*/
async recordDeletionEvent(transaction, userId) {
if (this.isRecordable()) {
await this.makeActionRepository(transaction).store(this.actionRepository().forResourceDelete({
resourceName: this.resourceName(),
resource: this.resource,
userId,
}));
}
}
/**
* Create an action event for the resource delete.
*/
async recordRestoreEvent(transaction, userId) {
if (this.isRecordable()) {
await this.makeActionRepository(transaction).store(this.actionRepository().forResourceRestore({
resourceName: this.resourceName(),
resource: this.resource,
userId,
}));
}
}
/**
* Create an action event for the resource delete.
*/
async recordStandaloneActionEvent(action, payload = {}, userId) {
await this.makeActionRepository().store(this.actionRepository().forActionRan({
resourceName: this.resourceName(),
resource: new Models_1.Fluent(),
previous: new Models_1.Fluent(),
batchId: (0, node_crypto_1.randomUUID)(),
payload: this.sanitizePayload(payload),
action,
userId,
}));
}
/**
* Create an action event for the resource delete.
*/
async recordBulkActionEvent(action, changes = [], payload = {}, userId) {
const batchId = (0, node_crypto_1.randomUUID)();
await this.makeActionRepository().insert(changes.map(({ resource, previous }) => {
return this.actionRepository().forActionRan({
resourceName: this.resourceName(),
payload: this.sanitizePayload(payload),
resource,
previous,
batchId,
action,
userId,
});
}));
}
/**
* Forget action event rows.
*/
async flushActionEvents(transaction) {
if (this.isRecordable()) {
await this.makeActionRepository(transaction).flush(this.resourceName(), this.resource.getKey());
}
}
/**
* Make action events repository with given transaction;
*/
makeActionRepository(transaction) {
return this.actionRepository().setTransaction(transaction);
}
/**
* Get action event repository for resource.
*/
actionRepository() {
return new (class extends Repositories_1.ActionEvent {
searchableColumns() {
return [];
}
})();
}
/**
* Determine could record the action.
*/
isRecordable() {
return (this.resource !== undefined &&
this.resource.getKey() !== undefined &&
this.recordEvents);
}
/**
* Removes unsafe values from the record to ensure data integrity.
*/
sanitizePayload(payload) {
return payload;
}
}
return RecordsResourceEvents;
};