UNPKG

autosql

Version:

An auto-parser of JSON into SQL.

48 lines (47 loc) 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ensureTimestamps = ensureTimestamps; const utilities_1 = require("../helpers/utilities"); const defaults_1 = require("../config/defaults"); function ensureTimestamps(dbConfig, metaData, startDate = new Date()) { if (!dbConfig.addTimestamps) { return metaData; // RETURN if timestamps are not required } const NORMALIZED_CREATED_TIMESTAMP_NAMES = defaults_1.CREATED_TIMESTAMP_NAMES.map(utilities_1.normalizeName); const NORMALIZED_MODIFIED_TIMESTAMP_NAMES = defaults_1.MODIFIED_TIMESTAMP_NAMES.map(utilities_1.normalizeName); const NORMALIZED_DWH_LOADED_TIMESTAMP_NAMES = defaults_1.DWH_LOADED_TIMESTAMP_NAMES.map(utilities_1.normalizeName); const normalizedColumns = Object.keys(metaData).map(utilities_1.normalizeName); // Check if columns already exist const hasCreatedAt = normalizedColumns.some(col => NORMALIZED_CREATED_TIMESTAMP_NAMES.includes(col)); const hasModifiedAt = normalizedColumns.some(col => NORMALIZED_MODIFIED_TIMESTAMP_NAMES.includes(col)); const hasLoadedAt = normalizedColumns.some(col => NORMALIZED_DWH_LOADED_TIMESTAMP_NAMES.includes(col)); // Add missing timestamp columns if (!hasCreatedAt) { metaData["dwh_created_at"] = { type: "datetime", allowNull: false, calculated: true, updatedCalculated: false, calculatedDefault: startDate.toISOString() }; } if (!hasModifiedAt) { metaData["dwh_modified_at"] = { type: "datetime", allowNull: true, calculated: true, updatedCalculated: true, calculatedDefault: startDate.toISOString() }; } if (!hasLoadedAt) { metaData["dwh_loaded_at"] = { type: "datetime", allowNull: true, calculated: true, updatedCalculated: true, calculatedDefault: startDate.toISOString() }; } return metaData; }