74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import { db } from "./src/db";
|
|
import { mapTable } from "./src/db/schema";
|
|
import { eq } from "drizzle-orm";
|
|
|
|
async function reorderMapPoints() {
|
|
try {
|
|
console.log("🚀 Начинаю обработку точек на карте...\n");
|
|
|
|
// Получаем все записи из таблицы map
|
|
const allPoints = await db.query.mapTable.findMany({
|
|
orderBy: (mapTable, { asc }) => [asc(mapTable.city), asc(mapTable.id)],
|
|
});
|
|
|
|
if (allPoints.length === 0) {
|
|
console.log("⚠️ Записей в таблице map не найдено.");
|
|
return;
|
|
}
|
|
|
|
console.log(`📊 Найдено записей: ${allPoints.length}`);
|
|
|
|
// Группируем точки по городам
|
|
const pointsByCity = new Map<string, Array<(typeof allPoints)[0]>>();
|
|
|
|
for (const point of allPoints) {
|
|
if (!pointsByCity.has(point.city)) {
|
|
pointsByCity.set(point.city, []);
|
|
}
|
|
pointsByCity.get(point.city)!.push(point);
|
|
}
|
|
|
|
console.log(`🏙️ Найдено городов: ${pointsByCity.size}\n`);
|
|
|
|
// Обновляем order для каждого города
|
|
let totalUpdated = 0;
|
|
|
|
for (const [city, points] of pointsByCity) {
|
|
console.log(`📍 Обрабатываю город: ${city} (${points.length} точек)`);
|
|
|
|
// Обновляем каждую точку с новым значением order
|
|
const updatePromises = points.map((point, index) =>
|
|
db
|
|
.update(mapTable)
|
|
.set({ order: index })
|
|
.where(eq(mapTable.id, point.id))
|
|
);
|
|
|
|
await Promise.all(updatePromises);
|
|
|
|
totalUpdated += points.length;
|
|
console.log(
|
|
` ✅ Обновлено: ${points.length} точек (order: 0-${
|
|
points.length - 1
|
|
})`
|
|
);
|
|
}
|
|
|
|
console.log("\n" + "=".repeat(50));
|
|
console.log(`✨ Успешно завершено!`);
|
|
console.log(`📊 Статистика:`);
|
|
console.log(` - Всего городов: ${pointsByCity.size}`);
|
|
console.log(` - Всего обновлено точек: ${totalUpdated}`);
|
|
console.log("=".repeat(50));
|
|
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error("\n❌ Ошибка при выполнении скрипта:");
|
|
console.error(error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Запускаем скрипт
|
|
reorderMapPoints();
|