UNPKG

@magic-mustard/sqlsync

Version:

SQLSync simplifies database schema evolution by allowing a declarative approach to table management

109 lines (108 loc) 4.76 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.SqlFiles = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * SqlFiles manages the ordered traversal of SQL files based on the structure provided by FileLoader. * It provides a 'next()' method to retrieve the next file in the specified order. */ class SqlFiles { constructor(schemaStructure) { this.schemaStructure = schemaStructure; this.currentCategoryIndex = 0; this.currentIndex = {}; this.currentSubIndex = {}; this.categories = []; this.categories = Object.entries(schemaStructure) .filter(([key, value]) => value && typeof value === 'object' && 'order' in value && Array.isArray(value.order)) .map(([key]) => key); if (schemaStructure['schema'] && 'order' in schemaStructure['schema'] && Array.isArray(schemaStructure['schema'].order) && !this.categories.includes('schema')) { this.categories.unshift('schema'); } } /** * Retrieves the next file in the ordered structure. * @returns SqlFileDetails of the next file, or null if there are no more files. */ next() { while (this.currentCategoryIndex < this.categories.length) { const category = this.categories[this.currentCategoryIndex]; const categoryData = this.schemaStructure[category]; const orderList = categoryData.order || []; if (this.currentIndex[category] === undefined) { this.currentIndex[category] = 0; this.currentSubIndex[category] = 0; } if (this.currentIndex[category] < orderList.length) { const itemPath = orderList[this.currentIndex[category]]; if (fs.statSync(itemPath).isDirectory()) { const subDirOrder = categoryData.orderedSubdirectoryFileOrder || []; while (this.currentSubIndex[category] < subDirOrder.length) { const subItem = subDirOrder[this.currentSubIndex[category]]; const subItemPath = path.join(itemPath, subItem); if (fs.existsSync(subItemPath) && fs.statSync(subItemPath).isFile() && subItem.endsWith('.sql')) { this.currentSubIndex[category]++; return { location: subItemPath, contents: fs.readFileSync(subItemPath, 'utf8'), }; } else { this.currentSubIndex[category]++; } } this.currentSubIndex[category] = 0; this.currentIndex[category]++; } else { this.currentIndex[category]++; return { location: itemPath, contents: fs.readFileSync(itemPath, 'utf8'), }; } } else { this.currentCategoryIndex++; this.currentIndex[category] = 0; this.currentSubIndex[category] = 0; } } return null; } } exports.SqlFiles = SqlFiles;