@lucaapp/service-utils 5.13.0 → 5.14.0
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.
|
@@ -11,6 +11,8 @@ interface AtomicAuditLogOptions {
|
|
|
11
11
|
attributeRevisionModelTableName?: string;
|
|
12
12
|
primaryKeyType?: typeof DataTypes.UUID | typeof DataTypes.INTEGER;
|
|
13
13
|
viewPartitionCount?: number;
|
|
14
|
+
getContext?: () => Record<string, unknown> | null | undefined;
|
|
15
|
+
getCorrelationId?: () => string | null | undefined;
|
|
14
16
|
}
|
|
15
17
|
export interface PartitionResult {
|
|
16
18
|
archiveTableName: string;
|
|
@@ -21,7 +23,11 @@ export declare class AtomicAuditLog {
|
|
|
21
23
|
private tableName;
|
|
22
24
|
private primaryKeyType;
|
|
23
25
|
private viewPartitionCount;
|
|
26
|
+
private getContext?;
|
|
27
|
+
private getCorrelationId?;
|
|
28
|
+
private knownColumnsCache?;
|
|
24
29
|
constructor(sequelize: Sequelize, options?: AtomicAuditLogOptions);
|
|
30
|
+
private getKnownColumns;
|
|
25
31
|
getTableName(): string;
|
|
26
32
|
getViewName(): string;
|
|
27
33
|
addHistory<T extends Model>(model: ModelStatic<T>, options?: AddHistoryOptions): void;
|
|
@@ -63,6 +63,21 @@ class AtomicAuditLog {
|
|
|
63
63
|
this.tableName = options.attributeRevisionModelTableName ?? 'AuditLogs';
|
|
64
64
|
this.primaryKeyType = options.primaryKeyType ?? sequelize_1.DataTypes.INTEGER;
|
|
65
65
|
this.viewPartitionCount = options.viewPartitionCount ?? 6;
|
|
66
|
+
this.getContext = options.getContext;
|
|
67
|
+
this.getCorrelationId = options.getCorrelationId;
|
|
68
|
+
}
|
|
69
|
+
async getKnownColumns() {
|
|
70
|
+
if (!this.knownColumnsCache) {
|
|
71
|
+
this.knownColumnsCache = this.sequelize
|
|
72
|
+
.query(`SELECT column_name FROM information_schema.columns
|
|
73
|
+
WHERE table_name = :tableName AND table_schema = current_schema()`, {
|
|
74
|
+
replacements: { tableName: this.tableName },
|
|
75
|
+
type: sequelize_1.QueryTypes.SELECT,
|
|
76
|
+
})
|
|
77
|
+
.then(rows => new Set(rows.map(row => row.column_name)))
|
|
78
|
+
.catch(() => new Set());
|
|
79
|
+
}
|
|
80
|
+
return this.knownColumnsCache;
|
|
66
81
|
}
|
|
67
82
|
getTableName() {
|
|
68
83
|
return this.tableName;
|
|
@@ -86,31 +101,45 @@ class AtomicAuditLog {
|
|
|
86
101
|
if (dialect !== 'postgres') {
|
|
87
102
|
return;
|
|
88
103
|
}
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
104
|
+
const knownColumns = await this.getKnownColumns();
|
|
105
|
+
const useRevision = knownColumns.has('revision');
|
|
106
|
+
if (useRevision) {
|
|
107
|
+
const lockKey = `hashtext('${this.tableName}:${modelId}')`;
|
|
108
|
+
await this.sequelize.query(`SELECT pg_advisory_xact_lock(${lockKey})`, {
|
|
109
|
+
transaction,
|
|
110
|
+
type: sequelize_1.QueryTypes.SELECT,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const replacements = {
|
|
114
|
+
modelId,
|
|
115
|
+
modelName,
|
|
116
|
+
operation,
|
|
117
|
+
diff: JSON.stringify(diff),
|
|
118
|
+
};
|
|
119
|
+
const columns = ['"modelId"', '"model"', '"operation"', '"diff"'];
|
|
120
|
+
const values = [':modelId', ':modelName', ':operation', ':diff::jsonb'];
|
|
121
|
+
if (this.getContext && knownColumns.has('context')) {
|
|
122
|
+
const context = this.getContext() ?? null;
|
|
123
|
+
columns.push('"context"');
|
|
124
|
+
values.push(':context::jsonb');
|
|
125
|
+
replacements.context = context === null ? null : JSON.stringify(context);
|
|
126
|
+
}
|
|
127
|
+
if (this.getCorrelationId && knownColumns.has('correlationId')) {
|
|
128
|
+
columns.push('"correlationId"');
|
|
129
|
+
values.push(':correlationId');
|
|
130
|
+
replacements.correlationId = this.getCorrelationId() ?? null;
|
|
131
|
+
}
|
|
132
|
+
if (useRevision) {
|
|
133
|
+
columns.push('"revision"');
|
|
134
|
+
values.push(`COALESCE((SELECT MAX("revision") + 1 FROM "${this.tableName}" WHERE "modelId" = :modelId), 0)`);
|
|
135
|
+
}
|
|
136
|
+
columns.push('"createdAt"');
|
|
137
|
+
values.push('NOW()');
|
|
138
|
+
const query = useRevision
|
|
139
|
+
? `INSERT INTO "${this.tableName}" (${columns.join(', ')})\n SELECT ${values.join(', ')}`
|
|
140
|
+
: `INSERT INTO "${this.tableName}" (${columns.join(', ')}) VALUES (${values.join(', ')})`;
|
|
107
141
|
await this.sequelize.query(query, {
|
|
108
|
-
replacements
|
|
109
|
-
modelId,
|
|
110
|
-
modelName,
|
|
111
|
-
operation,
|
|
112
|
-
diff: JSON.stringify(diff),
|
|
113
|
-
},
|
|
142
|
+
replacements,
|
|
114
143
|
type: sequelize_1.QueryTypes.INSERT,
|
|
115
144
|
transaction,
|
|
116
145
|
});
|
|
@@ -17,7 +17,9 @@ const createSequelizeDbAdapter = (sequelize) => ({
|
|
|
17
17
|
const connectionManager = sequelize.connectionManager;
|
|
18
18
|
const connection = await connectionManager.getConnection({ type: 'write' });
|
|
19
19
|
try {
|
|
20
|
-
const result =
|
|
20
|
+
const result = values && values.length > 0
|
|
21
|
+
? await connection.query(text, values)
|
|
22
|
+
: await connection.query(text);
|
|
21
23
|
return {
|
|
22
24
|
rows: Array.isArray(result.rows) ? result.rows : [],
|
|
23
25
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lucaapp/service-utils",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.14.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -99,6 +99,7 @@
|
|
|
99
99
|
"follow-redirects": "^1.16.0",
|
|
100
100
|
"axios": "1.16.0",
|
|
101
101
|
"path-to-regexp": "0.1.13",
|
|
102
|
-
"ip-address": "10.1.1"
|
|
102
|
+
"ip-address": "10.1.1",
|
|
103
|
+
"fast-xml-builder": "^1.1.7"
|
|
103
104
|
}
|
|
104
105
|
}
|