mssql-change-tracking
Version:
MS SQL server change tracking functions
70 lines • 2.87 kB
JavaScript
import { writeLog } from "fast-node-logger";
import { getTableFullPath } from "../../../helpers/util";
/**
* @description This function is to check the validity of the value of versionNumber against specific table in the database.
* @note
* - this function accept table name or table ID
* - If an application has a value for last_synchronization_version that is older than the minimum valid synchronization version for a table, that application cannot perform valid change enumeration. This is because some change information might have been cleaned up.
*/
export async function ctIsVersionValid(input) {
writeLog(`ctIsVersionValid`, { level: "trace" });
if (input.tableId) {
return input.pool
.request()
.query(ctIsVersionValidByTableIdQuery({
versionNumber: input.versionNumber,
dbName: input.dbName,
tableId: input.tableId,
}))
.then((result) => result.recordset)
.then((row) => (row === null || row === void 0 ? void 0 : row[0]["result"]) !== "Client must be reinitialized");
}
else if (input.tableName) {
return input.pool
.request()
.query(ctIsVersionValidByTableNameQuery({
versionNumber: input.versionNumber,
dbName: input.dbName,
schema: input.schema,
tableName: input.tableName,
}))
.then((result) => result.recordset)
.then((row) => (row === null || row === void 0 ? void 0 : row[0]["result"]) !== "Client must be reinitialized");
}
else {
throw new Error("tableName or tableId should be provided.");
}
}
export function ctIsVersionValidByTableIdQuery({ versionNumber, dbName, tableId, }) {
let query = `
-- Check individual table.
IF (${versionNumber} < CHANGE_TRACKING_MIN_VALID_VERSION(${tableId}))
BEGIN
-- Handle invalid version & do not enumerate changes
-- Client must be reinitialized
SELECT 'Client must be reinitialized' As result
END
`;
if (dbName) {
query = `USE [${dbName}]; `.concat(query);
}
return query;
}
export function ctIsVersionValidByTableNameQuery({ versionNumber, dbName, tableName, schema, }) {
const tableFullPath = getTableFullPath({ tableName, schema, dbName });
let query = `
-- Check individual table.
IF (${versionNumber} < CHANGE_TRACKING_MIN_VALID_VERSION(
OBJECT_ID('${tableFullPath}')))
BEGIN
-- Handle invalid version & do not enumerate changes
-- Client must be reinitialized
SELECT 'Client must be reinitialized' As result
END
`;
if (dbName) {
query = `USE [${dbName}]; `.concat(query);
}
return query;
}
//# sourceMappingURL=change-tracking-is-version-valid.js.map