This document describes the API folder and backend setup for storing mesh network location data in PostgreSQL.
api/config.ts- API configuration and endpointsapi/types.ts- TypeScript type definitionsapi/locationApi.ts- API client functions for location dataapi/index.ts- Main export fileapi/example-integration.ts- Example integration with mesh networkapi/README.md- API documentation
server/src/db/connection.js- PostgreSQL database connection poolserver/src/db/schema.js- Database schema initializationserver/src/db/locationRepository.js- Database operations for locationsserver/src/index.js- Updated with location API endpoints and mesh message handlingserver/package.json- Addedpg(PostgreSQL) dependencyserver/README.md- Server documentation
cd server
npm installThis will install the pg package for PostgreSQL connectivity.
Make sure PostgreSQL is installed and running, then create the database:
CREATE DATABASE quakepath;Create a .env file in the server directory:
cd server
# Create .env file (you may need to manually create this)Add the following configuration:
DB_HOST=localhost
DB_PORT=5432
DB_NAME=quakepath
DB_USER=postgres
DB_PASSWORD=your_password_here
PORT=3000
cd server
npm start
# or for development with auto-reload:
npm run devThe server will automatically:
- Connect to PostgreSQL
- Create the database schema (tables and indexes)
- Start listening on port 3000 (or your configured PORT)
In your frontend code, you can set the API URL using environment variables:
- Create a
.envfile in the project root (for Expo) - Set
EXPO_PUBLIC_API_URL=http://your-server-ip:3000 - Or modify
api/config.tsto use your server URL
When a mesh network device sends a location message, the server automatically:
- Receives the message via Socket.IO (
meshMessageevent) - Detects if it's a location message (
type === 'location') - Extracts location data from the message
- Saves it to PostgreSQL database
- Continues normal mesh message forwarding
You can also manually send location data from the frontend:
import { sendLocationToBackend } from '@/api';
const result = await sendLocationToBackend({
deviceId: 'device-123',
latitude: 40.7128,
longitude: -74.0060,
accuracy: 10.5,
timestamp: new Date().toISOString(),
});import { getDeviceLocations, getRecentLocations } from '@/api';
// Get locations for a specific device
const deviceLocations = await getDeviceLocations('device-123', 50);
// Get recent locations from all devices
const recentLocations = await getRecentLocations(100);Save location data to database.
Request:
{
"deviceId": "device-123",
"latitude": 40.7128,
"longitude": -74.0060,
"accuracy": 10.5,
"altitude": 5.2,
"heading": 90.0,
"speed": 0.0,
"timestamp": "2024-01-01T12:00:00Z",
"metadata": {}
}Get locations for a specific device.
Get recent locations from all devices.
The user_locations table stores:
id- Primary keydevice_id- Device identifierlatitude- Latitude coordinatelongitude- Longitude coordinateaccuracy- Location accuracy (meters)altitude- Altitude (meters)heading- Direction (degrees)speed- Speed (m/s)timestamp- Location timestampmetadata- Additional JSON datacreated_at- Record creation time
Indexes are created on:
device_idtimestampcreated_at- Composite:
(device_id, timestamp)
See api/example-integration.ts for a complete example of integrating the API with the mesh network hook.
- Verify PostgreSQL is running:
pg_isready - Check database credentials in
.env - Ensure database exists:
psql -l | grep quakepath
- Check server logs for error messages
- Verify database user has CREATE TABLE permissions
- Manually run schema creation if needed
- Verify server is running and accessible
- Check CORS settings if calling from web browser
- Verify API_BASE_URL in frontend configuration
- Set up your PostgreSQL database
- Configure environment variables
- Start the server
- Test API endpoints
- Integrate with your mesh network code
- Monitor location data in the database
For more details, see:
api/README.md- Frontend API documentationserver/README.md- Backend server documentation