A simple Node.js ES2023 headless API with Drizzle ORM for Victorian state district postcode lookup.
- Postcode Lookup: Look up district information by postcode
- PostgreSQL: Uses PostgreSQL database with Drizzle ORM
- ES2023: Modern JavaScript with ES modules
- Express: RESTful API with Express.js
- CORS: Cross-origin resource sharing enabled
- Node.js 18+
- PostgreSQL 12+
- npm or yarn
-
Clone and install dependencies:
cd vic-state-district-api npm install -
Create PostgreSQL database:
createdb vic-state-districts
-
Set up environment variables:
cp env.example .env # Edit .env with your database credentials if needed -
Generate and run database migrations:
npm run db:generate npm run db:migrate
-
Start the server:
# Development mode with auto-reload npm run dev # Production mode npm start
- GET
/health- Check if the API is running
- POST
/api/postcode_lookup- Lookup district by postcode - GET
/api/postcode_lookup/:postcode- Lookup district by postcode (URL parameter)
{
"postcode": "3000"
}{
"success": true,
"data": {
"postcode": 3000,
"districts": [
{
"name": "Melbourne",
"id": 1,
"mp": {
"name": "John Smith",
"party": "Labor",
"phoneNumber": "+61 3 1234 5678"
}
},
{
"name": "Richmond",
"id": 2,
"mp": {
"name": "Jane Doe",
"party": "Liberal",
"phoneNumber": "+61 3 9876 5432"
}
}
]
}
}id(serial, primary key)postcode_number(integer, unique)created_at(timestamp)updated_at(timestamp)
id(serial, primary key)name(text)district_id(integer, unique)mp_id(integer, foreign key to mps.id)created_at(timestamp)updated_at(timestamp)
id(serial, primary key)name(text)party(text)phone_number(text)created_at(timestamp)updated_at(timestamp)
postcode_id(integer, foreign key to postcodes.id)district_id(integer, foreign key to districts.id)created_at(timestamp)- Primary key: (
postcode_id,district_id)
- Many-to-Many:
postcodes↔districts(viapostcode_districts) - One-to-Many:
mps→districts
npm start- Start the servernpm run dev- Start with auto-reloadnpm run db:generate- Generate database migrationsnpm run db:migrate- Run database migrationsnpm run db:studio- Open Drizzle Studio
vic-state-district-api/
├── src/
│ ├── db/
│ │ ├── connection.js # Database connection
│ │ └── schema.js # Database schema
│ ├── routes/
│ │ └── postcode.js # Postcode lookup routes
│ └── server.js # Main server file
├── drizzle/ # Generated migrations
├── drizzle.config.js # Drizzle configuration
├── package.json
└── README.md
DATABASE_URL- PostgreSQL connection string (default:postgresql://localhost:5432/vic-state-districts)PORT- Server port (default: 3000)
MIT