Update README.md
This commit is contained in:
36
README.md
36
README.md
@@ -1,8 +1,8 @@
|
|||||||
# Djarea
|
# DJAREA
|
||||||
|
|
||||||
Django + React server functions. RPC, not REST.
|
A modern Django + React Framework for perfectionists with deadlines.
|
||||||
|
|
||||||
Write a Python function. Djarea generates a typed React hook. No routes, no serializers, no endpoint boilerplate.
|
Write a Pydantic function, add the @client decorator, use configurable **Shape** types for your models.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@client
|
@client
|
||||||
@@ -10,15 +10,19 @@ def current_user(request) -> UserShape:
|
|||||||
return UserShape.query(lambda qs: qs.filter(pk=request.user.pk))[0]
|
return UserShape.query(lambda qs: qs.filter(pk=request.user.pk))[0]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Djarea generates the entire React client: all your type interfaces, function call hooks, automatic JWT, and a simple <DjangoContext/> to make it all work. No API routing, no serializers, no REST/CRUD bullshit.
|
||||||
|
|
||||||
```tsx
|
```tsx
|
||||||
const user = useCurrentUser() // typed, cached, SSR-hydrated
|
const user: UserShape = useCurrentUser() // typed, cached, SSR-hydrated
|
||||||
```
|
```
|
||||||
|
|
||||||
The decorator is the API contract. The Shape is the query plan. The hook is generated. That's it.
|
The **Function** is the API contract. The **Shape** is the query. The hook is the artifact. That's it.
|
||||||
|
|
||||||
|
Starts with session auth and upgrades to JWT on login. **It just works**.
|
||||||
|
|
||||||
## What Djarea does
|
## What Djarea does
|
||||||
|
|
||||||
A `@client` function in Django becomes a callable hook in React. The function's type signature controls everything — input validation, output serialization, TypeScript types, and SQL projection.
|
A `@client` function in Django becomes a callable hook in React. The function's type signature orchestrates the entire pipeline for you — input validation, output serialization, TypeScript interfaces, and SQL projection.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class ArticleShape(Shape[Article]):
|
class ArticleShape(Shape[Article]):
|
||||||
@@ -28,12 +32,12 @@ class ArticleShape(Shape[Article]):
|
|||||||
tags: list[TagShape] = []
|
tags: list[TagShape] = []
|
||||||
```
|
```
|
||||||
|
|
||||||
This Shape does three things at once:
|
One Djarea **Shape** does three things simultaneously:
|
||||||
- Defines the Pydantic model for validation and serialization
|
- Defines the Pydantic model for validation and serialization
|
||||||
- Generates the django-readers spec, so the SQL query selects exactly these fields and nothing else
|
- Generates a django-readers spec for a lean, field-scoped SQL query
|
||||||
- Produces the TypeScript type on the React side
|
- Produces the TypeScript interface on the React side
|
||||||
|
|
||||||
One definition. Three layers stay in sync automatically.
|
Shapes are your codebase's **single source of truth** for backend/frontend data transfer.
|
||||||
|
|
||||||
## Quick start
|
## Quick start
|
||||||
|
|
||||||
@@ -58,10 +62,10 @@ from django.core.asgi import get_asgi_application
|
|||||||
application = wrap_asgi(get_asgi_application())
|
application = wrap_asgi(get_asgi_application())
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. Define server functions
|
### 2. Define your client functions
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# myapp/djarea_clients.py
|
# myapp/clients.py
|
||||||
from djarea.client import client
|
from djarea.client import client
|
||||||
from djarea.shapes import Shape
|
from djarea.shapes import Shape
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -74,10 +78,12 @@ def echo(request, text: str) -> EchoOutput:
|
|||||||
return EchoOutput(message=text)
|
return EchoOutput(message=text)
|
||||||
```
|
```
|
||||||
|
|
||||||
Functions in `djarea_clients.py` are discovered automatically — same convention as `models.py`.
|
Functions in `clients.py` are discovered automatically — same convention as `models.py`.
|
||||||
|
|
||||||
### 3. Generate TypeScript
|
### 3. Generate TypeScript
|
||||||
|
|
||||||
|
To get your generated React client, set this up in your frontend root:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// django.config.mjs
|
// django.config.mjs
|
||||||
export default {
|
export default {
|
||||||
@@ -91,6 +97,8 @@ export default {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Run this command everytime your client needs updating. You can also throw this it on a file watcher pointed at your backend code:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npx djarea-generate
|
npx djarea-generate
|
||||||
```
|
```
|
||||||
@@ -126,7 +134,7 @@ function MyComponent() {
|
|||||||
|
|
||||||
## Shapes
|
## Shapes
|
||||||
|
|
||||||
Shapes are Djarea's projection system. A Shape defines exactly which fields to select from the database, validated through Pydantic and projected through django-readers. Different views get different Shapes — same model, different queries.
|
Shapes are Djarea's data protocol. A Shape defines exactly which fields to select from the database, validated through Pydantic and projected through django-readers. Different views get different Shapes — same model, different queries.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Full detail page — joins books with chapters
|
# Full detail page — joins books with chapters
|
||||||
|
|||||||
Reference in New Issue
Block a user