#!/usr/bin/env node /** * sync_data_to_postgres.mjs — อัปเดตข้อมูลจาก mock (เครื่องนี้) เข้า Postgres บน VPS * * ที่มา: D:\Git\workspace\web-at-transit-mock\resource\data * - provinces.json, route_groups.json, stations.json, routes.json * - scheduleslist/schedules.json * - affiliates.json → sponsors * * หลักการ: upsert อย่างเดียว + แทน schedules ต่อสาย (delete→insert) ใน transaction เดียว * มี --dry-run (default) และ --backup ก่อนเขียนจริง * * ต้องมีไดรเวอร์ pg: * npm i pg (ในโฟลเดอร์ที่มี node_modules) * NODE_PATH=<ที่เก็บ node_modules> node script/sync_data_to_postgres.mjs --apply * * env: PGHOST PGPORT PGUSER PGPASSWORD PGDATABASE */ import fs from "node:fs"; import path from "node:path"; import { createRequire } from "node:module"; const require = createRequire(process.cwd() + "/package.json"); const { Client } = require("pg"); const DATA = process.env.DATA_DIR || "D:/Git/workspace/web-at-transit-mock/resource/data"; const args = process.argv.slice(2); const APPLY = args.includes("--apply"); const BACKUP_DIR = (() => { const i = args.indexOf("--backup"); return i > -1 ? args[i + 1] : "C:/Users/BANK/AppData/Local/hermes/cache/scratch/db-backup"; })(); const readJSON = (name) => JSON.parse(fs.readFileSync(path.join(DATA, name), "utf8")); function hhmm(t) { const s = String(t || "").trim(); if (!s) return null; return /^\d{2}:\d{2}$/.test(s) ? `${s}:00` : s; } function scheduleRows(file) { const d = JSON.parse(fs.readFileSync(file, "utf8")); const routeId = String(d.route_id || ""); if (!routeId) return []; const rows = []; // schema 3: { weekday: {label, directions: {ชื่อทิศ: {from_station_id, departures[]}}}, holiday: {...} } for (const [dayKey, group] of Object.entries(d)) { if (!group || typeof group !== "object" || Array.isArray(group) || !group.directions) continue; if (Array.isArray(group.directions)) continue; for (const [dirName, dir] of Object.entries(group.directions)) { const stationId = Number(dir.from_station_id); const times = (dir.departures || []).map(hhmm).filter(Boolean); if (!stationId || !times.length) continue; rows.push({ route_id: routeId, station_id: stationId, direction_note: dirName, departure_times: times, variants: { day: dayKey, label: group.label || "" }, schedule_note: null, }); } } // schema 2: { directions: [{direction_id, direction_label, stops:[{station_id, times[]}]}] } if (Array.isArray(d.directions)) { for (const dir of d.directions) { const label = dir.direction_label || dir.direction_id || ""; for (const stop of dir.stops || []) { const times = (stop.times || []).map(hhmm).filter(Boolean); if (!stop.station_id || !times.length) continue; rows.push({ route_id: routeId, station_id: Number(stop.station_id), direction_note: label, departure_times: times, variants: { day: d.day || "weekday", label: dir.direction_label || "" }, schedule_note: null, }); } } } return rows; } /** enum station_kind ไม่มี local_terminal — DB เดิมเก็บเป็น main_terminal */ const STATION_TYPE = { main_terminal: "main_terminal", van_terminal: "van_terminal", local_terminal: "main_terminal", local_stop: "local_stop", market: "market", landmark: "landmark", private_stop: "private_stop", other: "other", }; const stationType = (v) => STATION_TYPE[v] || "other"; async function main() { const provinces = readJSON("provinces.json").provinces || []; const groups = readJSON("route_groups.json").route_groups || []; const stations = readJSON("stations.json").stations || []; const routes = readJSON("routes.json").routes || []; const affiliates = readJSON("affiliates.json").partners || []; const schedDir = path.join(DATA, "scheduleslist"); const schedFiles = fs.existsSync(schedDir) ? fs.readdirSync(schedDir).filter((f) => /^schedules.+\.json$/.test(f)) : []; const schedules = schedFiles.flatMap((f) => scheduleRows(path.join(schedDir, f))); console.log( `source: provinces=${provinces.length} groups=${groups.length} stations=${stations.length} ` + `routes=${routes.length} schedules=${schedules.length} (files=${schedFiles.length}) partners=${affiliates.length}`, ); const client = new Client({ host: process.env.PGHOST, port: Number(process.env.PGPORT || 5432), user: process.env.PGUSER, password: process.env.PGPASSWORD, database: process.env.PGDATABASE, connectionTimeoutMillis: 8000, }); await client.connect(); // ---- backup ของเดิมก่อนเขียน ---- if (APPLY) { fs.mkdirSync(BACKUP_DIR, { recursive: true }); const stamp = new Date().toISOString().replace(/[:.]/g, "-"); for (const t of ["routes", "stations", "schedules", "route_stops", "route_provinces", "sponsors", "provinces", "route_groups"]) { const { rows } = await client.query(`select * from public.${t}`); fs.writeFileSync(path.join(BACKUP_DIR, `${t}-${stamp}.json`), JSON.stringify(rows, null, 1), "utf8"); } console.log(`backup → ${BACKUP_DIR}`); } const stats = { routes: 0, stations: 0, schedules: 0, sponsors: 0 }; await client.query("begin"); try { for (const p of provinces) { await client.query( `insert into public.provinces (id, name_th) values ($1,$2) on conflict (id) do update set name_th = excluded.name_th`, [p.province_id, p.province_name_th], ); } for (const g of groups) { await client.query( `insert into public.route_groups (id, code, label_th, display_order, is_active) values ($1,$2,$3,$4,$5) on conflict (id) do update set code=excluded.code, label_th=excluded.label_th, display_order=excluded.display_order, is_active=excluded.is_active`, [g.route_group_id, g.route_group, g.route_group_label, g.display_order ?? 0, g.is_active !== false], ); } for (const s of stations) { await client.query( `insert into public.stations (id, code, name_th, city_th, province_th, station_type, lat, lng, attributes, metadata) values ($1,$2,$3,$4,$5,$6,$7,$8,'{}'::jsonb,'{}'::jsonb) on conflict (id) do update set code=excluded.code, name_th=excluded.name_th, city_th=excluded.city_th, province_th=excluded.province_th, station_type=excluded.station_type, lat=excluded.lat, lng=excluded.lng`, [s.station_id, s.station_code, s.station_name_th, s.city_th || null, s.province_th || null, stationType(s.station_type), s.latitude ?? null, s.longitude ?? null], ); stats.stations++; } for (const r of routes) { const phones = (r.phone || []).map((n) => ({ label: "", number: String(n) })); const webUrl = Array.isArray(r.web_url) ? r.web_url[1] : (r.web_url || null); const contact = { phones, web_url: webUrl || null }; await client.query( `insert into public.routes (route_id, name_th, vehicle_type, route_group_id, contact, metadata) values ($1,$2,$3,$4,$5::jsonb,'{}'::jsonb) on conflict (route_id) do update set name_th=excluded.name_th, vehicle_type=excluded.vehicle_type, route_group_id=excluded.route_group_id, contact=excluded.contact`, [String(r.route_id), r.route_name_th, r.vehicle_type || "bus", r.route_group_id, JSON.stringify(contact)], ); await client.query("delete from public.route_provinces where route_id=$1", [String(r.route_id)]); for (const pid of r.province_ids || []) { await client.query( "insert into public.route_provinces (route_id, province_id) values ($1,$2) on conflict do nothing", [String(r.route_id), pid], ); } await client.query("delete from public.route_stops where route_id=$1", [String(r.route_id)]); let seq = 0; for (const sid of r.station_ids || []) { await client.query( "insert into public.route_stops (route_id, station_id, stop_sequence) values ($1,$2,$3) on conflict do nothing", [String(r.route_id), sid, ++seq], ); } stats.routes++; } const routeIds = [...new Set(schedules.map((s) => s.route_id))]; for (const rid of routeIds) { await client.query("delete from public.schedules where route_id=$1", [rid]); } // schedules.id ไม่มี default (schema เดิมสร้างด้วย serial ที่หลุด) → จ่ายค่าเอง const maxRow = await client.query("select coalesce(max(id), 0)::int as max_id from public.schedules"); let nextScheduleId = Number(maxRow.rows[0].max_id) + 1; for (const s of schedules) { await client.query( `insert into public.schedules (id, route_id, station_id, direction_note, departure_times, variants, schedule_note) values ($1,$2,$3,$4,$5::time[],$6::jsonb,$7)`, [nextScheduleId++, s.route_id, s.station_id, s.direction_note, s.departure_times, JSON.stringify(s.variants), s.schedule_note], ); stats.schedules++; } let order = 0; for (const p of affiliates) { await client.query( `insert into public.sponsors (slug, name_th, website_url, display_order, is_active, meta) values ($1,$2,$3,$4,$5,$6::jsonb) on conflict (slug) do update set name_th=excluded.name_th, website_url=excluded.website_url, display_order=excluded.display_order, is_active=excluded.is_active, meta=excluded.meta`, [ p.id, p.brand_th || p.brand || p.id, p.affiliate_link || p.program_url || null, order++, p.in_footer_marquee !== false, JSON.stringify({ brand: p.brand || null, tile_image: p.tile_image || null, program_url: p.program_url || null, status: p.status || null }), ], ); stats.sponsors++; } if (!APPLY) { await client.query("rollback"); console.log("DRY-RUN (rollback) — ใช้ --apply เพื่อเขียนจริง"); } else { await client.query("commit"); console.log("COMMITTED"); } console.log(JSON.stringify(stats)); } catch (err) { await client.query("rollback").catch(() => {}); throw err; } finally { await client.end(); } } main().catch((e) => { console.error("FAILED:", e.code || "", e.message); process.exit(1); });