Skip to content

Commit

Permalink
add new docs to pynest
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayTheDar committed May 22, 2024
1 parent bb22101 commit 1930f07
Show file tree
Hide file tree
Showing 24 changed files with 1,306 additions and 96 deletions.
17 changes: 0 additions & 17 deletions docs/api.md

This file was deleted.

12 changes: 12 additions & 0 deletions docs/async_orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,15 @@ class ExampleModule:
```shell
uvicorn "src.app_module:http_server" --host "0.0.0.0" --port "8000" --reload
```

---


<nav class="md-footer-nav">
<a href="/PyNest/blank" class="md-footer-nav__link">
<span>&larr; Application Example With sync ORM</span>
</a>
<a href="/PyNest/mongodb" class="md-footer-nav__link">
<span>Application Example With MongoDB &rarr;</span>
</a>
</nav>
11 changes: 11 additions & 0 deletions docs/blank.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,14 @@ uvicorn "src.app_module:http_server" --host "0.0.0.0" --port "8000" --reload
```

Now you can access the application at http://localhost:8000/docs and test the endpoints.

---

<nav class="md-footer-nav">
<a href="/PyNest/dependency_injection" class="md-footer-nav__link">
<span>&larr; Dependency Injection</span>
</a>
<a href="/PyNest/sync_orm" class="md-footer-nav__link">
<span>Application Example With ORM &rarr;</span>
</a>
</nav>
Binary file added docs/book_resource_api_docs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions docs/cli.md
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>&larr; Getting Started</span>
</a>
<a href="/PyNest/modules" class="md-footer-nav__link">
<span>Modules &rarr;</span>
</a>
</nav>
141 changes: 141 additions & 0 deletions docs/controllers.md
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 -

![img.png](book_resource_api_docs.png)

## 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>&larr; Modules</span>
</a>
<a href="/PyNest/providers" class="md-footer-nav__link">
<span>Providers &rarr;</span>
</a>
</nav>
12 changes: 0 additions & 12 deletions docs/database.md

This file was deleted.

8 changes: 0 additions & 8 deletions docs/decorators/controller.md

This file was deleted.

16 changes: 0 additions & 16 deletions docs/decorators/module.md

This file was deleted.

8 changes: 0 additions & 8 deletions docs/decorators/service.md

This file was deleted.

9 changes: 0 additions & 9 deletions docs/decorators/utils.md

This file was deleted.

Loading

0 comments on commit 1930f07

Please sign in to comment.