mssql-change-tracking
Version:
MS SQL server change tracking functions
61 lines (60 loc) • 4.07 kB
TypeScript
import sql from "mssql";
declare type CtChangesOutput = {
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;
[primaryKey: string]: any;
};
declare type ValidResult<PrimaryKeys> = {
currentVersion: string;
changes: Array<CtChangesOutput & PrimaryKeys>;
};
interface CtChangesInput extends QueryInput {
pool: sql.ConnectionPool;
sinceVersion: string;
tableName: string;
/**
* @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 and uses [Snapshot Isolation](https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/work-with-change-tracking-sql-server?view=sql-server-ver15#using-snapshot-isolation).
* - 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;
}
/**
* @returns changes since specific version number
* @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 ctChanges<PrimaryKeys>({ pool, sinceVersion, tableName, dbName, schema, safeRun, }: CtChangesInput): Promise<ValidResult<PrimaryKeys>>;
declare type QueryInput = {
schema?: string;
dbName?: string;
tableName: string;
sinceVersion: string;
};
/**
* @reference https://docs.microsoft.com/en-us/sql/relational-databases/system-functions/changetable-transact-sql?view=sql-server-ver15
* @note [required permissions](https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/manage-change-tracking-sql-server?view=sql-server-ver15#security)
*/
export declare function ctChangesQuery({ sinceVersion, dbName, schema, tableName, }: QueryInput): string;
/**
* @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.
*/
export declare function ctChangesSafeQuery({ tableName, sinceVersion, schema, dbName, changeQuery, }: QueryInput & {
changeQuery: string;
}): string;
export {};