Prompt file imported from yotamach/P-Frog (
.github/prompts/plan-newApi.prompt.md). Copyright stays with the author.
Plan: Create New API with Service, Model, Schema & Tests
This plan outlines the steps to create a complete new API endpoint following the existing patterns in the P-Frog backend. You'll create a model (DTO interface), Mongoose schema, service class with business logic, Express routes, and comprehensive unit tests for each layer.
Steps
-
Create the Model in
apps/api/src/models/as{entity}.model.ts- Define TypeScript interface with DTO fields (e.g.,
ExampleModel) - Add any enums for status/type fields
- Export from
apps/api/src/models/index.tsbarrel
- Define TypeScript interface with DTO fields (e.g.,
-
Create the Schema in
apps/api/src/schemas/as{entity}.schema.ts- Define
IExample extends Documentinterface with Mongoose document fields - Create
ExampleSchemawith field definitions, types, andrequiredflags - Add
toJSONtransform to convert_id→id - Export both
Examplemodel andIExampleinterface - Export from
apps/api/src/schemas/index.ts
- Define
-
Create the Service in
apps/api/src/services/as{entity}.service.ts- Create
ExampleServiceclass followingtask.service.tspattern - Implement CRUD methods:
getAll,getById,create,update,delete - Use
@schemasfor database operations andtslogLogger - Export from
apps/api/src/services/index.ts
- Create
-
Create the Route in
apps/api/src/routes/as{entity}.route.ts- Create Express Router with handlers for GET/POST/PATCH/DELETE
- Instantiate service, apply
authmiddleware to protected routes - Export as
AppRouterobject:{ url: '/{entity}s', router } - Export from
apps/api/src/routes/index.ts
-
Register the Route in
apps/api/src/main.ts- Import new route and call
app.addRouter(exampleRoutes)alongside existing routes
- Import new route and call
-
Create Unit Tests for all layers:
- Schema test at
schemas/tests/{entity}.schema.spec.ts: Test required fields, validation errors, model creation - Service test at
services/tests/{entity}.service.spec.ts: Mock schema module, test each CRUD method - Route test at
routes/tests/{entity}.route.spec.ts: Use supertest, mock service, test HTTP responses
- Schema test at
Further Considerations
- What entity name? You have
settings.schema.tsopen—should this be a "Settings" API or a different entity? - Authentication scope? Should all routes require auth, or should some be public (e.g., GET for read-only data)?
- User ownership? Should records be scoped by
created_byuser field like Tasks, or globally accessible?