Skip to main content

集中数据库访问

项目描述

Schemapy允许您为集中式数据库访问生成对象。您为您的 API 定义架构、需要执行的代码,然后为您处理验证部分。然后将所有内容封装在一个对象中。

Schemapy依赖PyDAL进行模式定义,但不依赖于它。

有关更多信息,请参阅文档

执照 发展状况 最新发布的 支持的 Python 版本 支持的 Python 实现 下载格式 构建状态 代码测试覆盖率 代码健康

安装

pip install schemapy

用法

使用PyDAL

from schemapy import API, DAL, Field
from datetime import datetime, timedelta

db = DAL('sqlite:memory')
db.define_table(
    'users',
    Field('name', type='string', required=True),
    Field('created_on', type='date')
)

db.define_table(
    'posts',
    Field('subject', type='string', required=True),
    Field('author', type='reference users', required=True),
    Field('created_on', type='date'),
    Field('content', type='text', required=True)
)

api = API(db)

@api.as_action(
    type='read',
    request=[
        Field('begin', type='date', required=True),
        Field('end', type='date', required=True)
    ],
    response=db.posts
)
def select_posts_by_date(db, req, action):
    query = (db.posts.created_on >= req.begin) | (db.posts.created_on <= req.end)
    return db(query).select()

now = datetime.now()
result = api.select_posts_by_date(
    begin=now - timedelta(days=1),
    end=now
)
print(list(result))

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

schemapy-0.2.4.tar.gz (6.6 kB 查看哈希)

已上传 source

内置分布

schemapy-0.2.4-py2.py3-none-any.whl (10.5 kB 查看哈希

已上传 2 7