Skip to content

PyNest CLI Deep Dive 🔍

The PyNest CLI is a powerful tool that helps you quickly scaffold, develop, and manage your PyNest applications. This guide will walk you through all the commands and best practices to make the most out of the PyNest CLI.

Installation 💻

Before using the PyNest CLI, ensure you have it installed. You can install it using pip:

pip install pynest-api

CLI Commands and Usage 🛠️

The PyNest CLI provides a variety of commands to help you create and manage your PyNest applications. Below are the detailed descriptions of the available commands and their usage.

Generate New Application

Create a new PyNest application.

pynest generate application --app-name <app_name> [--db-type <db_type>] [--is-async]
Options

  • --app-name, -n: The name of the new application. (Required)
  • --db-type, -db: The type of database to use (postgresql, mysql, sqlite, mongodb). (Optional)
  • --is-async: Whether the project should be async or not. This is applicable only for relational databases. (Optional)
  • --is-cli: Whether the project should be a CLI App. (Optional)

Example

pynest generate application --app-name my_pynest_app --db-type postgresql --is-async

This example will create a skeleton PyNest application named my_pynest_app with PostgreSQL as the database and async support.

File Structure 🗂️

Here's the typical file structure of a PyNest application generated by the CLI:

my_pynest_app/
├── src/
│   ├── __init__.py
│   ├── app_module.py
│   ├── app_controller.py
│   ├── app_service.py
│   ├── config.py
├── main.py
├── requirements.txt
└── README.md

!Note: The actual file structure may vary based on the modules and components you create

generate command

Generate boilerplate code for various components of the application.

Subcommands

Resource Generate a new resource with the associated controller, service, model and module files, with respect to the project configurations (e.g. database type).

pynest generate resource --name <module_name>

Options

  • --name, -n: The name of the new module. (Required)
  • --path, -p: The path where the module should be created. (Optional)

Example

pynest generate resource --name users

This will create a new resource named users with the associated controller, service, model and module files.

Module

Generate a new module file, which can be used to group related components of the application.

pynest generate module --name <module_name>

Options

  • --name, -n: The name of the new module. (Required)
  • --path, -p: The path where the module should be created. (Optional)

Example

pynest generate module --name auth

This will create a new module named auth in the default path.

Controller

Generate a new controller file, which will handle the routing and responses for a specific resource.

pynest generate controller --name <controller_name>

Options

  • --name, -n: The name of the new controller. (Required)
  • --path, -p: The path where the controller should be created. (Optional)

Example

pynest generate controller --name users

This will create a new controller named users in the default path.

Service

Generate a new service file, which will contain the business logic for a specific resource.

pynest generate service --name <service_name>

Options

  • --name, -n: The name of the new service. (Required)
  • --path, -p: The path where the service should be created. (Optional)

Example

pynest generate service --name users

This will create a new service named users in the default path.

Best Practices 🌟

To ensure a clean and maintainable codebase, follow these best practices when using the PyNest CLI:

  • Consistent Naming Conventions: Use consistent naming conventions for files and directories. For example, use lowercase with underscores for module names (users_module.py) and camelCase for class names (UsersController)._

  • Modular Structure: Keep your code modular. Each module should encapsulate a specific functionality of your application.

  • Service Layer: Keep business logic within services to maintain separation of concerns. Controllers should handle HTTP requests and responses only.

  • Dependency Injection: Use dependency injection to manage dependencies between services and controllers. This makes your code more testable and maintainable.

  • Environment Configuration: Use environment variables to manage configuration settings, such as database connection strings and API keys.

The PyNest CLI is a powerful tool that simplifies the development of PyNest applications. By following the best practices and utilizing the commands effectively, you can build scalable and maintainable applications with ease. Happy coding!