Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 12x 12x 12x 12x | import { Injectable } from '@nestjs/common';
import { PrismaService } from '@app/modules/prisma/prisma.service';
@Injectable()
export class TemporalService {
constructor(private prisma: PrismaService) {}
async getEntityHistory<T>(params: {
tableName: string;
entityId: number;
asOf?: Date;
from?: Date;
to?: Date;
}): Promise<T[]> {
const { tableName, entityId, asOf, from, to } = params;
// Build the SQL query based on the parameters
let query = `
SELECT *
FROM (
SELECT * FROM ${tableName}
WHERE id = $1
UNION ALL
SELECT * FROM ${tableName}_history
WHERE id = $1
) combined
`;
const values: any[] = [entityId];
let valueIndex = 2;
if (asOf) {
query += ` WHERE valid_from <= $${valueIndex} AND (valid_to > $${valueIndex} OR valid_to IS NULL)`;
values.push(asOf);
valueIndex++;
} else Iif (from || to) {
const conditions = [];
Iif (from) {
conditions.push(`valid_from >= $${valueIndex}`);
values.push(from);
valueIndex++;
}
Iif (to) {
conditions.push(`(valid_to <= $${valueIndex} OR valid_to IS NULL)`);
values.push(to);
valueIndex++;
}
Iif (conditions.length > 0) {
query += ` WHERE ${conditions.join(' AND ')}`;
}
}
query += ' ORDER BY valid_from DESC';
return this.prisma.client.$queryRawUnsafe(query, ...values);
}
async getEntityAtPoint<T extends { id: number }>(
tableName: string,
entityId: number,
timestamp: Date
): Promise<T | null> {
const results = await this.getEntityHistory<T>({
tableName,
entityId,
asOf: timestamp,
});
return results.length > 0 ? results[0] : null;
}
async compareEntityVersions<T extends { id: number }>(
tableName: string,
entityId: number,
version1Date: Date,
version2Date: Date
): Promise<{
version1: T | null;
version2: T | null;
differences: Array<{
field: string;
version1Value: any;
version2Value: any;
}>;
}> {
const [version1, version2] = await Promise.all([
this.getEntityAtPoint<T>(tableName, entityId, version1Date),
this.getEntityAtPoint<T>(tableName, entityId, version2Date),
]);
const differences = [];
Iif (version1 && version2) {
const allFields = new Set([
...Object.keys(version1),
...Object.keys(version2),
]);
for (const field of allFields) {
Iif (version1[field as keyof T] !== version2[field as keyof T]) {
differences.push({
field,
version1Value: version1[field as keyof T],
version2Value: version2[field as keyof T],
});
}
}
}
return {
version1,
version2,
differences,
};
}
async getEntityVersions<T extends { id: number }>(
tableName: string,
entityId: number,
options: {
limit?: number;
offset?: number;
from?: Date;
to?: Date;
} = {}
): Promise<{
versions: T[];
total: number;
}> {
const { limit = 10, offset = 0, from, to } = options;
const versions = await this.getEntityHistory<T>({
tableName,
entityId,
from,
to,
});
return {
versions: versions.slice(offset, offset + limit),
total: versions.length,
};
}
} |