import knex, { Knex } from "knex"; import config from "../../knexfile.js"; import { ERPObjType } from "../../types/erp-types.js"; const DEFAULT_RECORD_TRACKING_TABLE_NAME = "record_tracking"; export type RecordTrackingObject = { entityType?: ERPObjType; lastValue: string; // Typically date/time string recordId: string; // A value to disambiguate records with the same lastValue }; export class RecordTrackingManager { db: Knex; constructor() { this.db = knex(config.local); } async updateRecord(record: RecordTrackingObject) { if (record.lastValue && record.recordId) { const recordUpdated = await this.db(DEFAULT_RECORD_TRACKING_TABLE_NAME) .where({ entityType: record.entityType }) .update(record); if (recordUpdated < 1) { await this.db(DEFAULT_RECORD_TRACKING_TABLE_NAME).insert(record); } } } async getTrackingRecord(type: ERPObjType) { return await this.db(DEFAULT_RECORD_TRACKING_TABLE_NAME) .select("*") .where({ entityType: type.toString() }); } async destroy() { return this.db.destroy(); } }