Enums and Query Filtering
Introduction
In the previous unit, you learned how to build complete features that require coordination across multiple files. You discovered how to plan features and coordinate changes across controllers, routes, services, and TypeScript types.
Now, you'll learn how to prompt Codex effectively for Express - specific features like enum types and query filtering. To prompt Codex well, you need to understand the Express and TypeScript concepts you're asking for. This lesson teaches you both the concepts and how to prompt Codex effectively for them.
Understanding TypeScript Enums for Predefined Options
TypeScript enums let you define a set of predefined options for a field. Instead of allowing any text, your application accepts only values from a fixed list. This ensures data consistency and type safety.
An enum has two parts:
- Value (stored in the database):
work,personal,shopping. - Label (displayed to users):
Work,Personal,Shopping.
When prompting Codex, be specific about both the values and how they should be defined:
Codex will generate the proper TypeScript structure. You can also ask Codex to explain:
Codex will explain the structure, which helps you understand what you're asking for.
Prompting Codex for Enum Validation
After defining an enum type, you need to validate incoming data against those values. When prompting Codex, specify both the enum and the validation approach:
Codex understands the TypeScript context and will create validation correctly. You can also ask for specific validation libraries like Zod:
Being specific about the validation approach helps Codex implement the right solution.
Prompting Codex for API Endpoints That Return Enum Choices
Your api endpoints should tell clients what enum values are available. When prompting Codex, specify the endpoint structure and response format:
Codex will create the endpoint correctly. You can also ask for specific formatting:
Specifying the response structure helps Codex match your API design.
Prompting Codex for Utility Functions That Map Enums to Display Labels
When working with enums, you often need utility functions to convert values to human-readable labels. When prompting Codex, specify the mapping logic:
Codex will implement the mapping function. You can also ask for more complex transformations:
Being specific about the function purpose and return type helps Codex implement exactly what you need.
Understanding Express Query Filtering
Express query filtering lets you retrieve data based on query parameters. You can filter by exact matches, partial matches, and more. When prompting Codex for filtering, specify the parameter source and filter logic:
Codex will implement the filtering correctly. You can also ask for TypeScript type safety:
Being specific about types and behavior helps Codex implement type - safe filtering.
Prompting Codex for Filtering in Route Handlers
When asking Codex to add filtering to route handlers, be clear about:
- Which
query parametersto use. - What happens when no filter is provided.
- How filters combine (if multiple).
- TypeScript types for
query parameters.
Codex will implement the filtering logic correctly. You can also specify the database query approach using TypeORM:
Specifying the service layer and ORM helps Codex understand the architecture.
Prompting Codex for Query Parameter Validation
When adding query - based filtering, you need to validate that parameters are correct. When prompting Codex, specify validation requirements:
Codex will create the validation middleware. You can also ask for specific error handling:
Specifying error behavior helps Codex implement user - friendly validation.
Prompting Strategies for Express-Specific Features
- Specify typescript types: When asking for
enums, mention string enums and the specificvaluesyou want. - Use validation libraries: When asking for validation, specify Zod or Joi and the validation rules you need.
- Specify request syntax: When asking for
query parameters, usereq.query.categoryto be clear about Express syntax. - Explain response structure: When asking for api endpoints, specify the exact JSON structure you want returned.
- Be specific about database queries: When asking for filtering, specify the ORM you're using (TypeORM, Prisma) and the query approach.
- Include typescript interfaces: When asking for typed parameters, specify
interfacenames and property types.
Summary and What's Next
In this lesson, you learned how to prompt Codex effectively for Express - specific features. You discovered how to understand TypeScript enums for predefined options (values - labels), prompt Codex for enum definitions by specifying string enum values, prompt Codex for validation middleware using libraries like Zod, prompt Codex for api endpoints that return available enum choices, create utility functions that map enum values to display labels, understand Express query filtering with TypeScript types, prompt Codex for filtering in route handlers by specifying parameter sources and query logic, and prompt Codex for query parameter validation. You also learned effective prompting strategies: specifying TypeScript types, using validation libraries like Joi, using correct Express request syntax such as req.query, explaining JSON response structures, being specific about database queries with TypeORM or Prisma, and including TypeScript interfaces.
Next, you'll practice adding these features to your task tracker: categories for organization, priorities for focus, and filtering to help users find tasks through api endpoints.
Move on to the practice exercises to start adding enum types and filtering!
