跳转到主要内容

Dante,一个由SQLite支持的Python文档存储

项目描述

Dante,一个由SQLite支持的Python文档存储

Build Coverage PyPI

Dante是一个零配置、易于使用的Python文档存储(NoSQL数据库)。它非常适合探索性编程、原型设计、内部工具和简单的小型项目。

Dante可以存储Python字典或Pydantic模型,支持同步和异步模式,基于SQLite。

Dante不支持SQL、关系、ACID、聚合、复制,并且明确不是Web规模的。如果您需要这些功能,应选择其他数据库或ORM引擎。

快速入门

  1. 通过PyPI安装

    pip install dante-db
    
  2. 使用Python字典(示例

    from dante import Dante
    
    # Create 'mydatabase.db' in current directory and open it
    # (you can omit the database name to create a temporary in-memory database.)
    db = Dante("mydatabase.db")
    
    # Use 'mycollection' collection (also known as a "table")
    collection = db["mycollection"]
    
    # Insert a dictionary to the database
    data = {"name": "Dante", "text": "Hello World!"}
    collection.insert(data)
    
    # Find a dictionary with the specified attribute(s)
    result = collection.find_one(name="Dante")
    print(result["text"])
    
    new_data = {"name": "Virgil", "text": "Hello World!"}
    collection.update(new_data, name="Dante")
    

在内部,Dante将每个字典存储在SQLite数据库中的一个表中(每个集合一个表),该表有一个JSON编码的TEXT列。

与Pydantic一起使用

Dante非常适合与Pydantic一起使用。

使用与纯Python对象相同的API,您可以对Pydantic模型进行插入、查询和删除(示例

from dante import Dante
from pydantic import BaseModel

class Message(BaseModel):
    name: str
    text: str

# Open the database and get the collection for messages
db = Dante("mydatabase.db")
collection = db[Message]

# Insert a model to the database
obj = Message(name="Dante", text="Hello world!")
collection.insert(obj)

# Find a model with the specified attribute(s)
result = collection.find_one(name="Dante")
print(result.text)

# Find a model in the collection with the attribute name=Dante
# and update (overwrite) it with the new model data
result.name = "Virgil"
collection.update(result, name="Dante")

异步Dante

Dante支持使用相同的API进行异步使用,无论是纯Python对象还是Pydantic模型(示例

from asyncio import run
from dante import AsyncDante

async def main():
    db = AsyncDante("mydatabase.db")
    collection = await db["mycollection"]

    data = {"name": "Dante", "text": "Hello World!"}
    await collection.insert(data)

    result = await collection.find_one(name="Dante")
    print(result["text"])

    new_data = {"name": "Virgil", "text": "Hello World!"}
    await collection.update(new_data, name="Dante")

    await db.close()

run(main())

示例

查看命令行ToDo应用、简单的FastAPI CRUD应用以及示例目录中的其他示例。

开发

有关如何开发、测试和发布Dante的详细指南可在开发者文档中找到。

许可证(MIT)

版权(c)2024. Senko Rasic

特此授予任何获得此软件和相关文档副本(“软件”)的人免费使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向软件提供的人这样做,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于对适销性、特定用途适用性和非侵权的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论这些责任源于合同行为、侵权或其他行为,也不论这些责任是否与软件、使用或其他与软件相关的行为有关。

项目详情


下载文件

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

源代码分布

dante_db-0.1.1.tar.gz (12.5 kB 查看散列值)

上传时间 源代码

构建分布

dante_db-0.1.1-py3-none-any.whl (8.5 kB 查看散列值)

上传时间 Python 3

支持者