@wecode-team/cms-supabase-api 0.1.30 → 0.1.31

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.esm.js CHANGED
@@ -1031,7 +1031,72 @@ var DynamicTableService = /*#__PURE__*/function () {
1031
1031
  _classCallCheck(this, DynamicTableService);
1032
1032
  this.supabase = getSupabase();
1033
1033
  }
1034
+ /**
1035
+ * 默认按 created_at 倒序(新数据在前)。
1036
+ * 兼容:如果表不存在 created_at 字段,则回退按 id 倒序。
1037
+ */
1034
1038
  return _createClass(DynamicTableService, [{
1039
+ key: "orderByCreatedAtOrFallback",
1040
+ value: (function () {
1041
+ var _orderByCreatedAtOrFallback = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(buildQuery, offset, limit) {
1042
+ var _first$error;
1043
+ var first, msg, isMissingCreatedAt, second;
1044
+ return _regeneratorRuntime.wrap(function (_context) {
1045
+ while (1) switch (_context.prev = _context.next) {
1046
+ case 0:
1047
+ _context.next = 1;
1048
+ return buildQuery().order("created_at", {
1049
+ ascending: false
1050
+ }).order("id", {
1051
+ ascending: false
1052
+ }).range(offset, offset + limit - 1);
1053
+ case 1:
1054
+ first = _context.sent;
1055
+ if (first.error) {
1056
+ _context.next = 2;
1057
+ break;
1058
+ }
1059
+ return _context.abrupt("return", {
1060
+ data: first.data || [],
1061
+ count: first.count || 0
1062
+ });
1063
+ case 2:
1064
+ msg = String(((_first$error = first.error) === null || _first$error === void 0 ? void 0 : _first$error.message) || "");
1065
+ isMissingCreatedAt = msg.includes("created_at") || msg.toLowerCase().includes("column") || msg.toLowerCase().includes("does not exist");
1066
+ if (isMissingCreatedAt) {
1067
+ _context.next = 3;
1068
+ break;
1069
+ }
1070
+ throw first.error;
1071
+ case 3:
1072
+ _context.next = 4;
1073
+ return buildQuery().order("id", {
1074
+ ascending: false
1075
+ }).range(offset, offset + limit - 1);
1076
+ case 4:
1077
+ second = _context.sent;
1078
+ if (!second.error) {
1079
+ _context.next = 5;
1080
+ break;
1081
+ }
1082
+ throw second.error;
1083
+ case 5:
1084
+ return _context.abrupt("return", {
1085
+ data: second.data || [],
1086
+ count: second.count || 0
1087
+ });
1088
+ case 6:
1089
+ case "end":
1090
+ return _context.stop();
1091
+ }
1092
+ }, _callee);
1093
+ }));
1094
+ function orderByCreatedAtOrFallback(_x, _x2, _x3) {
1095
+ return _orderByCreatedAtOrFallback.apply(this, arguments);
1096
+ }
1097
+ return orderByCreatedAtOrFallback;
1098
+ }())
1099
+ }, {
1035
1100
  key: "applySupabaseFilters",
1036
1101
  value: function applySupabaseFilters(query, filters) {
1037
1102
  if (!filters || filters.length === 0) return query;
@@ -1107,12 +1172,12 @@ var DynamicTableService = /*#__PURE__*/function () {
1107
1172
  }, {
1108
1173
  key: "createTable",
1109
1174
  value: (function () {
1110
- var _createTable = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(tableName, schema, baseTableName) {
1175
+ var _createTable = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(tableName, schema, baseTableName) {
1111
1176
  var tablePrefix, createTableSQL, foreignKeys, _yield$this$supabase$, error, _t;
1112
- return _regeneratorRuntime.wrap(function (_context) {
1113
- while (1) switch (_context.prev = _context.next) {
1177
+ return _regeneratorRuntime.wrap(function (_context2) {
1178
+ while (1) switch (_context2.prev = _context2.next) {
1114
1179
  case 0:
1115
- _context.prev = 0;
1180
+ _context2.prev = 0;
1116
1181
  // 计算表名前缀(用于关联表)
1117
1182
  tablePrefix = baseTableName ? this.extractTablePrefix(tableName, baseTableName) : ""; // 构建CREATE TABLE SQL语句
1118
1183
  createTableSQL = "CREATE TABLE IF NOT EXISTS \"".concat(tableName, "\" (\n");
@@ -1165,33 +1230,33 @@ var DynamicTableService = /*#__PURE__*/function () {
1165
1230
  }
1166
1231
  createTableSQL += "\n);";
1167
1232
  // 执行创建表的SQL
1168
- _context.next = 1;
1233
+ _context2.next = 1;
1169
1234
  return this.supabase.rpc("execute_sql", {
1170
1235
  sql_query: createTableSQL
1171
1236
  });
1172
1237
  case 1:
1173
- _yield$this$supabase$ = _context.sent;
1238
+ _yield$this$supabase$ = _context2.sent;
1174
1239
  error = _yield$this$supabase$.error;
1175
1240
  if (!error) {
1176
- _context.next = 2;
1241
+ _context2.next = 2;
1177
1242
  break;
1178
1243
  }
1179
1244
  throw error;
1180
1245
  case 2:
1181
1246
  console.log("\u8868 ".concat(tableName, " \u521B\u5EFA\u6210\u529F"));
1182
- return _context.abrupt("return", true);
1247
+ return _context2.abrupt("return", true);
1183
1248
  case 3:
1184
- _context.prev = 3;
1185
- _t = _context["catch"](0);
1249
+ _context2.prev = 3;
1250
+ _t = _context2["catch"](0);
1186
1251
  console.error("\u521B\u5EFA\u8868 ".concat(tableName, " \u5931\u8D25:"), _t);
1187
- return _context.abrupt("return", false);
1252
+ return _context2.abrupt("return", false);
1188
1253
  case 4:
1189
1254
  case "end":
1190
- return _context.stop();
1255
+ return _context2.stop();
1191
1256
  }
1192
- }, _callee, this, [[0, 3]]);
1257
+ }, _callee2, this, [[0, 3]]);
1193
1258
  }));
1194
- function createTable(_x, _x2, _x3) {
1259
+ function createTable(_x4, _x5, _x6) {
1195
1260
  return _createTable.apply(this, arguments);
1196
1261
  }
1197
1262
  return createTable;
@@ -1203,39 +1268,39 @@ var DynamicTableService = /*#__PURE__*/function () {
1203
1268
  }, {
1204
1269
  key: "dropTable",
1205
1270
  value: (function () {
1206
- var _dropTable = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(tableName) {
1271
+ var _dropTable = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(tableName) {
1207
1272
  var _yield$this$supabase$2, error, _t2;
1208
- return _regeneratorRuntime.wrap(function (_context2) {
1209
- while (1) switch (_context2.prev = _context2.next) {
1273
+ return _regeneratorRuntime.wrap(function (_context3) {
1274
+ while (1) switch (_context3.prev = _context3.next) {
1210
1275
  case 0:
1211
- _context2.prev = 0;
1212
- _context2.next = 1;
1276
+ _context3.prev = 0;
1277
+ _context3.next = 1;
1213
1278
  return this.supabase.rpc("execute_sql", {
1214
1279
  sql_query: "DROP TABLE IF EXISTS \"".concat(tableName, "\";")
1215
1280
  });
1216
1281
  case 1:
1217
- _yield$this$supabase$2 = _context2.sent;
1282
+ _yield$this$supabase$2 = _context3.sent;
1218
1283
  error = _yield$this$supabase$2.error;
1219
1284
  if (!error) {
1220
- _context2.next = 2;
1285
+ _context3.next = 2;
1221
1286
  break;
1222
1287
  }
1223
1288
  throw error;
1224
1289
  case 2:
1225
1290
  console.log("\u8868 ".concat(tableName, " \u5220\u9664\u6210\u529F"));
1226
- return _context2.abrupt("return", true);
1291
+ return _context3.abrupt("return", true);
1227
1292
  case 3:
1228
- _context2.prev = 3;
1229
- _t2 = _context2["catch"](0);
1293
+ _context3.prev = 3;
1294
+ _t2 = _context3["catch"](0);
1230
1295
  console.error("\u5220\u9664\u8868 ".concat(tableName, " \u5931\u8D25:"), _t2);
1231
- return _context2.abrupt("return", false);
1296
+ return _context3.abrupt("return", false);
1232
1297
  case 4:
1233
1298
  case "end":
1234
- return _context2.stop();
1299
+ return _context3.stop();
1235
1300
  }
1236
- }, _callee2, this, [[0, 3]]);
1301
+ }, _callee3, this, [[0, 3]]);
1237
1302
  }));
1238
- function dropTable(_x4) {
1303
+ function dropTable(_x7) {
1239
1304
  return _dropTable.apply(this, arguments);
1240
1305
  }
1241
1306
  return dropTable;
@@ -1248,12 +1313,12 @@ var DynamicTableService = /*#__PURE__*/function () {
1248
1313
  }, {
1249
1314
  key: "dropAllForeignKeys",
1250
1315
  value: (function () {
1251
- var _dropAllForeignKeys = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(tableName) {
1316
+ var _dropAllForeignKeys = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4(tableName) {
1252
1317
  var parts, baseTableName, dropFKSQL, _yield$this$supabase$3, error, _t3;
1253
- return _regeneratorRuntime.wrap(function (_context3) {
1254
- while (1) switch (_context3.prev = _context3.next) {
1318
+ return _regeneratorRuntime.wrap(function (_context4) {
1319
+ while (1) switch (_context4.prev = _context4.next) {
1255
1320
  case 0:
1256
- _context3.prev = 0;
1321
+ _context4.prev = 0;
1257
1322
  // 获取可能的基础表名(去掉前缀)
1258
1323
  // 例如:jjj231122a342_posts -> posts
1259
1324
  parts = tableName.split('_');
@@ -1261,34 +1326,34 @@ var DynamicTableService = /*#__PURE__*/function () {
1261
1326
  dropFKSQL = "\n DO $$\n DECLARE\n r RECORD;\n BEGIN\n -- \u5C1D\u8BD5\u5339\u914D\u5B8C\u6574\u8868\u540D\n FOR r IN (\n SELECT tc.constraint_name\n FROM information_schema.table_constraints tc\n WHERE tc.table_name = '".concat(tableName, "'\n AND tc.constraint_type = 'FOREIGN KEY'\n )\n LOOP\n EXECUTE 'ALTER TABLE \"").concat(tableName, "\" DROP CONSTRAINT IF EXISTS \"' || r.constraint_name || '\"';\n RAISE NOTICE 'Dropped constraint: %', r.constraint_name;\n END LOOP;\n \n -- \u5C1D\u8BD5\u5339\u914D\u57FA\u7840\u8868\u540D\u7684\u7EA6\u675F\uFF08\u7EA6\u675F\u540D\u53EF\u80FD\u4E0D\u5305\u542B\u524D\u7F00\uFF09\n FOR r IN (\n SELECT tc.constraint_name\n FROM information_schema.table_constraints tc\n WHERE tc.table_name = '").concat(tableName, "'\n AND tc.constraint_type = 'FOREIGN KEY'\n AND tc.constraint_name LIKE '").concat(baseTableName, "%'\n )\n LOOP\n EXECUTE 'ALTER TABLE \"").concat(tableName, "\" DROP CONSTRAINT IF EXISTS \"' || r.constraint_name || '\"';\n RAISE NOTICE 'Dropped constraint (base match): %', r.constraint_name;\n END LOOP;\n END $$;\n ");
1262
1327
  console.log("\u5C1D\u8BD5\u5220\u9664\u8868 ".concat(tableName, " \u7684\u5916\u952E\u7EA6\u675F..."));
1263
1328
  console.log("\u57FA\u7840\u8868\u540D: ".concat(baseTableName));
1264
- _context3.next = 1;
1329
+ _context4.next = 1;
1265
1330
  return this.supabase.rpc("execute_sql", {
1266
1331
  sql_query: dropFKSQL
1267
1332
  });
1268
1333
  case 1:
1269
- _yield$this$supabase$3 = _context3.sent;
1334
+ _yield$this$supabase$3 = _context4.sent;
1270
1335
  error = _yield$this$supabase$3.error;
1271
1336
  if (!error) {
1272
- _context3.next = 2;
1337
+ _context4.next = 2;
1273
1338
  break;
1274
1339
  }
1275
1340
  console.error("execute_sql \u9519\u8BEF:", error);
1276
1341
  throw error;
1277
1342
  case 2:
1278
1343
  console.log("\u8868 ".concat(tableName, " \u7684\u5916\u952E\u7EA6\u675F\u5220\u9664\u64CD\u4F5C\u5DF2\u6267\u884C"));
1279
- return _context3.abrupt("return", true);
1344
+ return _context4.abrupt("return", true);
1280
1345
  case 3:
1281
- _context3.prev = 3;
1282
- _t3 = _context3["catch"](0);
1346
+ _context4.prev = 3;
1347
+ _t3 = _context4["catch"](0);
1283
1348
  console.error("\u5220\u9664\u8868 ".concat(tableName, " \u5916\u952E\u7EA6\u675F\u5931\u8D25:"), _t3);
1284
- return _context3.abrupt("return", false);
1349
+ return _context4.abrupt("return", false);
1285
1350
  case 4:
1286
1351
  case "end":
1287
- return _context3.stop();
1352
+ return _context4.stop();
1288
1353
  }
1289
- }, _callee3, this, [[0, 3]]);
1354
+ }, _callee4, this, [[0, 3]]);
1290
1355
  }));
1291
- function dropAllForeignKeys(_x5) {
1356
+ function dropAllForeignKeys(_x8) {
1292
1357
  return _dropAllForeignKeys.apply(this, arguments);
1293
1358
  }
1294
1359
  return dropAllForeignKeys;
@@ -1300,41 +1365,41 @@ var DynamicTableService = /*#__PURE__*/function () {
1300
1365
  }, {
1301
1366
  key: "dropForeignKeyByName",
1302
1367
  value: (function () {
1303
- var _dropForeignKeyByName = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4(tableName, constraintName) {
1368
+ var _dropForeignKeyByName = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5(tableName, constraintName) {
1304
1369
  var dropSQL, _yield$this$supabase$4, error, _t4;
1305
- return _regeneratorRuntime.wrap(function (_context4) {
1306
- while (1) switch (_context4.prev = _context4.next) {
1370
+ return _regeneratorRuntime.wrap(function (_context5) {
1371
+ while (1) switch (_context5.prev = _context5.next) {
1307
1372
  case 0:
1308
- _context4.prev = 0;
1373
+ _context5.prev = 0;
1309
1374
  dropSQL = "ALTER TABLE \"".concat(tableName, "\" DROP CONSTRAINT IF EXISTS \"").concat(constraintName, "\";");
1310
1375
  console.log("\u5220\u9664\u7EA6\u675F: ".concat(constraintName, " from ").concat(tableName));
1311
- _context4.next = 1;
1376
+ _context5.next = 1;
1312
1377
  return this.supabase.rpc("execute_sql", {
1313
1378
  sql_query: dropSQL
1314
1379
  });
1315
1380
  case 1:
1316
- _yield$this$supabase$4 = _context4.sent;
1381
+ _yield$this$supabase$4 = _context5.sent;
1317
1382
  error = _yield$this$supabase$4.error;
1318
1383
  if (!error) {
1319
- _context4.next = 2;
1384
+ _context5.next = 2;
1320
1385
  break;
1321
1386
  }
1322
1387
  throw error;
1323
1388
  case 2:
1324
1389
  console.log("\u5916\u952E\u7EA6\u675F ".concat(constraintName, " \u5DF2\u5220\u9664"));
1325
- return _context4.abrupt("return", true);
1390
+ return _context5.abrupt("return", true);
1326
1391
  case 3:
1327
- _context4.prev = 3;
1328
- _t4 = _context4["catch"](0);
1392
+ _context5.prev = 3;
1393
+ _t4 = _context5["catch"](0);
1329
1394
  console.error("\u5220\u9664\u5916\u952E\u7EA6\u675F ".concat(constraintName, " \u5931\u8D25:"), _t4);
1330
- return _context4.abrupt("return", false);
1395
+ return _context5.abrupt("return", false);
1331
1396
  case 4:
1332
1397
  case "end":
1333
- return _context4.stop();
1398
+ return _context5.stop();
1334
1399
  }
1335
- }, _callee4, this, [[0, 3]]);
1400
+ }, _callee5, this, [[0, 3]]);
1336
1401
  }));
1337
- function dropForeignKeyByName(_x6, _x7) {
1402
+ function dropForeignKeyByName(_x9, _x0) {
1338
1403
  return _dropForeignKeyByName.apply(this, arguments);
1339
1404
  }
1340
1405
  return dropForeignKeyByName;
@@ -1346,39 +1411,39 @@ var DynamicTableService = /*#__PURE__*/function () {
1346
1411
  }, {
1347
1412
  key: "dropForeignKey",
1348
1413
  value: (function () {
1349
- var _dropForeignKey = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5(tableName, constraintName) {
1414
+ var _dropForeignKey = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee6(tableName, constraintName) {
1350
1415
  var _yield$this$supabase$5, error, _t5;
1351
- return _regeneratorRuntime.wrap(function (_context5) {
1352
- while (1) switch (_context5.prev = _context5.next) {
1416
+ return _regeneratorRuntime.wrap(function (_context6) {
1417
+ while (1) switch (_context6.prev = _context6.next) {
1353
1418
  case 0:
1354
- _context5.prev = 0;
1355
- _context5.next = 1;
1419
+ _context6.prev = 0;
1420
+ _context6.next = 1;
1356
1421
  return this.supabase.rpc("execute_sql", {
1357
1422
  sql_query: "ALTER TABLE \"".concat(tableName, "\" DROP CONSTRAINT IF EXISTS \"").concat(constraintName, "\";")
1358
1423
  });
1359
1424
  case 1:
1360
- _yield$this$supabase$5 = _context5.sent;
1425
+ _yield$this$supabase$5 = _context6.sent;
1361
1426
  error = _yield$this$supabase$5.error;
1362
1427
  if (!error) {
1363
- _context5.next = 2;
1428
+ _context6.next = 2;
1364
1429
  break;
1365
1430
  }
1366
1431
  throw error;
1367
1432
  case 2:
1368
1433
  console.log("\u5916\u952E\u7EA6\u675F ".concat(constraintName, " \u5DF2\u4ECE\u8868 ").concat(tableName, " \u5220\u9664"));
1369
- return _context5.abrupt("return", true);
1434
+ return _context6.abrupt("return", true);
1370
1435
  case 3:
1371
- _context5.prev = 3;
1372
- _t5 = _context5["catch"](0);
1436
+ _context6.prev = 3;
1437
+ _t5 = _context6["catch"](0);
1373
1438
  console.error("\u5220\u9664\u5916\u952E\u7EA6\u675F\u5931\u8D25:", _t5);
1374
- return _context5.abrupt("return", false);
1439
+ return _context6.abrupt("return", false);
1375
1440
  case 4:
1376
1441
  case "end":
1377
- return _context5.stop();
1442
+ return _context6.stop();
1378
1443
  }
1379
- }, _callee5, this, [[0, 3]]);
1444
+ }, _callee6, this, [[0, 3]]);
1380
1445
  }));
1381
- function dropForeignKey(_x8, _x9) {
1446
+ function dropForeignKey(_x1, _x10) {
1382
1447
  return _dropForeignKey.apply(this, arguments);
1383
1448
  }
1384
1449
  return dropForeignKey;
@@ -1390,39 +1455,39 @@ var DynamicTableService = /*#__PURE__*/function () {
1390
1455
  }, {
1391
1456
  key: "tableExists",
1392
1457
  value: (function () {
1393
- var _tableExists = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee6(tableName) {
1458
+ var _tableExists = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee7(tableName) {
1394
1459
  var _yield$this$supabase$6, data, error, _t6;
1395
- return _regeneratorRuntime.wrap(function (_context6) {
1396
- while (1) switch (_context6.prev = _context6.next) {
1460
+ return _regeneratorRuntime.wrap(function (_context7) {
1461
+ while (1) switch (_context7.prev = _context7.next) {
1397
1462
  case 0:
1398
- _context6.prev = 0;
1399
- _context6.next = 1;
1463
+ _context7.prev = 0;
1464
+ _context7.next = 1;
1400
1465
  return this.supabase.rpc("check_table_exists", {
1401
1466
  table_name: tableName
1402
1467
  });
1403
1468
  case 1:
1404
- _yield$this$supabase$6 = _context6.sent;
1469
+ _yield$this$supabase$6 = _context7.sent;
1405
1470
  data = _yield$this$supabase$6.data;
1406
1471
  error = _yield$this$supabase$6.error;
1407
1472
  if (!error) {
1408
- _context6.next = 2;
1473
+ _context7.next = 2;
1409
1474
  break;
1410
1475
  }
1411
1476
  throw error;
1412
1477
  case 2:
1413
- return _context6.abrupt("return", data || false);
1478
+ return _context7.abrupt("return", data || false);
1414
1479
  case 3:
1415
- _context6.prev = 3;
1416
- _t6 = _context6["catch"](0);
1480
+ _context7.prev = 3;
1481
+ _t6 = _context7["catch"](0);
1417
1482
  console.error("\u68C0\u67E5\u8868 ".concat(tableName, " \u662F\u5426\u5B58\u5728\u5931\u8D25:"), _t6);
1418
- return _context6.abrupt("return", false);
1483
+ return _context7.abrupt("return", false);
1419
1484
  case 4:
1420
1485
  case "end":
1421
- return _context6.stop();
1486
+ return _context7.stop();
1422
1487
  }
1423
- }, _callee6, this, [[0, 3]]);
1488
+ }, _callee7, this, [[0, 3]]);
1424
1489
  }));
1425
- function tableExists(_x0) {
1490
+ function tableExists(_x11) {
1426
1491
  return _tableExists.apply(this, arguments);
1427
1492
  }
1428
1493
  return tableExists;
@@ -1434,39 +1499,39 @@ var DynamicTableService = /*#__PURE__*/function () {
1434
1499
  }, {
1435
1500
  key: "getTableStructure",
1436
1501
  value: (function () {
1437
- var _getTableStructure = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee7(tableName) {
1502
+ var _getTableStructure = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee8(tableName) {
1438
1503
  var _yield$this$supabase$7, data, error, _t7;
1439
- return _regeneratorRuntime.wrap(function (_context7) {
1440
- while (1) switch (_context7.prev = _context7.next) {
1504
+ return _regeneratorRuntime.wrap(function (_context8) {
1505
+ while (1) switch (_context8.prev = _context8.next) {
1441
1506
  case 0:
1442
- _context7.prev = 0;
1443
- _context7.next = 1;
1507
+ _context8.prev = 0;
1508
+ _context8.next = 1;
1444
1509
  return this.supabase.rpc("get_table_structure", {
1445
1510
  table_name: tableName
1446
1511
  });
1447
1512
  case 1:
1448
- _yield$this$supabase$7 = _context7.sent;
1513
+ _yield$this$supabase$7 = _context8.sent;
1449
1514
  data = _yield$this$supabase$7.data;
1450
1515
  error = _yield$this$supabase$7.error;
1451
1516
  if (!error) {
1452
- _context7.next = 2;
1517
+ _context8.next = 2;
1453
1518
  break;
1454
1519
  }
1455
1520
  throw error;
1456
1521
  case 2:
1457
- return _context7.abrupt("return", data);
1522
+ return _context8.abrupt("return", data);
1458
1523
  case 3:
1459
- _context7.prev = 3;
1460
- _t7 = _context7["catch"](0);
1524
+ _context8.prev = 3;
1525
+ _t7 = _context8["catch"](0);
1461
1526
  console.error("\u83B7\u53D6\u8868 ".concat(tableName, " \u7ED3\u6784\u5931\u8D25:"), _t7);
1462
- return _context7.abrupt("return", null);
1527
+ return _context8.abrupt("return", null);
1463
1528
  case 4:
1464
1529
  case "end":
1465
- return _context7.stop();
1530
+ return _context8.stop();
1466
1531
  }
1467
- }, _callee7, this, [[0, 3]]);
1532
+ }, _callee8, this, [[0, 3]]);
1468
1533
  }));
1469
- function getTableStructure(_x1) {
1534
+ function getTableStructure(_x12) {
1470
1535
  return _getTableStructure.apply(this, arguments);
1471
1536
  }
1472
1537
  return getTableStructure;
@@ -1478,12 +1543,12 @@ var DynamicTableService = /*#__PURE__*/function () {
1478
1543
  }, {
1479
1544
  key: "executeQuery",
1480
1545
  value: (function () {
1481
- var _executeQuery = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee8(sql, values) {
1546
+ var _executeQuery = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee9(sql, values) {
1482
1547
  var finalSql, _yield$this$supabase$8, data, error, _t8;
1483
- return _regeneratorRuntime.wrap(function (_context8) {
1484
- while (1) switch (_context8.prev = _context8.next) {
1548
+ return _regeneratorRuntime.wrap(function (_context9) {
1549
+ while (1) switch (_context9.prev = _context9.next) {
1485
1550
  case 0:
1486
- _context8.prev = 0;
1551
+ _context9.prev = 0;
1487
1552
  // 如果有参数,需要先替换占位符
1488
1553
  finalSql = sql;
1489
1554
  if (values && values.length > 0) {
@@ -1492,34 +1557,34 @@ var DynamicTableService = /*#__PURE__*/function () {
1492
1557
  finalSql = finalSql.replace("?", placeholder);
1493
1558
  });
1494
1559
  }
1495
- _context8.next = 1;
1560
+ _context9.next = 1;
1496
1561
  return this.supabase.rpc("execute_sql_with_params", {
1497
1562
  sql_query: finalSql,
1498
1563
  params: values || []
1499
1564
  });
1500
1565
  case 1:
1501
- _yield$this$supabase$8 = _context8.sent;
1566
+ _yield$this$supabase$8 = _context9.sent;
1502
1567
  data = _yield$this$supabase$8.data;
1503
1568
  error = _yield$this$supabase$8.error;
1504
1569
  if (!error) {
1505
- _context8.next = 2;
1570
+ _context9.next = 2;
1506
1571
  break;
1507
1572
  }
1508
1573
  throw error;
1509
1574
  case 2:
1510
- return _context8.abrupt("return", data);
1575
+ return _context9.abrupt("return", data);
1511
1576
  case 3:
1512
- _context8.prev = 3;
1513
- _t8 = _context8["catch"](0);
1577
+ _context9.prev = 3;
1578
+ _t8 = _context9["catch"](0);
1514
1579
  console.error("执行 SQL 查询失败:", _t8);
1515
1580
  throw _t8;
1516
1581
  case 4:
1517
1582
  case "end":
1518
- return _context8.stop();
1583
+ return _context9.stop();
1519
1584
  }
1520
- }, _callee8, this, [[0, 3]]);
1585
+ }, _callee9, this, [[0, 3]]);
1521
1586
  }));
1522
- function executeQuery(_x10, _x11) {
1587
+ function executeQuery(_x13, _x14) {
1523
1588
  return _executeQuery.apply(this, arguments);
1524
1589
  }
1525
1590
  return executeQuery;
@@ -1531,72 +1596,67 @@ var DynamicTableService = /*#__PURE__*/function () {
1531
1596
  }, {
1532
1597
  key: "getTableData",
1533
1598
  value: (function () {
1534
- var _getTableData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee9(tableName) {
1599
+ var _getTableData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee0(tableName) {
1600
+ var _this = this;
1535
1601
  var page,
1536
1602
  limit,
1537
1603
  where,
1538
1604
  filters,
1539
1605
  offset,
1540
- query,
1541
- _yield$query$order$ra,
1606
+ buildQuery,
1607
+ _yield$this$orderByCr,
1542
1608
  data,
1543
- error,
1544
1609
  count,
1545
- _args9 = arguments,
1610
+ _args0 = arguments,
1546
1611
  _t9;
1547
- return _regeneratorRuntime.wrap(function (_context9) {
1548
- while (1) switch (_context9.prev = _context9.next) {
1612
+ return _regeneratorRuntime.wrap(function (_context0) {
1613
+ while (1) switch (_context0.prev = _context0.next) {
1549
1614
  case 0:
1550
- page = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : 1;
1551
- limit = _args9.length > 2 && _args9[2] !== undefined ? _args9[2] : 10;
1552
- where = _args9.length > 3 ? _args9[3] : undefined;
1553
- filters = _args9.length > 4 ? _args9[4] : undefined;
1554
- _context9.prev = 1;
1555
- offset = (page - 1) * limit; // 构建查询
1556
- query = this.supabase.from(tableName).select("*", {
1557
- count: "exact"
1558
- }); // 添加 filters(AND)
1559
- query = this.applySupabaseFilters(query, filters);
1560
- // 添加where条件
1561
- if (where) {
1562
- // 这里简化处理,实际使用中可能需要更复杂的where解析
1563
- query = query.or(where);
1564
- }
1565
- // 添加分页和排序
1566
- _context9.next = 2;
1567
- return query.order("id", {
1568
- ascending: false
1569
- }).range(offset, offset + limit - 1);
1615
+ page = _args0.length > 1 && _args0[1] !== undefined ? _args0[1] : 1;
1616
+ limit = _args0.length > 2 && _args0[2] !== undefined ? _args0[2] : 10;
1617
+ where = _args0.length > 3 ? _args0[3] : undefined;
1618
+ filters = _args0.length > 4 ? _args0[4] : undefined;
1619
+ _context0.prev = 1;
1620
+ offset = (page - 1) * limit;
1621
+ buildQuery = function buildQuery() {
1622
+ // 构建查询
1623
+ var query = _this.supabase.from(tableName).select("*", {
1624
+ count: "exact"
1625
+ });
1626
+ // 添加 filters(AND)
1627
+ query = _this.applySupabaseFilters(query, filters);
1628
+ // 添加where条件
1629
+ if (where) {
1630
+ // 这里简化处理,实际使用中可能需要更复杂的where解析
1631
+ query = query.or(where);
1632
+ }
1633
+ return query;
1634
+ };
1635
+ _context0.next = 2;
1636
+ return this.orderByCreatedAtOrFallback(buildQuery, offset, limit);
1570
1637
  case 2:
1571
- _yield$query$order$ra = _context9.sent;
1572
- data = _yield$query$order$ra.data;
1573
- error = _yield$query$order$ra.error;
1574
- count = _yield$query$order$ra.count;
1575
- if (!error) {
1576
- _context9.next = 3;
1577
- break;
1578
- }
1579
- throw error;
1580
- case 3:
1581
- return _context9.abrupt("return", {
1638
+ _yield$this$orderByCr = _context0.sent;
1639
+ data = _yield$this$orderByCr.data;
1640
+ count = _yield$this$orderByCr.count;
1641
+ return _context0.abrupt("return", {
1582
1642
  data: data || [],
1583
1643
  total: count || 0,
1584
1644
  page: page,
1585
1645
  limit: limit,
1586
1646
  totalPages: Math.ceil((count || 0) / limit)
1587
1647
  });
1588
- case 4:
1589
- _context9.prev = 4;
1590
- _t9 = _context9["catch"](1);
1648
+ case 3:
1649
+ _context0.prev = 3;
1650
+ _t9 = _context0["catch"](1);
1591
1651
  console.error("\u83B7\u53D6\u8868 ".concat(tableName, " \u6570\u636E\u5931\u8D25:"), _t9);
1592
1652
  throw _t9;
1593
- case 5:
1653
+ case 4:
1594
1654
  case "end":
1595
- return _context9.stop();
1655
+ return _context0.stop();
1596
1656
  }
1597
- }, _callee9, this, [[1, 4]]);
1657
+ }, _callee0, this, [[1, 3]]);
1598
1658
  }));
1599
- function getTableData(_x12) {
1659
+ function getTableData(_x15) {
1600
1660
  return _getTableData.apply(this, arguments);
1601
1661
  }
1602
1662
  return getTableData;
@@ -1608,77 +1668,71 @@ var DynamicTableService = /*#__PURE__*/function () {
1608
1668
  }, {
1609
1669
  key: "getTableDataWithSearch",
1610
1670
  value: (function () {
1611
- var _getTableDataWithSearch = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee0(tableName) {
1671
+ var _getTableDataWithSearch = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee1(tableName) {
1672
+ var _this2 = this;
1612
1673
  var page,
1613
1674
  limit,
1614
1675
  searchableFields,
1615
1676
  searchValue,
1616
1677
  filters,
1617
1678
  offset,
1618
- query,
1619
- searchConditions,
1620
- _yield$query$order$ra2,
1679
+ buildQuery,
1680
+ _yield$this$orderByCr2,
1621
1681
  data,
1622
- error,
1623
1682
  count,
1624
- _args0 = arguments,
1683
+ _args1 = arguments,
1625
1684
  _t0;
1626
- return _regeneratorRuntime.wrap(function (_context0) {
1627
- while (1) switch (_context0.prev = _context0.next) {
1685
+ return _regeneratorRuntime.wrap(function (_context1) {
1686
+ while (1) switch (_context1.prev = _context1.next) {
1628
1687
  case 0:
1629
- page = _args0.length > 1 && _args0[1] !== undefined ? _args0[1] : 1;
1630
- limit = _args0.length > 2 && _args0[2] !== undefined ? _args0[2] : 10;
1631
- searchableFields = _args0.length > 3 ? _args0[3] : undefined;
1632
- searchValue = _args0.length > 4 ? _args0[4] : undefined;
1633
- filters = _args0.length > 5 ? _args0[5] : undefined;
1634
- _context0.prev = 1;
1635
- offset = (page - 1) * limit; // 构建查询
1636
- query = this.supabase.from(tableName).select("*", {
1637
- count: "exact"
1638
- }); // 添加 filters(AND)
1639
- query = this.applySupabaseFilters(query, filters);
1640
- // 构建搜索条件:使用Supabase的ilike操作符
1641
- if (searchableFields.length > 0) {
1642
- searchConditions = searchableFields.map(function (field) {
1643
- return "".concat(field, ".ilike.%").concat(searchValue, "%");
1644
- }).join(',');
1645
- query = query.or(searchConditions);
1646
- }
1647
- // 添加分页和排序
1648
- _context0.next = 2;
1649
- return query.order("id", {
1650
- ascending: false
1651
- }).range(offset, offset + limit - 1);
1688
+ page = _args1.length > 1 && _args1[1] !== undefined ? _args1[1] : 1;
1689
+ limit = _args1.length > 2 && _args1[2] !== undefined ? _args1[2] : 10;
1690
+ searchableFields = _args1.length > 3 ? _args1[3] : undefined;
1691
+ searchValue = _args1.length > 4 ? _args1[4] : undefined;
1692
+ filters = _args1.length > 5 ? _args1[5] : undefined;
1693
+ _context1.prev = 1;
1694
+ offset = (page - 1) * limit;
1695
+ buildQuery = function buildQuery() {
1696
+ // 构建查询
1697
+ var query = _this2.supabase.from(tableName).select("*", {
1698
+ count: "exact"
1699
+ });
1700
+ // 添加 filters(AND)
1701
+ query = _this2.applySupabaseFilters(query, filters);
1702
+ // 构建搜索条件:使用Supabase的ilike操作符
1703
+ if (searchableFields.length > 0) {
1704
+ var searchConditions = searchableFields.map(function (field) {
1705
+ return "".concat(field, ".ilike.%").concat(searchValue, "%");
1706
+ }).join(",");
1707
+ query = query.or(searchConditions);
1708
+ }
1709
+ return query;
1710
+ };
1711
+ _context1.next = 2;
1712
+ return this.orderByCreatedAtOrFallback(buildQuery, offset, limit);
1652
1713
  case 2:
1653
- _yield$query$order$ra2 = _context0.sent;
1654
- data = _yield$query$order$ra2.data;
1655
- error = _yield$query$order$ra2.error;
1656
- count = _yield$query$order$ra2.count;
1657
- if (!error) {
1658
- _context0.next = 3;
1659
- break;
1660
- }
1661
- throw error;
1662
- case 3:
1663
- return _context0.abrupt("return", {
1714
+ _yield$this$orderByCr2 = _context1.sent;
1715
+ data = _yield$this$orderByCr2.data;
1716
+ count = _yield$this$orderByCr2.count;
1717
+ return _context1.abrupt("return", {
1664
1718
  data: data || [],
1665
1719
  total: count || 0,
1666
1720
  page: page,
1667
1721
  limit: limit,
1668
1722
  totalPages: Math.ceil((count || 0) / limit)
1669
1723
  });
1670
- case 4:
1671
- _context0.prev = 4;
1672
- _t0 = _context0["catch"](1);
1724
+ case 3:
1725
+ _context1.prev = 3;
1726
+ _t0 = _context1["catch"](1);
1673
1727
  console.error("\u83B7\u53D6\u8868 ".concat(tableName, " \u641C\u7D22\u6570\u636E\u5931\u8D25:"), _t0);
1674
1728
  throw _t0;
1675
- case 5:
1729
+ case 4:
1676
1730
  case "end":
1677
- return _context0.stop();
1731
+ return _context1.stop();
1678
1732
  }
1679
- }, _callee0, this, [[1, 4]]);
1733
+ }, _callee1, this, [[1, 3]]);
1680
1734
  }));
1681
- function getTableDataWithSearch(_x13) {
1735
+ function getTableDataWithSearch(_x16) {
1682
1736
  return _getTableDataWithSearch.apply(this, arguments);
1683
1737
  }
1684
1738
  return getTableDataWithSearch;
@@ -1690,7 +1744,7 @@ var DynamicTableService = /*#__PURE__*/function () {
1690
1744
  }, {
1691
1745
  key: "insertData",
1692
1746
  value: (function () {
1693
- var _insertData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee1(tableName, data) {
1747
+ var _insertData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee10(tableName, data) {
1694
1748
  var autoFixForeignKey,
1695
1749
  _yield$this$supabase$9,
1696
1750
  result,
@@ -1700,25 +1754,25 @@ var DynamicTableService = /*#__PURE__*/function () {
1700
1754
  constraintName,
1701
1755
  dropResult,
1702
1756
  _dropResult,
1703
- _args1 = arguments,
1757
+ _args10 = arguments,
1704
1758
  _t1;
1705
- return _regeneratorRuntime.wrap(function (_context1) {
1706
- while (1) switch (_context1.prev = _context1.next) {
1759
+ return _regeneratorRuntime.wrap(function (_context10) {
1760
+ while (1) switch (_context10.prev = _context10.next) {
1707
1761
  case 0:
1708
- autoFixForeignKey = _args1.length > 2 && _args1[2] !== undefined ? _args1[2] : true;
1709
- _context1.prev = 1;
1710
- _context1.next = 2;
1762
+ autoFixForeignKey = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : true;
1763
+ _context10.prev = 1;
1764
+ _context10.next = 2;
1711
1765
  return this.supabase.from(tableName).insert(data).select().single();
1712
1766
  case 2:
1713
- _yield$this$supabase$9 = _context1.sent;
1767
+ _yield$this$supabase$9 = _context10.sent;
1714
1768
  result = _yield$this$supabase$9.data;
1715
1769
  error = _yield$this$supabase$9.error;
1716
1770
  if (!error) {
1717
- _context1.next = 10;
1771
+ _context10.next = 10;
1718
1772
  break;
1719
1773
  }
1720
1774
  if (!(autoFixForeignKey && error.code === '23503' && (_error$message = error.message) !== null && _error$message !== void 0 && _error$message.includes('foreign key constraint'))) {
1721
- _context1.next = 9;
1775
+ _context10.next = 9;
1722
1776
  break;
1723
1777
  }
1724
1778
  console.log("\u68C0\u6D4B\u5230\u5916\u952E\u7EA6\u675F\u9519\u8BEF\uFF0C\u5C1D\u8BD5\u81EA\u52A8\u5220\u9664\u5916\u952E\u7EA6\u675F\u5E76\u91CD\u8BD5...");
@@ -1727,59 +1781,59 @@ var DynamicTableService = /*#__PURE__*/function () {
1727
1781
  // 格式: violates foreign key constraint "constraint_name"
1728
1782
  constraintMatch = error.message.match(/foreign key constraint "([^"]+)"/);
1729
1783
  if (!(constraintMatch && constraintMatch[1])) {
1730
- _context1.next = 6;
1784
+ _context10.next = 6;
1731
1785
  break;
1732
1786
  }
1733
1787
  constraintName = constraintMatch[1];
1734
1788
  console.log("\u63D0\u53D6\u5230\u7EA6\u675F\u540D: ".concat(constraintName));
1735
1789
  // 直接删除该约束
1736
- _context1.next = 3;
1790
+ _context10.next = 3;
1737
1791
  return this.dropForeignKeyByName(tableName, constraintName);
1738
1792
  case 3:
1739
- dropResult = _context1.sent;
1793
+ dropResult = _context10.sent;
1740
1794
  if (!dropResult) {
1741
- _context1.next = 5;
1795
+ _context10.next = 5;
1742
1796
  break;
1743
1797
  }
1744
1798
  console.log("\u5916\u952E\u7EA6\u675F ".concat(constraintName, " \u5DF2\u5220\u9664\uFF0C\u91CD\u8BD5\u63D2\u5165\u6570\u636E..."));
1745
1799
  // 重试插入,但这次不再自动修复(避免无限循环)
1746
- _context1.next = 4;
1800
+ _context10.next = 4;
1747
1801
  return this.insertData(tableName, data, false);
1748
1802
  case 4:
1749
- return _context1.abrupt("return", _context1.sent);
1803
+ return _context10.abrupt("return", _context10.sent);
1750
1804
  case 5:
1751
- _context1.next = 9;
1805
+ _context10.next = 9;
1752
1806
  break;
1753
1807
  case 6:
1754
- _context1.next = 7;
1808
+ _context10.next = 7;
1755
1809
  return this.dropAllForeignKeys(tableName);
1756
1810
  case 7:
1757
- _dropResult = _context1.sent;
1811
+ _dropResult = _context10.sent;
1758
1812
  if (!_dropResult) {
1759
- _context1.next = 9;
1813
+ _context10.next = 9;
1760
1814
  break;
1761
1815
  }
1762
1816
  console.log("\u5916\u952E\u7EA6\u675F\u5DF2\u5220\u9664\uFF0C\u91CD\u8BD5\u63D2\u5165\u6570\u636E...");
1763
- _context1.next = 8;
1817
+ _context10.next = 8;
1764
1818
  return this.insertData(tableName, data, false);
1765
1819
  case 8:
1766
- return _context1.abrupt("return", _context1.sent);
1820
+ return _context10.abrupt("return", _context10.sent);
1767
1821
  case 9:
1768
1822
  throw error;
1769
1823
  case 10:
1770
- return _context1.abrupt("return", result);
1824
+ return _context10.abrupt("return", result);
1771
1825
  case 11:
1772
- _context1.prev = 11;
1773
- _t1 = _context1["catch"](1);
1826
+ _context10.prev = 11;
1827
+ _t1 = _context10["catch"](1);
1774
1828
  console.error("\u63D2\u5165\u6570\u636E\u5230\u8868 ".concat(tableName, " \u5931\u8D25:"), _t1);
1775
1829
  throw _t1;
1776
1830
  case 12:
1777
1831
  case "end":
1778
- return _context1.stop();
1832
+ return _context10.stop();
1779
1833
  }
1780
- }, _callee1, this, [[1, 11]]);
1834
+ }, _callee10, this, [[1, 11]]);
1781
1835
  }));
1782
- function insertData(_x14, _x15) {
1836
+ function insertData(_x17, _x18) {
1783
1837
  return _insertData.apply(this, arguments);
1784
1838
  }
1785
1839
  return insertData;
@@ -1792,7 +1846,7 @@ var DynamicTableService = /*#__PURE__*/function () {
1792
1846
  }, {
1793
1847
  key: "updateData",
1794
1848
  value: (function () {
1795
- var _updateData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee10(tableName, id, data) {
1849
+ var _updateData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee11(tableName, id, data) {
1796
1850
  var autoFixForeignKey,
1797
1851
  _yield$this$supabase$0,
1798
1852
  result,
@@ -1802,25 +1856,25 @@ var DynamicTableService = /*#__PURE__*/function () {
1802
1856
  constraintName,
1803
1857
  dropResult,
1804
1858
  _dropResult2,
1805
- _args10 = arguments,
1859
+ _args11 = arguments,
1806
1860
  _t10;
1807
- return _regeneratorRuntime.wrap(function (_context10) {
1808
- while (1) switch (_context10.prev = _context10.next) {
1861
+ return _regeneratorRuntime.wrap(function (_context11) {
1862
+ while (1) switch (_context11.prev = _context11.next) {
1809
1863
  case 0:
1810
- autoFixForeignKey = _args10.length > 3 && _args10[3] !== undefined ? _args10[3] : true;
1811
- _context10.prev = 1;
1812
- _context10.next = 2;
1864
+ autoFixForeignKey = _args11.length > 3 && _args11[3] !== undefined ? _args11[3] : true;
1865
+ _context11.prev = 1;
1866
+ _context11.next = 2;
1813
1867
  return this.supabase.from(tableName).update(data).eq("id", id).select().single();
1814
1868
  case 2:
1815
- _yield$this$supabase$0 = _context10.sent;
1869
+ _yield$this$supabase$0 = _context11.sent;
1816
1870
  result = _yield$this$supabase$0.data;
1817
1871
  error = _yield$this$supabase$0.error;
1818
1872
  if (!error) {
1819
- _context10.next = 10;
1873
+ _context11.next = 10;
1820
1874
  break;
1821
1875
  }
1822
1876
  if (!(autoFixForeignKey && error.code === '23503' && (_error$message2 = error.message) !== null && _error$message2 !== void 0 && _error$message2.includes('foreign key constraint'))) {
1823
- _context10.next = 9;
1877
+ _context11.next = 9;
1824
1878
  break;
1825
1879
  }
1826
1880
  console.log("\u68C0\u6D4B\u5230\u5916\u952E\u7EA6\u675F\u9519\u8BEF\uFF0C\u5C1D\u8BD5\u81EA\u52A8\u5220\u9664\u5916\u952E\u7EA6\u675F\u5E76\u91CD\u8BD5...");
@@ -1828,58 +1882,58 @@ var DynamicTableService = /*#__PURE__*/function () {
1828
1882
  // 尝试从错误消息中提取约束名
1829
1883
  constraintMatch = error.message.match(/foreign key constraint "([^"]+)"/);
1830
1884
  if (!(constraintMatch && constraintMatch[1])) {
1831
- _context10.next = 6;
1885
+ _context11.next = 6;
1832
1886
  break;
1833
1887
  }
1834
1888
  constraintName = constraintMatch[1];
1835
1889
  console.log("\u63D0\u53D6\u5230\u7EA6\u675F\u540D: ".concat(constraintName));
1836
1890
  // 直接删除该约束
1837
- _context10.next = 3;
1891
+ _context11.next = 3;
1838
1892
  return this.dropForeignKeyByName(tableName, constraintName);
1839
1893
  case 3:
1840
- dropResult = _context10.sent;
1894
+ dropResult = _context11.sent;
1841
1895
  if (!dropResult) {
1842
- _context10.next = 5;
1896
+ _context11.next = 5;
1843
1897
  break;
1844
1898
  }
1845
1899
  console.log("\u5916\u952E\u7EA6\u675F ".concat(constraintName, " \u5DF2\u5220\u9664\uFF0C\u91CD\u8BD5\u66F4\u65B0\u6570\u636E..."));
1846
- _context10.next = 4;
1900
+ _context11.next = 4;
1847
1901
  return this.updateData(tableName, id, data, false);
1848
1902
  case 4:
1849
- return _context10.abrupt("return", _context10.sent);
1903
+ return _context11.abrupt("return", _context11.sent);
1850
1904
  case 5:
1851
- _context10.next = 9;
1905
+ _context11.next = 9;
1852
1906
  break;
1853
1907
  case 6:
1854
- _context10.next = 7;
1908
+ _context11.next = 7;
1855
1909
  return this.dropAllForeignKeys(tableName);
1856
1910
  case 7:
1857
- _dropResult2 = _context10.sent;
1911
+ _dropResult2 = _context11.sent;
1858
1912
  if (!_dropResult2) {
1859
- _context10.next = 9;
1913
+ _context11.next = 9;
1860
1914
  break;
1861
1915
  }
1862
1916
  console.log("\u5916\u952E\u7EA6\u675F\u5DF2\u5220\u9664\uFF0C\u91CD\u8BD5\u66F4\u65B0\u6570\u636E...");
1863
- _context10.next = 8;
1917
+ _context11.next = 8;
1864
1918
  return this.updateData(tableName, id, data, false);
1865
1919
  case 8:
1866
- return _context10.abrupt("return", _context10.sent);
1920
+ return _context11.abrupt("return", _context11.sent);
1867
1921
  case 9:
1868
1922
  throw error;
1869
1923
  case 10:
1870
- return _context10.abrupt("return", result);
1924
+ return _context11.abrupt("return", result);
1871
1925
  case 11:
1872
- _context10.prev = 11;
1873
- _t10 = _context10["catch"](1);
1926
+ _context11.prev = 11;
1927
+ _t10 = _context11["catch"](1);
1874
1928
  console.error("\u66F4\u65B0\u8868 ".concat(tableName, " \u6570\u636E\u5931\u8D25:"), _t10);
1875
1929
  throw _t10;
1876
1930
  case 12:
1877
1931
  case "end":
1878
- return _context10.stop();
1932
+ return _context11.stop();
1879
1933
  }
1880
- }, _callee10, this, [[1, 11]]);
1934
+ }, _callee11, this, [[1, 11]]);
1881
1935
  }));
1882
- function updateData(_x16, _x17, _x18) {
1936
+ function updateData(_x19, _x20, _x21) {
1883
1937
  return _updateData.apply(this, arguments);
1884
1938
  }
1885
1939
  return updateData;
@@ -1892,37 +1946,37 @@ var DynamicTableService = /*#__PURE__*/function () {
1892
1946
  }, {
1893
1947
  key: "deleteData",
1894
1948
  value: (function () {
1895
- var _deleteData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee11(tableName, id) {
1949
+ var _deleteData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee12(tableName, id) {
1896
1950
  var _yield$this$supabase$1, result, error, _t11;
1897
- return _regeneratorRuntime.wrap(function (_context11) {
1898
- while (1) switch (_context11.prev = _context11.next) {
1951
+ return _regeneratorRuntime.wrap(function (_context12) {
1952
+ while (1) switch (_context12.prev = _context12.next) {
1899
1953
  case 0:
1900
- _context11.prev = 0;
1901
- _context11.next = 1;
1954
+ _context12.prev = 0;
1955
+ _context12.next = 1;
1902
1956
  return this.supabase.from(tableName)["delete"]().eq("id", id).select().single();
1903
1957
  case 1:
1904
- _yield$this$supabase$1 = _context11.sent;
1958
+ _yield$this$supabase$1 = _context12.sent;
1905
1959
  result = _yield$this$supabase$1.data;
1906
1960
  error = _yield$this$supabase$1.error;
1907
1961
  if (!error) {
1908
- _context11.next = 2;
1962
+ _context12.next = 2;
1909
1963
  break;
1910
1964
  }
1911
1965
  throw error;
1912
1966
  case 2:
1913
- return _context11.abrupt("return", result);
1967
+ return _context12.abrupt("return", result);
1914
1968
  case 3:
1915
- _context11.prev = 3;
1916
- _t11 = _context11["catch"](0);
1969
+ _context12.prev = 3;
1970
+ _t11 = _context12["catch"](0);
1917
1971
  console.error("\u5220\u9664\u8868 ".concat(tableName, " \u6570\u636E\u5931\u8D25:"), _t11);
1918
1972
  throw _t11;
1919
1973
  case 4:
1920
1974
  case "end":
1921
- return _context11.stop();
1975
+ return _context12.stop();
1922
1976
  }
1923
- }, _callee11, this, [[0, 3]]);
1977
+ }, _callee12, this, [[0, 3]]);
1924
1978
  }));
1925
- function deleteData(_x19, _x20) {
1979
+ function deleteData(_x22, _x23) {
1926
1980
  return _deleteData.apply(this, arguments);
1927
1981
  }
1928
1982
  return deleteData;
@@ -1935,43 +1989,43 @@ var DynamicTableService = /*#__PURE__*/function () {
1935
1989
  }, {
1936
1990
  key: "getDataById",
1937
1991
  value: (function () {
1938
- var _getDataById = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee12(tableName, id) {
1992
+ var _getDataById = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee13(tableName, id) {
1939
1993
  var _yield$this$supabase$10, data, error, _t12;
1940
- return _regeneratorRuntime.wrap(function (_context12) {
1941
- while (1) switch (_context12.prev = _context12.next) {
1994
+ return _regeneratorRuntime.wrap(function (_context13) {
1995
+ while (1) switch (_context13.prev = _context13.next) {
1942
1996
  case 0:
1943
- _context12.prev = 0;
1944
- _context12.next = 1;
1997
+ _context13.prev = 0;
1998
+ _context13.next = 1;
1945
1999
  return this.supabase.from(tableName).select("*").eq("id", id).single();
1946
2000
  case 1:
1947
- _yield$this$supabase$10 = _context12.sent;
2001
+ _yield$this$supabase$10 = _context13.sent;
1948
2002
  data = _yield$this$supabase$10.data;
1949
2003
  error = _yield$this$supabase$10.error;
1950
2004
  if (!error) {
1951
- _context12.next = 3;
2005
+ _context13.next = 3;
1952
2006
  break;
1953
2007
  }
1954
2008
  if (!(error.code === "PGRST116")) {
1955
- _context12.next = 2;
2009
+ _context13.next = 2;
1956
2010
  break;
1957
2011
  }
1958
- return _context12.abrupt("return", null);
2012
+ return _context13.abrupt("return", null);
1959
2013
  case 2:
1960
2014
  throw error;
1961
2015
  case 3:
1962
- return _context12.abrupt("return", data);
2016
+ return _context13.abrupt("return", data);
1963
2017
  case 4:
1964
- _context12.prev = 4;
1965
- _t12 = _context12["catch"](0);
2018
+ _context13.prev = 4;
2019
+ _t12 = _context13["catch"](0);
1966
2020
  console.error("\u83B7\u53D6\u8868 ".concat(tableName, " \u5355\u6761\u6570\u636E\u5931\u8D25:"), _t12);
1967
2021
  throw _t12;
1968
2022
  case 5:
1969
2023
  case "end":
1970
- return _context12.stop();
2024
+ return _context13.stop();
1971
2025
  }
1972
- }, _callee12, this, [[0, 4]]);
2026
+ }, _callee13, this, [[0, 4]]);
1973
2027
  }));
1974
- function getDataById(_x21, _x22) {
2028
+ function getDataById(_x24, _x25) {
1975
2029
  return _getDataById.apply(this, arguments);
1976
2030
  }
1977
2031
  return getDataById;
@@ -1985,47 +2039,47 @@ var DynamicTableService = /*#__PURE__*/function () {
1985
2039
  }, {
1986
2040
  key: "getRelationOptions",
1987
2041
  value: (function () {
1988
- var _getRelationOptions = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee13(params) {
2042
+ var _getRelationOptions = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee14(params) {
1989
2043
  var tableName, _params$displayField, displayField, _params$limit, limit, search, query, _yield$query, data, error, _t13;
1990
- return _regeneratorRuntime.wrap(function (_context13) {
1991
- while (1) switch (_context13.prev = _context13.next) {
2044
+ return _regeneratorRuntime.wrap(function (_context14) {
2045
+ while (1) switch (_context14.prev = _context14.next) {
1992
2046
  case 0:
1993
- _context13.prev = 0;
2047
+ _context14.prev = 0;
1994
2048
  tableName = params.tableName, _params$displayField = params.displayField, displayField = _params$displayField === void 0 ? 'name' : _params$displayField, _params$limit = params.limit, limit = _params$limit === void 0 ? 100 : _params$limit, search = params.search; // 构建查询,获取所有字段(Supabase 动态字段选择类型推断有问题)
1995
2049
  query = this.supabase.from(tableName).select('*').limit(limit); // 如果有搜索条件,添加模糊搜索
1996
2050
  if (search) {
1997
2051
  query = query.ilike(displayField, "%".concat(search, "%"));
1998
2052
  }
1999
- _context13.next = 1;
2053
+ _context14.next = 1;
2000
2054
  return query;
2001
2055
  case 1:
2002
- _yield$query = _context13.sent;
2056
+ _yield$query = _context14.sent;
2003
2057
  data = _yield$query.data;
2004
2058
  error = _yield$query.error;
2005
2059
  if (!error) {
2006
- _context13.next = 2;
2060
+ _context14.next = 2;
2007
2061
  break;
2008
2062
  }
2009
2063
  throw error;
2010
2064
  case 2:
2011
- return _context13.abrupt("return", (data || []).map(function (item) {
2065
+ return _context14.abrupt("return", (data || []).map(function (item) {
2012
2066
  return _objectSpread$2({
2013
2067
  id: item.id,
2014
2068
  label: item[displayField] || "ID: ".concat(item.id)
2015
2069
  }, item);
2016
2070
  }));
2017
2071
  case 3:
2018
- _context13.prev = 3;
2019
- _t13 = _context13["catch"](0);
2072
+ _context14.prev = 3;
2073
+ _t13 = _context14["catch"](0);
2020
2074
  console.error("\u83B7\u53D6\u5173\u8054\u8868 ".concat(params.tableName, " \u9009\u9879\u5931\u8D25:"), _t13);
2021
2075
  throw _t13;
2022
2076
  case 4:
2023
2077
  case "end":
2024
- return _context13.stop();
2078
+ return _context14.stop();
2025
2079
  }
2026
- }, _callee13, this, [[0, 3]]);
2080
+ }, _callee14, this, [[0, 3]]);
2027
2081
  }));
2028
- function getRelationOptions(_x23) {
2082
+ function getRelationOptions(_x26) {
2029
2083
  return _getRelationOptions.apply(this, arguments);
2030
2084
  }
2031
2085
  return getRelationOptions;
@@ -2042,25 +2096,26 @@ var DynamicTableService = /*#__PURE__*/function () {
2042
2096
  }, {
2043
2097
  key: "getTableDataWithRelations",
2044
2098
  value: (function () {
2045
- var _getTableDataWithRelations = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee14(tableName, schema) {
2099
+ var _getTableDataWithRelations = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee15(tableName, schema) {
2100
+ var _this3 = this;
2046
2101
  var page,
2047
2102
  limit,
2048
2103
  offset,
2049
2104
  selectFields,
2050
2105
  relationFields,
2051
2106
  relationSelects,
2052
- _yield$this$supabase$11,
2107
+ buildQuery,
2108
+ _yield$this$orderByCr3,
2053
2109
  data,
2054
- error,
2055
2110
  count,
2056
- _args14 = arguments,
2111
+ _args15 = arguments,
2057
2112
  _t14;
2058
- return _regeneratorRuntime.wrap(function (_context14) {
2059
- while (1) switch (_context14.prev = _context14.next) {
2113
+ return _regeneratorRuntime.wrap(function (_context15) {
2114
+ while (1) switch (_context15.prev = _context15.next) {
2060
2115
  case 0:
2061
- page = _args14.length > 2 && _args14[2] !== undefined ? _args14[2] : 1;
2062
- limit = _args14.length > 3 && _args14[3] !== undefined ? _args14[3] : 10;
2063
- _context14.prev = 1;
2116
+ page = _args15.length > 2 && _args15[2] !== undefined ? _args15[2] : 1;
2117
+ limit = _args15.length > 3 && _args15[3] !== undefined ? _args15[3] : 10;
2118
+ _context15.prev = 1;
2064
2119
  offset = (page - 1) * limit; // 构建 select 语句,包含关联表数据
2065
2120
  selectFields = '*';
2066
2121
  relationFields = schema.filter(function (f) {
@@ -2076,42 +2131,36 @@ var DynamicTableService = /*#__PURE__*/function () {
2076
2131
  });
2077
2132
  selectFields = "*, ".concat(relationSelects.join(', '));
2078
2133
  }
2079
- _context14.next = 2;
2080
- return this.supabase.from(tableName).select(selectFields, {
2081
- count: "exact"
2082
- }).order("id", {
2083
- ascending: false
2084
- }).range(offset, offset + limit - 1);
2134
+ buildQuery = function buildQuery() {
2135
+ return _this3.supabase.from(tableName).select(selectFields, {
2136
+ count: "exact"
2137
+ });
2138
+ };
2139
+ _context15.next = 2;
2140
+ return this.orderByCreatedAtOrFallback(buildQuery, offset, limit);
2085
2141
  case 2:
2086
- _yield$this$supabase$11 = _context14.sent;
2087
- data = _yield$this$supabase$11.data;
2088
- error = _yield$this$supabase$11.error;
2089
- count = _yield$this$supabase$11.count;
2090
- if (!error) {
2091
- _context14.next = 3;
2092
- break;
2093
- }
2094
- throw error;
2095
- case 3:
2096
- return _context14.abrupt("return", {
2142
+ _yield$this$orderByCr3 = _context15.sent;
2143
+ data = _yield$this$orderByCr3.data;
2144
+ count = _yield$this$orderByCr3.count;
2145
+ return _context15.abrupt("return", {
2097
2146
  data: data || [],
2098
2147
  total: count || 0,
2099
2148
  page: page,
2100
2149
  limit: limit,
2101
2150
  totalPages: Math.ceil((count || 0) / limit)
2102
2151
  });
2103
- case 4:
2104
- _context14.prev = 4;
2105
- _t14 = _context14["catch"](1);
2152
+ case 3:
2153
+ _context15.prev = 3;
2154
+ _t14 = _context15["catch"](1);
2106
2155
  console.error("\u83B7\u53D6\u8868 ".concat(tableName, " \u5173\u8054\u6570\u636E\u5931\u8D25:"), _t14);
2107
2156
  throw _t14;
2108
- case 5:
2157
+ case 4:
2109
2158
  case "end":
2110
- return _context14.stop();
2159
+ return _context15.stop();
2111
2160
  }
2112
- }, _callee14, this, [[1, 4]]);
2161
+ }, _callee15, this, [[1, 3]]);
2113
2162
  }));
2114
- function getTableDataWithRelations(_x24, _x25) {
2163
+ function getTableDataWithRelations(_x27, _x28) {
2115
2164
  return _getTableDataWithRelations.apply(this, arguments);
2116
2165
  }
2117
2166
  return getTableDataWithRelations;
@@ -3535,7 +3584,7 @@ function updateTableData(_x5, _x6) {
3535
3584
  // DELETE - 删除数据
3536
3585
  function _updateTableData() {
3537
3586
  _updateTableData = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(c, tableName) {
3538
- var _model$json_schema3, dynamicTableService, cmsModelService, body, id, data, recordId, _response6, _response7, existingData, _response8, rawUpdateData, updateData, _i3, _Object$entries3, _Object$entries3$_i, key, value, model, normalizedUpdateData, _response9, result, _response0, response, _response1, _t5, _t6;
3587
+ var _model$json_schema3, dynamicTableService, cmsModelService, body, id, data, recordId, _response6, _response7, existingData, _response8, rawUpdateData, updateData, _i3, _Object$entries3, _Object$entries3$_i, key, value, model, _response9, result, _response0, response, _response1, _t5, _t6;
3539
3588
  return _regeneratorRuntime.wrap(function (_context3) {
3540
3589
  while (1) switch (_context3.prev = _context3.next) {
3541
3590
  case 0:
@@ -3598,13 +3647,12 @@ function _updateTableData() {
3598
3647
  return cmsModelService.findByTableName(tableName);
3599
3648
  case 6:
3600
3649
  model = _context3.sent;
3601
- normalizedUpdateData = updateData;
3602
3650
  if (!(model !== null && model !== void 0 && (_model$json_schema3 = model.json_schema) !== null && _model$json_schema3 !== void 0 && _model$json_schema3.fields)) {
3603
3651
  _context3.next = 9;
3604
3652
  break;
3605
3653
  }
3606
3654
  _context3.prev = 7;
3607
- normalizedUpdateData = normalizeJsonLikeFields(model.json_schema.fields, updateData);
3655
+ normalizeJsonLikeFields(model.json_schema.fields, updateData);
3608
3656
  _context3.next = 9;
3609
3657
  break;
3610
3658
  case 8:
@@ -3617,7 +3665,7 @@ function _updateTableData() {
3617
3665
  return _context3.abrupt("return", c.json(_response9, 200));
3618
3666
  case 9:
3619
3667
  _context3.next = 10;
3620
- return dynamicTableService.updateData(tableName, recordId, normalizedUpdateData);
3668
+ return dynamicTableService.updateData(tableName, id, updateData);
3621
3669
  case 10:
3622
3670
  result = _context3.sent;
3623
3671
  if (result) {