UNPKG

@grouparoo/core

Version:
79 lines (78 loc) 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TableSpeculation = void 0; const configWriter_1 = require("../modules/configWriter"); var TableSpeculation; (function (TableSpeculation) { const uniqueMatchers = [ /email/, /^id$/, /^guid$/, /^uuid$/, /userid/, /user_id/, /phone/, /mobile/, ]; function isUniqueColumn(columnName) { if (!columnName) return false; for (const matcher of uniqueMatchers) { if (columnName.toLowerCase().match(matcher)) return true; } return false; } TableSpeculation.isUniqueColumn = isUniqueColumn; function columnType(columnName, samples = [], defaultDatabaseType = "string") { let databaseType = defaultDatabaseType !== null && defaultDatabaseType !== void 0 ? defaultDatabaseType : "string"; const name = columnName.toLowerCase(); const value = samples.find((v) => v !== null && v !== undefined); const exists = value !== undefined && value !== null; if (exists && typeof value === "number") databaseType = "float"; if (exists && (value === false || value === true)) databaseType = "boolean"; if (exists && value instanceof Date) databaseType = "date"; if (databaseType === "string") { if (name.includes("email")) return "email"; if (name.includes("phone")) return "phoneNumber"; if (name.includes("mobile")) return "phoneNumber"; } if (databaseType === "float") { if (name === "id") return "integer"; if (name === "guid") return "integer"; if (name === "uuid") return "integer"; if (name.match(/_id$/)) return "integer"; if (name.match(/Id$/)) return "integer"; if (name.match(/_guid$/)) return "integer"; if (name.match(/Guid$/)) return "integer"; if (name.match(/_uuid$/)) return "integer"; if (name.match(/Uuid$/)) return "integer"; } return databaseType; } TableSpeculation.columnType = columnType; function suggestKey(key, model, existingProperties) { // Check for existing keys in a case-insensitive way const proposedKey = configWriter_1.ConfigWriter.generateId(key); const matchingProperty = existingProperties.find((p) => configWriter_1.ConfigWriter.generateId(p.key) === proposedKey); return matchingProperty ? `${configWriter_1.ConfigWriter.generateId(model.name)}_${proposedKey}` : proposedKey; } TableSpeculation.suggestKey = suggestKey; })(TableSpeculation = exports.TableSpeculation || (exports.TableSpeculation = {}));