Skip to main content
Version: Next

Synthesized API Routes

With jsonclasses-server, CRUD routes are automatically synthesized for the targeted classes by decorating.

CRUD Routes#

With @api and it's enable or disable parameter, a class gains the following routes.

C - Create#

With C enabled, a route POST: /{resource-name} is created. See the example.

@api@pymongo@jsonclassclass Product:    id: str = types.readonly.str.primary.mongoid.required    name: str    stock: int# POST /products with JSON or multipart form body.

R - Read#

With R enabled, a route GET: /{resource-name}/{id} is created. See the example.

@api@pymongo@jsonclassclass Product:    id: str = types.readonly.str.primary.mongoid.required    name: str    stock: int# GET /products/{id} with query string.

U - Update#

With U enabled, a route PATCH: /{resource-name}/{id} is created. See the example.

@api@pymongo@jsonclassclass Product:    id: str = types.readonly.str.primary.mongoid.required    name: str    stock: int# PATCH /products/{id} with JSON or multipart form body.

D - Delete#

With D enabled, a route DELETE: /{resource-name}/{id} is created. See the example.

@api@pymongo@jsonclassclass Product:    id: str = types.readonly.str.primary.mongoid.required    name: str    stock: int# DELETE /products/{id}.

L - List#

With L enabled, a route GET: /{resource-name} is created. See the example.

@api@pymongo@jsonclassclass Product:    id: str = types.readonly.str.primary.mongoid.required    name: str    stock: int# GET /products with query string.

E - Ensure#

With E enabled, a route POST: /{resource-name}/ensure is created. This is disabled and not generated by default. This special route performs in a special manner. The unique fields are used to query the database. If it's not exist, a new object is created. If it's exist, an existing object is updated. This is useful for cases like sending authorization code, feeding default data into the database. See the example.

@api(enable='E')@pymongo@jsonclassclass AuthorizationCode:    id: str = types.readonly.str.primary.mongoid.required    email: Optional[str] = types.str.email.unique    phone_no: Optional[str] = types.str.digit.unique    value: str = types.str.fsetonsave(types.randomdigits(4)).required    created_at: datetime = types.readonly.datetime.tscreated.required    updated_at: datetime = types.readonly.datetime.tsupdated.umininterval(timedelta(minutes=1)).required# POST /authorization-codes/ensure with email or phone_no creates a new auth code.

Session Routes#

A class decorated with @authorized can be used to setup user sessions. A signed in user is called operator in JSONClasses permission validation model. With this decorated, a route POST: /{resource-name}/session is created. Unique fields with authidentity modifier is used to find the instance. Fields with modifiers starts with authby are used for authenticating. See the example.

@authorized@api@pymongo@jsonclassclass User:    id: str = types.readonly.str.primary.mongoid.required    username: str = types.str.unique.authidentity.required    password: str = types.writeonly.unqueryable.str.length(8, 16).salt.authbycheckpw.required# POST /users/session with username and password