Express.js-like framework built on Django
Project description
Shanks Django CLI
๐ CLI tool untuk generate Django project dengan Express.js syntax dan Prisma-like ORM.
๐ฆ Installation
pip install shanks-django
โจ Features
- Express.js-like syntax - Familiar routing
- Prisma-like ORM - Modern database queries
- Auto-caching enabled - GET requests cached by default (10x faster!)
- Smart cache invalidation - Auto-clear on POST/PUT/DELETE
- Route grouping - Organize routes like Gin (Go)
- Auto-type detection - No need to specify
<int:id> - Swagger built-in - Auto-generated API docs
- CLI generators - Generate CRUD & Auth instantly
- SORM CLI - Prisma-like database management
๐ Quick Start
# Buat project baru
shanks new myproject
cd myproject
# Generate CRUD endpoints
shanks create posts --crud
# Generate auth endpoints
shanks create auth --simple
# Run migrations
sorm make
sorm db migrate
# Or use push (make + migrate in one command)
sorm db push
# Start server
shanks run
Visit:
- API: http://127.0.0.1:8000/api/health
- Swagger: http://127.0.0.1:8000/docs
That's it! Your API now has:
- โ Auto-caching enabled (10x faster GET requests)
- โ Smart cache invalidation on writes
- โ Swagger documentation
- โ CRUD endpoints with pagination
๐ ๏ธ CLI Commands
Development Server
# Start server (default: 127.0.0.1:8000)
shanks run
# Custom port
shanks run 3000
# Custom host dan port
shanks run 0.0.0.0:8000
Auto-reload seperti nodemon, langsung detect perubahan file.
Project Management
# Buat project baru dengan struktur Go-like
shanks new myproject
Struktur yang di-generate:
myproject/
โโโ manage.py
โโโ myproject/
โ โโโ settings.py
โ โโโ urls.py
โ โโโ wsgi.py
โโโ app/
โโโ models/
โโโ routes/
โโโ middleware/
โโโ dto/
Generate CRUD Endpoints
# Generate full CRUD dengan model
shanks create posts --crud
Ini akan create:
app/models/posts.py- Model dengan SORMapp/routes/posts.py- Complete CRUD routes
Yang di-generate:
- โ List dengan pagination (page, limit)
- โ Get by ID (findById)
- โ Create
- โ Update
- โ Delete
- โ Auth checks
- โ Error handling
Contoh hasil generate:
# app/routes/posts.py
from shanks import App, Response
from app.models import Post
app = App()
@app.get('api/posts')
def list_posts(req):
page = int(req.query.get('page', 1))
limit = int(req.query.get('limit', 10))
posts = Post.find_many()
return {'posts': [...], 'page': page, 'limit': limit}
@app.get('api/posts/<post_id>')
def get_post(req, post_id):
post = Post.find_unique(id=post_id)
if not post:
return Response().status_code(404).json({'error': 'Not found'})
return {'post': {...}}
@app.post('api/posts')
def create_post(req):
post = Post.create(**req.body)
return Response().status_code(201).json({'id': post.id})
@app.put('api/posts/<post_id>')
def update_post(req, post_id):
post = Post.find_unique(id=post_id)
post.update_self(**req.body)
return {'updated': True}
@app.delete('api/posts/<post_id>')
def delete_post(req, post_id):
post = Post.find_unique(id=post_id)
post.delete_self()
return {'deleted': True}
Generate Auth Endpoints
# Simple auth: /login, /register, /me
shanks create auth --simple
# Complete auth: + email verification
shanks create auth --complete
Yang di-generate untuk --simple:
- POST
/api/auth/register- Register user baru - POST
/api/auth/login- Login user - GET
/api/auth/me- Get current user
Yang di-generate untuk --complete:
- Semua dari
--simple - POST
/api/auth/verify- Email verification - POST
/api/auth/resend- Resend verification email
Database Management (SORM)
# Create migrations
sorm make
# Apply migrations
sorm db migrate
# Create + apply migrations (one command)
sorm db push
# Reset database (flush all data)
sorm db reset
# Open database shell
sorm db shell
# Open admin panel (like Prisma Studio)
sorm studio
Command sorm mirip dengan Prisma CLI:
sorm make=prisma migrate dev --create-onlysorm db migrate=prisma migrate deploysorm db push=prisma db pushsorm studio=prisma studio(tapi pake Django Admin)
Auto-Type Detection di Routes
Sekarang gak perlu specify type di URL parameters! Shanks auto-detect:
# Auto-detect as int (karena nama berakhiran '_id')
@app.get('api/posts/<post_id>')
def get_post(req, post_id):
return {'id': post_id}
# Auto-detect as string
@app.get('api/users/<username>')
def get_user(req, username):
return {'username': username}
# Masih bisa explicit type kalau perlu
@app.get('api/posts/<slug:slug>') # force as slug
def get_user(req, username):
return {'username': username}
Auto-detection rules:
- Parameter ends with
_idatau namaidโ treated asint - Lainnya โ treated as
string - Bisa tetap specify type explicitly:
<int:id>,<slug:slug>,<uuid:uuid>
Route Grouping (Gin-style)
Organize routes dengan grouping seperti Gin di Go:
from shanks import App
app = App()
# Create route group
auth = app.group('api/v1/auth')
@auth.post('login')
def login(req):
return {'message': 'Login'}
@auth.post('register')
def register(req):
return {'message': 'Register'}
@auth.get('me')
def me(req):
return {'user': req.user}
# Include group to main app
app.include(auth)
# urlpatterns auto-generated! โจ
Hasil:
- POST
/api/v1/auth/login - POST
/api/v1/auth/register - GET
/api/v1/auth/me
With Middleware
# Auth middleware
def auth_middleware(req, res, next):
if not req.headers.get('Authorization'):
return Response().status_code(401).json({'error': 'Unauthorized'})
next()
# Protected group with middleware
admin = app.group('api/v1/admin', auth_middleware)
@admin.get('users')
def get_users(req):
return {'users': []}
@admin.get('settings')
def get_settings(req):
return {'settings': {}}
app.include(admin)
Multiple Groups
# Auth routes
auth = app.group('api/v1/auth')
@auth.post('login')
def login(req): ...
# User routes
users = app.group('api/v1/users')
@users.get('')
def list_users(req): ...
@users.get('<user_id>')
def get_user(req, user_id): ...
# Post routes
posts = app.group('api/v1/posts')
@posts.get('')
def list_posts(req): ...
# Include all
app.include(auth, users, posts)
Lihat ROUTE_GROUPING_EXAMPLE.md untuk contoh lengkap!
Built-in Caching (Enabled by Default!)
Shanks automatically caches all GET requests - 10x faster responses with zero configuration!
from shanks import App
# Cache is enabled by default!
app = App()
@app.get('api/posts')
def list_posts(req):
# First request: fetches from DB, caches result
# Next requests: served from cache (10x faster!)
return {'posts': [...]}
@app.post('api/posts')
def create_post(req):
# Automatically invalidates /api/posts cache
return {'created': True}
Customize Cache
# Change cache TTL (default 5 minutes)
app.cache_config(ttl=600) # Cache for 10 minutes
# Cache specific methods
app.cache_config(ttl=300, methods=['GET', 'HEAD'])
# Disable cache for specific group
realtime = app.group('api/realtime')
realtime.disable_cache() # No caching for realtime endpoints
# Different cache settings per group
api_v1 = app.group('api/v1')
api_v1.cache_config(ttl=60) # 1 minute cache
api_v2 = app.group('api/v2')
api_v2.cache_config(ttl=600) # 10 minutes cache
Manual Cache Control
from shanks import invalidate_cache, get_cache
# Clear all cache
invalidate_cache()
# Clear specific pattern
invalidate_cache('/api/posts')
# Direct cache access
cache = get_cache()
cache.set('key', 'value', ttl=300)
value = cache.get('key')
cache.delete('key')
How It Works
- Auto-cache GET requests: First request fetches from DB and caches
- Smart invalidation: POST/PUT/DELETE automatically clear related cache
- Pattern matching:
/api/posts/123invalidates/api/postscache - TTL-based: Cache expires after configured time (default 5 minutes)
Benefits:
- โก 10x faster response times
- ๐ Automatic - no code changes needed
- ๐ง Smart invalidation on writes
- ๐พ Memory efficient with TTL
- ๐ฏ Pattern-based invalidation
Code Quality
# Format code dengan Black
shanks format
# Lint dengan Flake8
shanks lint
# Format + Lint sekaligus
shanks check
Help
# Lihat semua commands
shanks help
๐ Dokumentasi Lengkap
CLI ini adalah bagian dari Shanks Django framework yang menyediakan:
- Express.js-like syntax untuk routing
- Prisma-like ORM untuk database queries
- Built-in caching, CORS, Swagger
- Middleware support
- Multi-database support (PostgreSQL, MySQL, MongoDB, Redis)
Untuk dokumentasi lengkap tentang API, ORM, middleware, dan fitur lainnya:
- GitHub: https://github.com/Ararya/shanks-django
- Documentation: https://github.com/Ararya/shanks-django/wiki
๐ VSCode Extension
Install extension untuk snippets dan IntelliSense:
- Buka VSCode
- Extensions (Ctrl+Shift+X)
- Cari "Shanks Django"
- Install
Atau langsung: https://marketplace.visualstudio.com/items?itemName=Ararya.shanks-django
Snippets yang tersedia:
shanks-app- Create new Shanks appshanks-get- GET routeshanks-post- POST routeshanks-crud- Full CRUD template- Dan banyak lagi...
๐ค Contributing
Contributions welcome! Check Contributing Guide.
# Clone repository
git clone https://github.com/Ararya/shanks-django.git
cd shanks-django
# Install dependencies
pip install -e ".[dev]"
# Run tests
pytest
๐ License
MIT License - see LICENSE file.
๐ Links
- GitHub: https://github.com/Araryarch/shanks-django
- PyPI: https://pypi.org/project/shanks-django/
- Issues: https://github.com/Araryarch/shanks-django/issues
- VSCode Extension: https://marketplace.visualstudio.com/items?itemName=Ararya.shanks-django
Made with โค๏ธ by Ararya
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shanks_django-0.2.1.tar.gz.
File metadata
- Download URL: shanks_django-0.2.1.tar.gz
- Upload date:
- Size: 37.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3286f9bfb94b4f4b14967660887d1b436fd23b6ab0bdf9f3e8a4a22ddf550088
|
|
| MD5 |
30b5f94cccd62964bd4761a91a77fbd9
|
|
| BLAKE2b-256 |
a5db82bbdf040dc49aa6e911795ac5025e723f4c1c8ef877fd491fc288835113
|
File details
Details for the file shanks_django-0.2.1-py3-none-any.whl.
File metadata
- Download URL: shanks_django-0.2.1-py3-none-any.whl
- Upload date:
- Size: 36.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25491ecb011a808196eb26fef4eafd79d6bd036af25537aede5956cbab237848
|
|
| MD5 |
1a5ed7c96f172aecc3b2495a66677cee
|
|
| BLAKE2b-256 |
4f2fac81b252d010d376c05f62d23a3d6d1ea4114f14d6809955834d3ab8e9ff
|