@dbcube/schema-builder 1.0.18 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -627,7 +627,7 @@ var DependencyResolver = class {
627
627
  } else if (fs4.existsSync(file)) {
628
628
  filePath = path3.resolve(file);
629
629
  } else {
630
- filePath = path3.join(process.cwd(), "dbcube", "cubes", file);
630
+ filePath = path3.join(process.cwd(), "dbcube", file);
631
631
  }
632
632
  try {
633
633
  const tableNameResult = FileUtils_default.extracTableNameFromCube(filePath);
@@ -724,7 +724,6 @@ var DependencyResolver = class {
724
724
  }
725
725
  }
726
726
  if (result.length !== dependencies.length) {
727
- console.warn("\u26A0\uFE0F Circular dependencies detected in tables. Some tables may not execute in optimal order.");
728
727
  for (const dep of dependencies) {
729
728
  if (!result.includes(dep.tableName)) {
730
729
  result.push(dep.tableName);
@@ -745,7 +744,6 @@ var DependencyResolver = class {
745
744
  fs4.mkdirSync(dbcubeDir, { recursive: true });
746
745
  }
747
746
  fs4.writeFileSync(orderFile, JSON.stringify(order, null, 2), "utf8");
748
- console.log(`\u{1F4C4} Execution order saved to: ${path3.relative(projectRoot, orderFile)}`);
749
747
  } catch (error) {
750
748
  console.error("\u274C Failed to save execution order:", error);
751
749
  }
@@ -773,14 +771,13 @@ var DependencyResolver = class {
773
771
  static orderCubeFiles(cubeFiles, cubeType) {
774
772
  const executionOrder = this.loadExecutionOrder();
775
773
  if (!executionOrder) {
776
- console.log("\u{1F4C4} No execution order found, processing files in current order");
777
774
  return cubeFiles;
778
775
  }
779
776
  const orderList = cubeType === "table" ? executionOrder.tables : executionOrder.seeders;
780
777
  const orderedFiles = [];
781
778
  const fileMap = /* @__PURE__ */ new Map();
782
779
  for (const file of cubeFiles) {
783
- const filePath = path3.isAbsolute(file) ? file : path3.join(process.cwd(), "dbcube", "cubes", file);
780
+ const filePath = path3.isAbsolute(file) ? file : path3.join(process.cwd(), "dbcube", file);
784
781
  const tableNameResult = FileUtils_default.extracTableNameFromCube(filePath);
785
782
  const tableName = tableNameResult.status === 200 ? tableNameResult.message : path3.basename(file, `.${cubeType}.cube`);
786
783
  fileMap.set(tableName, file);
@@ -794,7 +791,6 @@ var DependencyResolver = class {
794
791
  for (const [, file] of fileMap) {
795
792
  orderedFiles.push(file);
796
793
  }
797
- console.log(`\u{1F4CB} Using dependency order: ${orderList.join(" \u2192 ")}`);
798
794
  return orderedFiles;
799
795
  }
800
796
  };
@@ -901,6 +897,62 @@ var Schema = class {
901
897
  return 1;
902
898
  }
903
899
  }
900
+ /**
901
+ * Extracts foreign key dependencies from a cube file
902
+ */
903
+ extractForeignKeyDependencies(filePath) {
904
+ const dependencies = [];
905
+ try {
906
+ const content = fs5.readFileSync(filePath, "utf8");
907
+ const lines = content.split("\n");
908
+ let insideForeignKey = false;
909
+ let braceCount = 0;
910
+ for (const line of lines) {
911
+ if (/foreign\s*:\s*\{/.test(line)) {
912
+ insideForeignKey = true;
913
+ braceCount = 1;
914
+ const sameLineMatch = line.match(/table\s*:\s*["']([^"']+)["']/);
915
+ if (sameLineMatch) {
916
+ dependencies.push(sameLineMatch[1]);
917
+ insideForeignKey = false;
918
+ braceCount = 0;
919
+ }
920
+ continue;
921
+ }
922
+ if (insideForeignKey) {
923
+ braceCount += (line.match(/\{/g) || []).length;
924
+ braceCount -= (line.match(/\}/g) || []).length;
925
+ const tableMatch = line.match(/table\s*:\s*["']([^"']+)["']/);
926
+ if (tableMatch) {
927
+ dependencies.push(tableMatch[1]);
928
+ }
929
+ if (braceCount === 0) {
930
+ insideForeignKey = false;
931
+ }
932
+ }
933
+ }
934
+ } catch (error) {
935
+ console.error(`Error reading dependencies from ${filePath}:`, error);
936
+ }
937
+ return dependencies;
938
+ }
939
+ /**
940
+ * Finds the line number where a foreign key table reference is located
941
+ */
942
+ findForeignKeyLineNumber(filePath, tableName) {
943
+ try {
944
+ const content = fs5.readFileSync(filePath, "utf8");
945
+ const lines = content.split("\n");
946
+ for (let i = 0; i < lines.length; i++) {
947
+ if (lines[i].includes(`table: "${tableName}"`) || lines[i].includes(`table: '${tableName}'`)) {
948
+ return i + 1;
949
+ }
950
+ }
951
+ return 1;
952
+ } catch {
953
+ return 1;
954
+ }
955
+ }
904
956
  async createDatabase() {
905
957
  const startTime = Date.now();
906
958
  const rootPath = path4.resolve(process.cwd());
@@ -947,15 +999,14 @@ var Schema = class {
947
999
  }
948
1000
  async refreshTables() {
949
1001
  const startTime = Date.now();
950
- const cubesDir = path4.join(process.cwd(), "dbcube", "cubes");
1002
+ const cubesDir = path4.join(process.cwd(), "dbcube");
951
1003
  if (!fs5.existsSync(cubesDir)) {
952
1004
  throw new Error("\u274C The cubes folder does not exist");
953
1005
  }
954
- const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "table.cube");
1006
+ const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".table.cube");
955
1007
  if (cubeFiles.length === 0) {
956
1008
  throw new Error("\u274C There are no cubes to execute");
957
1009
  }
958
- console.log("\u{1F504} Resolving table dependencies...");
959
1010
  DependencyResolver.resolveDependencies(cubeFiles, "table");
960
1011
  const orderedCubeFiles = DependencyResolver.orderCubeFiles(cubeFiles, "table");
961
1012
  UIUtils.showOperationHeader("EXECUTING REFRESH TABLES", this.name, "\u{1F504}");
@@ -964,6 +1015,7 @@ var Schema = class {
964
1015
  let errorCount = 0;
965
1016
  const processedTables = [];
966
1017
  const errors = [];
1018
+ const failedTables = /* @__PURE__ */ new Set();
967
1019
  for (let index = 0; index < orderedCubeFiles.length; index++) {
968
1020
  const file = orderedCubeFiles[index];
969
1021
  const filePath = path4.isAbsolute(file) ? file : path4.join(cubesDir, file);
@@ -977,6 +1029,22 @@ var Schema = class {
977
1029
  if (!validation.isValid && validation.error) {
978
1030
  UIUtils.showItemError(tableName, validation.error.error);
979
1031
  errors.push(validation.error);
1032
+ failedTables.add(tableName);
1033
+ errorCount++;
1034
+ continue;
1035
+ }
1036
+ const dependencies = this.extractForeignKeyDependencies(filePath);
1037
+ const missingDependencies = dependencies.filter((dep) => failedTables.has(dep));
1038
+ if (missingDependencies.length > 0) {
1039
+ const dependencyError = {
1040
+ itemName: tableName,
1041
+ error: `Cannot refresh table '${tableName}' because it depends on failed table(s): ${missingDependencies.join(", ")}`,
1042
+ filePath,
1043
+ lineNumber: this.findForeignKeyLineNumber(filePath, missingDependencies[0])
1044
+ };
1045
+ UIUtils.showItemError(tableName, dependencyError.error);
1046
+ errors.push(dependencyError);
1047
+ failedTables.add(tableName);
980
1048
  errorCount++;
981
1049
  continue;
982
1050
  }
@@ -1033,6 +1101,7 @@ var Schema = class {
1033
1101
  };
1034
1102
  UIUtils.showItemError(tableName, error.message);
1035
1103
  errors.push(processError);
1104
+ failedTables.add(tableName);
1036
1105
  errorCount++;
1037
1106
  }
1038
1107
  }
@@ -1052,15 +1121,14 @@ var Schema = class {
1052
1121
  }
1053
1122
  async freshTables() {
1054
1123
  const startTime = Date.now();
1055
- const cubesDir = path4.join(process.cwd(), "dbcube", "cubes");
1124
+ const cubesDir = path4.join(process.cwd(), "dbcube");
1056
1125
  if (!fs5.existsSync(cubesDir)) {
1057
1126
  throw new Error("\u274C The cubes folder does not exist");
1058
1127
  }
1059
- const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "table.cube");
1128
+ const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".table.cube");
1060
1129
  if (cubeFiles.length === 0) {
1061
1130
  throw new Error("\u274C There are no cubes to execute");
1062
1131
  }
1063
- console.log("\u{1F504} Resolving table dependencies...");
1064
1132
  DependencyResolver.resolveDependencies(cubeFiles, "table");
1065
1133
  const orderedCubeFiles = DependencyResolver.orderCubeFiles(cubeFiles, "table");
1066
1134
  UIUtils.showOperationHeader("EXECUTING FRESH TABLES", this.name);
@@ -1069,6 +1137,7 @@ var Schema = class {
1069
1137
  let errorCount = 0;
1070
1138
  const processedTables = [];
1071
1139
  const errors = [];
1140
+ const failedTables = /* @__PURE__ */ new Set();
1072
1141
  for (let index = 0; index < orderedCubeFiles.length; index++) {
1073
1142
  const file = orderedCubeFiles[index];
1074
1143
  const filePath = path4.isAbsolute(file) ? file : path4.join(cubesDir, file);
@@ -1082,6 +1151,22 @@ var Schema = class {
1082
1151
  if (!validation.isValid && validation.error) {
1083
1152
  UIUtils.showItemError(tableName, validation.error.error);
1084
1153
  errors.push(validation.error);
1154
+ failedTables.add(tableName);
1155
+ errorCount++;
1156
+ continue;
1157
+ }
1158
+ const dependencies = this.extractForeignKeyDependencies(filePath);
1159
+ const missingDependencies = dependencies.filter((dep) => failedTables.has(dep));
1160
+ if (missingDependencies.length > 0) {
1161
+ const dependencyError = {
1162
+ itemName: tableName,
1163
+ error: `Cannot create table '${tableName}' because it depends on failed table(s): ${missingDependencies.join(", ")}`,
1164
+ filePath,
1165
+ lineNumber: this.findForeignKeyLineNumber(filePath, missingDependencies[0])
1166
+ };
1167
+ UIUtils.showItemError(tableName, dependencyError.error);
1168
+ errors.push(dependencyError);
1169
+ failedTables.add(tableName);
1085
1170
  errorCount++;
1086
1171
  continue;
1087
1172
  }
@@ -1136,6 +1221,7 @@ var Schema = class {
1136
1221
  };
1137
1222
  UIUtils.showItemError(tableName, error.message);
1138
1223
  errors.push(processError);
1224
+ failedTables.add(tableName);
1139
1225
  errorCount++;
1140
1226
  }
1141
1227
  }
@@ -1155,11 +1241,11 @@ var Schema = class {
1155
1241
  }
1156
1242
  async executeSeeders() {
1157
1243
  const startTime = Date.now();
1158
- const cubesDir = path4.join(process.cwd(), "dbcube", "cubes");
1244
+ const cubesDir = path4.join(process.cwd(), "dbcube");
1159
1245
  if (!fs5.existsSync(cubesDir)) {
1160
1246
  throw new Error("\u274C The cubes folder does not exist");
1161
1247
  }
1162
- const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "seeder.cube");
1248
+ const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".seeder.cube");
1163
1249
  if (cubeFiles.length === 0) {
1164
1250
  throw new Error("\u274C There are no cubes to execute");
1165
1251
  }
@@ -1227,12 +1313,12 @@ var Schema = class {
1227
1313
  }
1228
1314
  async executeTriggers() {
1229
1315
  const startTime = Date.now();
1230
- const cubesDir = path4.join(process.cwd(), "dbcube", "cubes");
1316
+ const cubesDir = path4.join(process.cwd(), "dbcube");
1231
1317
  const triggersDirExit = path4.join(process.cwd(), "dbcube", "triggers");
1232
1318
  if (!fs5.existsSync(cubesDir)) {
1233
1319
  throw new Error("\u274C The cubes folder does not exist");
1234
1320
  }
1235
- const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "trigger.cube");
1321
+ const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".trigger.cube");
1236
1322
  if (cubeFiles.length === 0) {
1237
1323
  throw new Error("\u274C There are no cubes to execute");
1238
1324
  }