Property Manager API Development
Guide for developing the Property Manager API - a Node.js/Express backend application using Supabase for data persistence.
Running the Application
Use these commands to run, test, and develop the application:
**Development mode (hot reload)**: `npm run dev`**Production mode**: `npm start`**Run all tests**: `npm test`**Run specific test file**: `npx jest tests/filename.test.js`Code Style and Architecture Conventions
When writing or modifying code in this repository, follow these established patterns:
Module System
Use **CommonJS modules** (`require`/`module.exports`)Do not use ES6 imports/exportsNaming Conventions
**camelCase**: variables, functions, and object properties**PascalCase**: classes and constructor functionsAsync Patterns
Prefer **async/await** over raw Promises or callbacksAlways use try/catch blocks in async controller functions for error handlingValidation
Use **express-validator** for input validation in routesDefine validation rules before controller handlersReturn structured validation errors to clientsArchitecture Patterns
**Routes**: Define endpoints and apply validation middleware**Controllers**: Handle business logic and return structured responses with appropriate HTTP status codes**Middleware**: Use Express middleware for authentication and cross-cutting concerns**Database**: Use the Supabase client for all database operationsError Handling
Controllers should catch errors and return structured error responsesInclude appropriate HTTP status codes (400 for client errors, 500 for server errors)Log errors server-side for debuggingResponse Format
Return consistent JSON response structuresInclude status codes, data payloads, and error messages as appropriateUse appropriate HTTP status codes for success (200, 201) and errors (400, 401, 404, 500)Example Patterns
Controller Function
```javascript
async function getSomething(req, res) {
try {
const { id } = req.params;
// Business logic here
const data = await supabase.from('table').select('*').eq('id', id);
return res.status(200).json({ success: true, data });
} catch (error) {
console.error('Error:', error);
return res.status(500).json({ success: false, error: error.message });
}
}
```
Route with Validation
```javascript
const { body, validationResult } = require('express-validator');
router.post(
'/endpoint',
[
body('field').notEmpty().withMessage('Field is required'),
body('email').isEmail().withMessage('Invalid email')
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
// Call controller...
}
);
```
Database Operations
Use Supabase client for all queriesHandle database errors gracefullyUse appropriate Supabase query methods (.select(), .insert(), .update(), .delete())Testing
Write tests for all routes and controllersUse Jest as the testing frameworkTest success paths and error handlingMock Supabase client in testsWhen working on this codebase, maintain consistency with these patterns and conventions.