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