API Star tools to create CRUD resources.
Project description
API Star CRUD
- Version: :Version: * Version: 0.3.1
- Status: Production/Stable
- Author: José Antonio Perdiguero López
Features
Supported ORM:
- SQLAlchemy through apistar-sqlalchemy.
- Peewee through apistar-peewee-orm.
The resources are classes with a default implementation for methods:
create
: Create a new element for this resource.retrieve
: Retrieve an element of this resource.update
: Update (partially or fully) an element of this resource.delete
: Delete an element of this resource.list
: List resource collection.drop
: Drop resource collection.
The routes for these methods are:
Method | Verb | URL |
---|---|---|
create | POST | / |
retrieve | GET | /{element_id}/ |
update | PUT | /{element_id}/ |
delete | DELETE | /{element_id}/ |
list | GET | / |
drop | DELETE | / |
Quick start
Install API star CRUD:
$ pip install apistar-crud[peewee]
or
$ pip install apistar-crud[sqlalchemy]
Follow the steps:
- Create an input type and output type for your resource:
- Define a model based on your ORM.
- Build your resource using the metaclass specific for your ORM.
- Add the routes for your resource.
SQLAlchemy
Example of a fully functional resource based on SQLAlchemy.
Create an input type and output type:
class PuppyInputType(types.Type):
name = validators.String()
class PuppyOutputType(types.Type):
id = validators.Integer()
name = validators.String()
Define a model:
class PuppyModel(Base):
__tablename__ = "Puppy"
id = Column(Integer, primary_key=True)
name = Column(String)
The resource:
from apistar_crud.sqlalchemy import Resource
class PuppyResource(metaclass=Resource):
model = PuppyModel
input_type = PuppyInputType
output_type = PuppyOutputType
methods = ("create", "retrieve", "update", "delete", "list", "drop")
The resource generates his own routes:
from apistar import Include
routes = [
Include("/puppy", "Puppy", PuppyResource.routes),
]
Peewee
Example of a fully functional resource based on Peewee.
Create an input type and output type:
class PuppyInputType(types.Type):
name = validators.String()
class PuppyOutputType(types.Type):
id = validators.Integer()
name = validators.String()
Define a model:
class PuppyModel(peewee.Model):
name = peewee.CharField()
The resource:
from apistar_crud.peewee import Resource
class PuppyResource(metaclass=Resource):
model = PuppyModel
input_type = PuppyInputType
output_type = PuppyOutputType
methods = ("create", "retrieve", "update", "delete", "list", "drop")
The resource generates his own routes:
from apistar import Include
routes = [
Include("/puppy", "Puppy", PuppyResource.routes),
]
Override methods
To customize CRUD methods you can override them like:
from apistar_crud.peewee import Resource
class PuppyResource(metaclass=Resource):
model = PuppyModel
input_type = PuppyInputType
output_type = PuppyOutputType
methods = ("create", "retrieve", "update", "delete", "list", "drop")
@staticmethod
def create(element: PuppyInputType) -> PuppyOutputType:
# Do your custom process
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
apistar-crud-0.3.1.tar.gz
(16.6 kB
view hashes)
Built Distribution
Close
Hashes for apistar_crud-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e1b04a5d4be5cd192e484e7c24a1f691b45b6536bd9dbbd981df2c8992bd4dd |
|
MD5 | 6a76e3cf6daa572314e775ecca34fb0b |
|
BLAKE2b-256 | f2db66cc78af229501c71f6a64f5b83aaabcc85798681016a5d4ff761447e633 |