mssql-change-tracking
Version:
MS SQL server change tracking functions
51 lines (50 loc) • 3.31 kB
TypeScript
import sql from "mssql";
declare type CtChangesAllFieldsInput = QueryInput & {
pool: sql.ConnectionPool;
/**
* @default true
* @behavior throws error if version is not valid
* @description
* - If set to true, will check the validity if version number before query for changes.
* - Before an application obtains changes by using CHANGETABLE(CHANGES ...), the application must validate the value for last_synchronization_version that it plans to pass to CHANGETABLE(CHANGES ...). If the value of last_synchronization_version is not valid, that application must reinitialize all the data. [Reference](https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/work-with-change-tracking-sql-server?view=sql-server-ver15#validating-the-last-synchronized-version)
* @steps To obtain data inside a snapshot transaction, perform the following steps: ([Reference](https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/work-with-change-tracking-sql-server?view=sql-server-ver15#using-snapshot-isolation))
* 1. Set the transaction isolation level to snapshot and start a transaction.
* 1. Validate the last synchronization version by using CHANGE_TRACKING_MIN_VALID_VERSION().
* 1. Obtain the version to be used the next time by using CHANGE_TRACKING_CURRENT_VERSION().
* 1. Obtain the changes for the table by using CHANGETABLE(CHANGES ...)
* 1. Commit the transaction.
*/
safeRun?: boolean;
};
declare type CtChangesAllFieldsOutput = {
SYS_CHANGE_VERSION: string;
SYS_CHANGE_CREATION_VERSION: string;
SYS_CHANGE_OPERATION: "I" | "U" | "D";
SYS_CHANGE_COLUMNS: null | string;
SYS_CHANGE_CONTEXT: null | string;
[targetTableFields: string]: any;
};
declare type ValidResult<TargetTableFields> = {
currentVersion: string;
changes: Array<CtChangesAllFieldsOutput & TargetTableFields>;
};
/**
* @returns changes since specific version number including target table fields
* @description This row-set function is used to query for change information. The function queries the data stored in the internal change tracking tables. The function returns a results set that contains the primary keys of rows that have changed together with other change information such as the operation, columns updated and version for the row.
*/
export declare function ctChangesAllFields<TargetTableFields>({ pool, sinceVersion, tableName, schema, dbName, primaryKeys, safeRun, }: CtChangesAllFieldsInput): Promise<ValidResult<TargetTableFields>>;
declare type QueryInput = {
schema?: string;
dbName?: string;
tableName: string;
primaryKeys: string[];
sinceVersion: string;
/** whatever you put here, I add at the end of sql query! `WHERE ${whereRaw}`*/
whereRaw?: string;
};
/**
* @description it returns change tracking fields plus all target table fields
* @note it adds primary keys with 'PRIMARYKEY_' prefix so if target table has 'KeyID' as primary key the result will include 'PRIMARYKEY_KeyID' as a field. `ct.[${primaryKeyName}] as PRIMARYKEY_${primaryKeyName}`
*/
export declare function ctChangesAllFieldsQuery({ schema, dbName, tableName, primaryKeys, sinceVersion, whereRaw, }: QueryInput): string;
export {};