-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb22101
commit 1930f07
Showing
24 changed files
with
1,306 additions
and
96 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# 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: | ||
|
||
```bash | ||
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. | ||
|
||
### create-nest-app | ||
Create a new PyNest application. | ||
|
||
```bash | ||
pynest create-nest-app --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) | ||
|
||
#### Example | ||
```bash | ||
pynest create-nest-app --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: | ||
|
||
```text | ||
my_pynest_app/ | ||
├── src/ | ||
│ ├── __init__.py | ||
│ ├── app_module.py | ||
│ ├── app_controller.py | ||
│ ├── app_service.py | ||
├── main.py | ||
├── requirements.txt | ||
└── README.md | ||
``` | ||
|
||
Note: The actual file structure may vary based on the modules and components you create.: | ||
|
||
|
||
### generate (Alias: g) | ||
Generate boilerplate code for various components of the application. | ||
|
||
#### Subcommands | ||
|
||
**module** | ||
Generate a new module with the associated controller, service, model and module files._ | ||
|
||
```bash | ||
pynest g module --name <module_name> | ||
``` | ||
|
||
**Options** | ||
|
||
|
||
* `--name`, `-n`: The name of the new module. (Required) | ||
|
||
**Example** | ||
```bash | ||
pynest g module --name users | ||
``` | ||
|
||
This will create a new module named `users` with the associated controller, service, model and module files. | ||
|
||
Note: In the future the cli will support more subcommands like `controller`, `service`, `model` and `provider` and `resource` that will replace the current `g module` as in nestjs cli: | ||
|
||
|
||
|
||
## 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! | ||
|
||
--- | ||
<nav class="md-footer-nav"> | ||
<a href="/PyNest/getting_started" class="md-footer-nav__link"> | ||
<span>← Getting Started</span> | ||
</a> | ||
<a href="/PyNest/modules" class="md-footer-nav__link"> | ||
<span>Modules →</span> | ||
</a> | ||
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Controllers in PyNest 🚀 | ||
|
||
The Controller decorator in PyNest is used to define a controller, which is a class responsible for handling incoming requests and returning responses to the client. Controllers register routes in the application and manage request and response objects, effectively acting as the gateway through which clients interact with your application. | ||
|
||
## Defining a Controller | ||
|
||
To create a basic controller in PyNest, use the @Controller decorator. | ||
This decorator is required to define a controller and specify an optional route path prefix, | ||
which helps group related routes and minimize repetitive code. | ||
|
||
```python | ||
from nest.core import Controller, Get, Post | ||
|
||
@Controller('/books') | ||
class BookController: | ||
def __init__(self, book_service: BookService): | ||
self.book_service = book_service | ||
|
||
@Get('/') | ||
def get_books(self): | ||
return self.book_service.get_books() | ||
|
||
@Post('/') | ||
def add_book(self, book): | ||
self.book_service.add_book(book) | ||
return {"message": "Book added successfully!"} | ||
``` | ||
|
||
In the example above: | ||
|
||
The `@Controller('/books')` decorator defines a controller with the prefix `/books`. | ||
|
||
The `BookController` class handles HTTP `GET` requests to `/books` and `POST` requests | ||
to `/books` using the `@Get` and `@Post` decorators, | ||
respectively. | ||
|
||
The `BookService` dependency is injected into the BookController to handle the business logic. | ||
|
||
## Routing | ||
|
||
Routing is the mechanism that controls which controller receives which requests. | ||
Each controller can have multiple routes, and different routes can perform different actions. | ||
|
||
### Example | ||
```python | ||
from nest.core import Controller, Get | ||
|
||
@Controller('/cats') | ||
class CatsController: | ||
@Get('/') | ||
def find_all(self): | ||
return 'This action returns all cats' | ||
``` | ||
|
||
In this example: | ||
|
||
The `@Controller('/cats')` decorator specifies the route path prefix for the `CatsController`. | ||
The `@Get('/')` decorator creates a handler for the HTTP `GET` requests to `/cats`. | ||
When a `GET` request is made to `/cats`, the find_all method is invoked, returning a string response. | ||
|
||
## Creating Controllers Using the CLI (In Progress!) | ||
|
||
To create a controller using the PyNest CLI, execute the following command: | ||
|
||
```bash | ||
pynest generate controller <controller_name> | ||
``` | ||
|
||
For example, to create a BooksController, you would run: | ||
|
||
```bash | ||
pynest generate controller books | ||
``` | ||
|
||
## Handling Requests and Responses | ||
|
||
Controllers in PyNest handle HTTP requests and responses through various decorators | ||
that correspond to HTTP methods like GET, | ||
POST, PUT, DELETE, etc. | ||
|
||
### Example | ||
|
||
```python | ||
from nest.core import Controller, Get, Post, Put, Delete | ||
from .book_service import BookService | ||
from .book_models import Book | ||
|
||
@Controller('/books') | ||
class BooksController: | ||
def __init__(self, book_service: BookService): | ||
self.book_service = book_service | ||
|
||
@Get('/') | ||
def get_books(self): | ||
return self.book_service.get_books() | ||
|
||
@Get('/:book_id') | ||
def get_book(self, book_id: int): | ||
return self.book_service.get_book(book_id) | ||
|
||
@Post('/') | ||
def add_book(self, book: Book): | ||
self.book_service.add_book(book) | ||
return {"message": "Book added successfully!"} | ||
|
||
@Put('/:book_id') | ||
def update_book(self, book_id: int , book: Book): | ||
self.book_service.update_book(book_id, book) | ||
return {"message": "Book updated successfully!"} | ||
|
||
@Delete('/:book_id') | ||
def delete_book(self, book_id: int): | ||
self.book_service.delete_book(book_id) | ||
return {"message": "Book deleted successfully!"} | ||
``` | ||
|
||
When we will go to the docs, we will see this api resource - | ||
|
||
 | ||
|
||
## Best Practices | ||
|
||
* Keep Controllers Focused: Controllers should only handle HTTP requests and delegate business logic to services. | ||
* Use Dependency Injection: Inject services into controllers to manage dependencies effectively. | ||
* Group Related Routes: Use route path prefixes to group related routes and minimize repetitive code. | ||
|
||
|
||
## Conclusion 🎉 | ||
Controllers are essential components in PyNest applications, managing the flow of requests and responses. By defining clear routes and leveraging the power of decorators, you can build efficient and maintainable endpoints for your application. Happy coding! | ||
|
||
|
||
--- | ||
|
||
<nav class="md-footer-nav"> | ||
<a href="/PyNest/modules" class="md-footer-nav__link"> | ||
<span>← Modules</span> | ||
</a> | ||
<a href="/PyNest/providers" class="md-footer-nav__link"> | ||
<span>Providers →</span> | ||
</a> | ||
</nav> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.