@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.cjs CHANGED
@@ -660,7 +660,7 @@ var DependencyResolver = class {
660
660
  } else if (import_fs3.default.existsSync(file)) {
661
661
  filePath = import_path2.default.resolve(file);
662
662
  } else {
663
- filePath = import_path2.default.join(process.cwd(), "dbcube", "cubes", file);
663
+ filePath = import_path2.default.join(process.cwd(), "dbcube", file);
664
664
  }
665
665
  try {
666
666
  const tableNameResult = FileUtils_default.extracTableNameFromCube(filePath);
@@ -757,7 +757,6 @@ var DependencyResolver = class {
757
757
  }
758
758
  }
759
759
  if (result.length !== dependencies.length) {
760
- console.warn("\u26A0\uFE0F Circular dependencies detected in tables. Some tables may not execute in optimal order.");
761
760
  for (const dep of dependencies) {
762
761
  if (!result.includes(dep.tableName)) {
763
762
  result.push(dep.tableName);
@@ -778,7 +777,6 @@ var DependencyResolver = class {
778
777
  import_fs3.default.mkdirSync(dbcubeDir, { recursive: true });
779
778
  }
780
779
  import_fs3.default.writeFileSync(orderFile, JSON.stringify(order, null, 2), "utf8");
781
- console.log(`\u{1F4C4} Execution order saved to: ${import_path2.default.relative(projectRoot, orderFile)}`);
782
780
  } catch (error) {
783
781
  console.error("\u274C Failed to save execution order:", error);
784
782
  }
@@ -806,14 +804,13 @@ var DependencyResolver = class {
806
804
  static orderCubeFiles(cubeFiles, cubeType) {
807
805
  const executionOrder = this.loadExecutionOrder();
808
806
  if (!executionOrder) {
809
- console.log("\u{1F4C4} No execution order found, processing files in current order");
810
807
  return cubeFiles;
811
808
  }
812
809
  const orderList = cubeType === "table" ? executionOrder.tables : executionOrder.seeders;
813
810
  const orderedFiles = [];
814
811
  const fileMap = /* @__PURE__ */ new Map();
815
812
  for (const file of cubeFiles) {
816
- const filePath = import_path2.default.isAbsolute(file) ? file : import_path2.default.join(process.cwd(), "dbcube", "cubes", file);
813
+ const filePath = import_path2.default.isAbsolute(file) ? file : import_path2.default.join(process.cwd(), "dbcube", file);
817
814
  const tableNameResult = FileUtils_default.extracTableNameFromCube(filePath);
818
815
  const tableName = tableNameResult.status === 200 ? tableNameResult.message : import_path2.default.basename(file, `.${cubeType}.cube`);
819
816
  fileMap.set(tableName, file);
@@ -827,7 +824,6 @@ var DependencyResolver = class {
827
824
  for (const [, file] of fileMap) {
828
825
  orderedFiles.push(file);
829
826
  }
830
- console.log(`\u{1F4CB} Using dependency order: ${orderList.join(" \u2192 ")}`);
831
827
  return orderedFiles;
832
828
  }
833
829
  };
@@ -934,6 +930,62 @@ var Schema = class {
934
930
  return 1;
935
931
  }
936
932
  }
933
+ /**
934
+ * Extracts foreign key dependencies from a cube file
935
+ */
936
+ extractForeignKeyDependencies(filePath) {
937
+ const dependencies = [];
938
+ try {
939
+ const content = import_fs4.default.readFileSync(filePath, "utf8");
940
+ const lines = content.split("\n");
941
+ let insideForeignKey = false;
942
+ let braceCount = 0;
943
+ for (const line of lines) {
944
+ if (/foreign\s*:\s*\{/.test(line)) {
945
+ insideForeignKey = true;
946
+ braceCount = 1;
947
+ const sameLineMatch = line.match(/table\s*:\s*["']([^"']+)["']/);
948
+ if (sameLineMatch) {
949
+ dependencies.push(sameLineMatch[1]);
950
+ insideForeignKey = false;
951
+ braceCount = 0;
952
+ }
953
+ continue;
954
+ }
955
+ if (insideForeignKey) {
956
+ braceCount += (line.match(/\{/g) || []).length;
957
+ braceCount -= (line.match(/\}/g) || []).length;
958
+ const tableMatch = line.match(/table\s*:\s*["']([^"']+)["']/);
959
+ if (tableMatch) {
960
+ dependencies.push(tableMatch[1]);
961
+ }
962
+ if (braceCount === 0) {
963
+ insideForeignKey = false;
964
+ }
965
+ }
966
+ }
967
+ } catch (error) {
968
+ console.error(`Error reading dependencies from ${filePath}:`, error);
969
+ }
970
+ return dependencies;
971
+ }
972
+ /**
973
+ * Finds the line number where a foreign key table reference is located
974
+ */
975
+ findForeignKeyLineNumber(filePath, tableName) {
976
+ try {
977
+ const content = import_fs4.default.readFileSync(filePath, "utf8");
978
+ const lines = content.split("\n");
979
+ for (let i = 0; i < lines.length; i++) {
980
+ if (lines[i].includes(`table: "${tableName}"`) || lines[i].includes(`table: '${tableName}'`)) {
981
+ return i + 1;
982
+ }
983
+ }
984
+ return 1;
985
+ } catch {
986
+ return 1;
987
+ }
988
+ }
937
989
  async createDatabase() {
938
990
  const startTime = Date.now();
939
991
  const rootPath = import_path3.default.resolve(process.cwd());
@@ -980,15 +1032,14 @@ var Schema = class {
980
1032
  }
981
1033
  async refreshTables() {
982
1034
  const startTime = Date.now();
983
- const cubesDir = import_path3.default.join(process.cwd(), "dbcube", "cubes");
1035
+ const cubesDir = import_path3.default.join(process.cwd(), "dbcube");
984
1036
  if (!import_fs4.default.existsSync(cubesDir)) {
985
1037
  throw new Error("\u274C The cubes folder does not exist");
986
1038
  }
987
- const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "table.cube");
1039
+ const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".table.cube");
988
1040
  if (cubeFiles.length === 0) {
989
1041
  throw new Error("\u274C There are no cubes to execute");
990
1042
  }
991
- console.log("\u{1F504} Resolving table dependencies...");
992
1043
  DependencyResolver.resolveDependencies(cubeFiles, "table");
993
1044
  const orderedCubeFiles = DependencyResolver.orderCubeFiles(cubeFiles, "table");
994
1045
  UIUtils.showOperationHeader("EXECUTING REFRESH TABLES", this.name, "\u{1F504}");
@@ -997,6 +1048,7 @@ var Schema = class {
997
1048
  let errorCount = 0;
998
1049
  const processedTables = [];
999
1050
  const errors = [];
1051
+ const failedTables = /* @__PURE__ */ new Set();
1000
1052
  for (let index = 0; index < orderedCubeFiles.length; index++) {
1001
1053
  const file = orderedCubeFiles[index];
1002
1054
  const filePath = import_path3.default.isAbsolute(file) ? file : import_path3.default.join(cubesDir, file);
@@ -1010,6 +1062,22 @@ var Schema = class {
1010
1062
  if (!validation.isValid && validation.error) {
1011
1063
  UIUtils.showItemError(tableName, validation.error.error);
1012
1064
  errors.push(validation.error);
1065
+ failedTables.add(tableName);
1066
+ errorCount++;
1067
+ continue;
1068
+ }
1069
+ const dependencies = this.extractForeignKeyDependencies(filePath);
1070
+ const missingDependencies = dependencies.filter((dep) => failedTables.has(dep));
1071
+ if (missingDependencies.length > 0) {
1072
+ const dependencyError = {
1073
+ itemName: tableName,
1074
+ error: `Cannot refresh table '${tableName}' because it depends on failed table(s): ${missingDependencies.join(", ")}`,
1075
+ filePath,
1076
+ lineNumber: this.findForeignKeyLineNumber(filePath, missingDependencies[0])
1077
+ };
1078
+ UIUtils.showItemError(tableName, dependencyError.error);
1079
+ errors.push(dependencyError);
1080
+ failedTables.add(tableName);
1013
1081
  errorCount++;
1014
1082
  continue;
1015
1083
  }
@@ -1066,6 +1134,7 @@ var Schema = class {
1066
1134
  };
1067
1135
  UIUtils.showItemError(tableName, error.message);
1068
1136
  errors.push(processError);
1137
+ failedTables.add(tableName);
1069
1138
  errorCount++;
1070
1139
  }
1071
1140
  }
@@ -1085,15 +1154,14 @@ var Schema = class {
1085
1154
  }
1086
1155
  async freshTables() {
1087
1156
  const startTime = Date.now();
1088
- const cubesDir = import_path3.default.join(process.cwd(), "dbcube", "cubes");
1157
+ const cubesDir = import_path3.default.join(process.cwd(), "dbcube");
1089
1158
  if (!import_fs4.default.existsSync(cubesDir)) {
1090
1159
  throw new Error("\u274C The cubes folder does not exist");
1091
1160
  }
1092
- const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "table.cube");
1161
+ const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".table.cube");
1093
1162
  if (cubeFiles.length === 0) {
1094
1163
  throw new Error("\u274C There are no cubes to execute");
1095
1164
  }
1096
- console.log("\u{1F504} Resolving table dependencies...");
1097
1165
  DependencyResolver.resolveDependencies(cubeFiles, "table");
1098
1166
  const orderedCubeFiles = DependencyResolver.orderCubeFiles(cubeFiles, "table");
1099
1167
  UIUtils.showOperationHeader("EXECUTING FRESH TABLES", this.name);
@@ -1102,6 +1170,7 @@ var Schema = class {
1102
1170
  let errorCount = 0;
1103
1171
  const processedTables = [];
1104
1172
  const errors = [];
1173
+ const failedTables = /* @__PURE__ */ new Set();
1105
1174
  for (let index = 0; index < orderedCubeFiles.length; index++) {
1106
1175
  const file = orderedCubeFiles[index];
1107
1176
  const filePath = import_path3.default.isAbsolute(file) ? file : import_path3.default.join(cubesDir, file);
@@ -1115,6 +1184,22 @@ var Schema = class {
1115
1184
  if (!validation.isValid && validation.error) {
1116
1185
  UIUtils.showItemError(tableName, validation.error.error);
1117
1186
  errors.push(validation.error);
1187
+ failedTables.add(tableName);
1188
+ errorCount++;
1189
+ continue;
1190
+ }
1191
+ const dependencies = this.extractForeignKeyDependencies(filePath);
1192
+ const missingDependencies = dependencies.filter((dep) => failedTables.has(dep));
1193
+ if (missingDependencies.length > 0) {
1194
+ const dependencyError = {
1195
+ itemName: tableName,
1196
+ error: `Cannot create table '${tableName}' because it depends on failed table(s): ${missingDependencies.join(", ")}`,
1197
+ filePath,
1198
+ lineNumber: this.findForeignKeyLineNumber(filePath, missingDependencies[0])
1199
+ };
1200
+ UIUtils.showItemError(tableName, dependencyError.error);
1201
+ errors.push(dependencyError);
1202
+ failedTables.add(tableName);
1118
1203
  errorCount++;
1119
1204
  continue;
1120
1205
  }
@@ -1169,6 +1254,7 @@ var Schema = class {
1169
1254
  };
1170
1255
  UIUtils.showItemError(tableName, error.message);
1171
1256
  errors.push(processError);
1257
+ failedTables.add(tableName);
1172
1258
  errorCount++;
1173
1259
  }
1174
1260
  }
@@ -1188,11 +1274,11 @@ var Schema = class {
1188
1274
  }
1189
1275
  async executeSeeders() {
1190
1276
  const startTime = Date.now();
1191
- const cubesDir = import_path3.default.join(process.cwd(), "dbcube", "cubes");
1277
+ const cubesDir = import_path3.default.join(process.cwd(), "dbcube");
1192
1278
  if (!import_fs4.default.existsSync(cubesDir)) {
1193
1279
  throw new Error("\u274C The cubes folder does not exist");
1194
1280
  }
1195
- const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "seeder.cube");
1281
+ const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".seeder.cube");
1196
1282
  if (cubeFiles.length === 0) {
1197
1283
  throw new Error("\u274C There are no cubes to execute");
1198
1284
  }
@@ -1260,12 +1346,12 @@ var Schema = class {
1260
1346
  }
1261
1347
  async executeTriggers() {
1262
1348
  const startTime = Date.now();
1263
- const cubesDir = import_path3.default.join(process.cwd(), "dbcube", "cubes");
1349
+ const cubesDir = import_path3.default.join(process.cwd(), "dbcube");
1264
1350
  const triggersDirExit = import_path3.default.join(process.cwd(), "dbcube", "triggers");
1265
1351
  if (!import_fs4.default.existsSync(cubesDir)) {
1266
1352
  throw new Error("\u274C The cubes folder does not exist");
1267
1353
  }
1268
- const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "trigger.cube");
1354
+ const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".trigger.cube");
1269
1355
  if (cubeFiles.length === 0) {
1270
1356
  throw new Error("\u274C There are no cubes to execute");
1271
1357
  }