This commit is contained in:
165
src/dal/postgresDAL.js
Normal file
165
src/dal/postgresDAL.js
Normal file
@@ -0,0 +1,165 @@
|
||||
// src/dal/postgresdal.js
|
||||
import pg from "pg";
|
||||
|
||||
const pool = new pg.Pool({
|
||||
connectionString: "postgresql://nate@localhost:5432/gtfs"
|
||||
});
|
||||
|
||||
async function dbQuery(sql, params = []) {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
const res = await client.query(sql, params);
|
||||
return res.rows;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
function toNumber(v) {
|
||||
const n = Number(v);
|
||||
return Number.isFinite(n) ? n : undefined;
|
||||
}
|
||||
|
||||
/* Vehicles */
|
||||
|
||||
export async function getVehicles() {
|
||||
const rows = await dbQuery(
|
||||
`SELECT vehicle_id, route_num, route_name, destination,
|
||||
bearing, speed, latitude, longitude
|
||||
FROM vehicles`
|
||||
);
|
||||
|
||||
return rows.map((v) => ({
|
||||
vehicleId: v.vehicle_id,
|
||||
routeNum: v.route_num,
|
||||
routeName: v.route_name,
|
||||
destination: v.destination,
|
||||
bearing: v.bearing == null ? undefined : toNumber(v.bearing),
|
||||
speed: v.speed == null ? undefined : toNumber(v.speed),
|
||||
location: {
|
||||
latitude: toNumber(v.latitude),
|
||||
longitude: toNumber(v.longitude),
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
export async function getVehicleById(id) {
|
||||
if (id == null) return null;
|
||||
|
||||
const rows = await dbQuery(
|
||||
`SELECT vehicle_id, route_num, route_name, destination,
|
||||
bearing, speed, latitude, longitude
|
||||
FROM vehicles
|
||||
WHERE vehicle_id = $1`,
|
||||
[String(id)]
|
||||
);
|
||||
|
||||
if (!rows.length) return null;
|
||||
const v = rows[0];
|
||||
|
||||
return {
|
||||
vehicleId: v.vehicle_id,
|
||||
routeNum: v.route_num,
|
||||
routeName: v.route_name,
|
||||
destination: v.destination,
|
||||
bearing: v.bearing == null ? undefined : toNumber(v.bearing),
|
||||
speed: v.speed == null ? undefined : toNumber(v.speed),
|
||||
location: {
|
||||
latitude: toNumber(v.latitude),
|
||||
longitude: toNumber(v.longitude),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/* Routes */
|
||||
|
||||
export async function getRoutes() {
|
||||
const rows = await dbQuery(`SELECT data FROM routes ORDER BY route_id`);
|
||||
return rows.map((r) => r.data);
|
||||
}
|
||||
|
||||
export async function getRouteById(routeId) {
|
||||
if (routeId == null) return null;
|
||||
|
||||
const rows = await dbQuery(
|
||||
`SELECT data FROM routes WHERE route_id = $1`,
|
||||
[String(routeId)]
|
||||
);
|
||||
|
||||
return rows[0]?.data || null;
|
||||
}
|
||||
|
||||
/* Route paths */
|
||||
|
||||
export async function getRoutePathsMap() {
|
||||
const rows = await dbQuery(`SELECT route_id, path FROM route_paths`);
|
||||
const map = {};
|
||||
for (const row of rows) {
|
||||
map[String(row.route_id)] = row.path;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
export async function getRoutePath(routeId) {
|
||||
if (routeId == null) return null;
|
||||
|
||||
const rows = await dbQuery(
|
||||
`SELECT path FROM route_paths WHERE route_id = $1`,
|
||||
[String(routeId)]
|
||||
);
|
||||
|
||||
return rows[0]?.path || null;
|
||||
}
|
||||
|
||||
/* Stations */
|
||||
|
||||
function transformationStationRow(row) {
|
||||
return {
|
||||
stop_id: row.stop_id,
|
||||
stop_code: row.stop_code,
|
||||
stop_name: row.stop_name,
|
||||
stop_desc: row.stop_desc,
|
||||
location: {
|
||||
latitude: toNumber(row.latitude),
|
||||
longitude: toNumber(row.longitude),
|
||||
},
|
||||
stop_url: row.stop_url,
|
||||
location_type: row.location_type,
|
||||
parent_station: row.parent_station,
|
||||
lines: row.lines, // text[]
|
||||
};
|
||||
}
|
||||
|
||||
export async function getStationsRaw() {
|
||||
// For compatibility, just return the full array of normalized station rows.
|
||||
const rows = await dbQuery(`SELECT * FROM stations`);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function getStations() {
|
||||
const rows = await dbQuery(`SELECT * FROM stations`);
|
||||
return rows.map(transformationStationRow);
|
||||
}
|
||||
|
||||
export async function getStopsByRoute(routeId) {
|
||||
if (routeId == null) return [];
|
||||
// lines @> ARRAY[$1]::text[] means: lines contains this value
|
||||
const rows = await dbQuery(
|
||||
`SELECT * FROM stations WHERE lines @> ARRAY[$1]::text[]`,
|
||||
[String(routeId)]
|
||||
);
|
||||
return rows.map(transformationStationRow);
|
||||
}
|
||||
|
||||
export async function getStationById(id) {
|
||||
if (id == null) return null;
|
||||
|
||||
const rows = await dbQuery(
|
||||
`SELECT * FROM stations WHERE stop_id = $1`,
|
||||
[String(id)]
|
||||
);
|
||||
|
||||
if (!rows.length) return null;
|
||||
return transformationStationRow(rows[0]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user