Expert backend development patterns for TypeScript, Node.js, PostgreSQL, and SQL. Emphasizes functional programming, type safety, comprehensive testing, and secure coding practices for maintainable systems.
Expert backend development skill for building maintainable and efficient Node.js applications with TypeScript, PostgreSQL, and comprehensive testing practices.
This skill provides comprehensive guidelines for backend development using TypeScript, Node.js, and PostgreSQL. It emphasizes functional programming patterns, type safety, secure coding practices, SQL optimization, and thorough testing with Jest.
1. **Use Functional Programming Patterns**
- Avoid classes and unnecessary use of `this`
- Write pure functions where possible, especially for utility logic
- Break down code into reusable, modular functions
- Group related functions together logically
2. **Follow Naming Conventions**
- Use camelCase for variables and function names (e.g., `createFlow`, `flowExists`)
- Use PascalCase for interfaces and types
- Prefix boolean variables with `is`, `has`, `should`, or `can` (e.g., `isProjectUser`)
- Maintain consistent naming throughout the codebase
3. **Export Functions Explicitly**
- Export functions and interfaces explicitly
- Group exports logically at the end of files
4. **Use `this: void` in Function Signatures**
- Prevent the use of `this` within functions by adding `this: void` parameter
1. **Prefer Interfaces Over Types**
- Use `interface` for object shapes instead of `type`
- Be explicit with type definitions; avoid `any`
2. **Add Explicit Type Annotations**
- Annotate function parameters and return types for clarity
- Utilize type inference judiciously; be explicit when it improves readability
3. **Use Async/Await for Asynchronous Operations**
- Always use `async` functions with `await` for async operations
- Handle promises properly to avoid unhandled rejections
1. **Create Custom Error Classes**
- Define custom error classes for specific error conditions (e.g., `FlowDoesNotExistError`)
- Include relevant context in error messages (e.g., IDs, resource names)
2. **Validate Inputs Early**
- Validate inputs at the beginning of functions
- Throw errors early with meaningful messages
- Use early returns to handle invalid inputs
3. **Test Error Conditions**
- In Jest tests, use `await expect(...).rejects.toThrow(ErrorClass)` to verify error throwing
1. **Always Use Parameterized Queries**
- Prevent SQL injection by using parameterized queries
- Write SQL using tagged template literals with your SQL handler
2. **Use CTEs for Complex Queries**
- Employ Common Table Expressions (`WITH` clauses) to enhance readability
- Break down complex queries into logical steps
3. **Use Transactions for Multiple Operations**
- Wrap related database operations in transactions (`begin`, `commit`, `rollback`)
- Ensure atomic operations to maintain data integrity
4. **Handle Optional Parameters Carefully**
- Avoid SQL syntax errors by properly handling optional query parameters
5. **Optimize Query Performance**
- Write efficient SQL queries; avoid unnecessary data retrieval
- Ensure appropriate database indexes are in place
- Batch operations when possible to reduce overhead
1. **Use Jest for Testing**
- Organize tests with `describe` and `it` blocks
- Write comprehensive tests covering all public functions
2. **Test All Edge Cases**
- Include tests for normal operation, edge cases, and error conditions
- Ensure tests are deterministic and independent of external state
3. **Use Mocking for Isolation**
- Use mock functions and objects to isolate the unit under test
- Mock database calls and external dependencies
4. **Use Appropriate Assertions**
- Use Jest matchers like `toEqual`, `toThrow`, `rejects.toThrow`
- Verify both successful outcomes and error conditions
1. **Sanitize and Validate All Inputs**
- Always validate user inputs before processing
- Use validation libraries like Zod for schema validation
2. **Follow Secure Coding Practices**
- Prevent SQL injection with parameterized queries
- Avoid exposing sensitive information in error messages or logs
3. **Verify Access Control**
- Check user permissions and access rights before operations
- Implement proper authorization checks
1. **Use Consistent Code Style**
- Use 2-space indentation consistently
- Use single quotes for strings; template literals when necessary
- Use destructuring for objects and arrays
2. **Leverage Modern JavaScript Features**
- Use default parameters for optional arguments
- Use short-circuit evaluation for defaults and conditionals
- Use arrow functions where appropriate
1. **Write JSDoc Comments**
- Document all public functions and interfaces with JSDoc
- Detail parameters, return types, and function purpose
- Keep comments up-to-date with code changes
2. **Use Inline Comments Sparingly**
- Explain complex logic or non-obvious decisions
- Avoid redundant comments that just restate the code
1. **Run Linting After Changes**
- Execute `yarn lint` in the module directory after modifying TypeScript files
- Ensure code adheres to style guidelines
2. **Run Tests After Changes**
- Execute `yarn test` after code modifications
- Verify all tests pass and no regressions are introduced
3. **Follow Systematic Development Process**
- Analyze requirements thoroughly before coding
- Design function signatures and data flow
- Implement code following guidelines
- Write comprehensive tests
- Run linting and tests
- Review code for compliance
- Document the implementation
When building a new backend feature:
1. Define clear interfaces for your data models
2. Create pure functions with explicit type annotations
3. Implement database operations using parameterized queries and transactions
4. Write custom error classes for domain-specific errors
5. Validate inputs at function boundaries
6. Write comprehensive Jest tests covering success and error cases
7. Run `yarn lint` and `yarn test` before committing
8. Document functions with JSDoc comments
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/backend-development-with-typescript-and-nodejs/raw