使用Pydantic基本模型的ArangoDB数据库模型。
arangodantic的Python项目详细描述
阿朗格但丁
使用Pydantic基本模型的ArangoDB数据库模型。在
安装
该软件包在PyPi上可用:
pip install arangodantic # Or with Shylock pip install arangodantic[shylock]
使用
通过扩展DocumentModel
或{
配置Arangodantic。您可以选择定义集合名称前缀, 一个密钥生成功能和一个锁(如果您想使用锁,则需要 的支持功能;Shylock 箱子,任何其他的锁定服务,如 Sherlock至少在理论上也是如此 工作)。在
确保您有一个具有已知凭据的ArangoDB服务器可用
^{pr2}$importasynciofromuuidimportuuid4fromaioarangodbimportArangoClientfrompydanticimportBaseModelfromshylockimportAsyncLockasLockfromshylockimportShylockAioArangoDBBackendfromshylockimportconfigureasconfigure_shylockfromarangodanticimportDocumentModel,EdgeModel,configure# Define modelsclassOwner(BaseModel):"""Dummy owner Pydantic model."""first_name:strlast_name:strclassCompany(DocumentModel):"""Dummy company Arangodantic model."""company_id:strowner:OwnerclassLink(EdgeModel):"""Dummy Link Arangodantic model."""type:strasyncdefmain():# Configure the database settingshosts="http://localhost:8529"username="root"password=""database="example"prefix="example-"client=ArangoClient(hosts=hosts)# Connect to "_system" database and create the actual database if it doesn't exist# Only for demo, you likely want to create the database in advance.sys_db=awaitclient.db("_system",username=username,password=password)ifnotawaitsys_db.has_database(database):awaitsys_db.create_database(database)# Configure Arangodantic and Shylockdb=awaitclient.db(database,username=username,password=password)configure_shylock(awaitShylockAioArangoDBBackend.create(db,f"{prefix}shylock"))configure(db,prefix=prefix,key_gen=uuid4,lock=Lock)# Create collections if they don't yet exist# Only for demo, you likely want to create the collections in advance.awaitCompany.ensure_collection()awaitLink.ensure_collection()# Let's create some example entriesowner=Owner(first_name="John",last_name="Doe")company=Company(company_id="1234567-8",owner=owner)awaitcompany.save()print(f"Company saved with key: {company.key_}")second_owner=Owner(first_name="Jane",last_name="Doe")second_company=Company(company_id="2345678-9",owner=second_owner)awaitsecond_company.save()print(f"Second company saved with key: {second_company.key_}")link=Link(_from=company,_to=second_company,type="CustomerOf")awaitlink.save()print(f"Link saved with key: {link.key_}")# Hold named locks while loading and doing changesasyncwithCompany.lock_and_load(company.key_)asc:assertc.owner==ownerc.owner=second_ownerawaitc.save()awaitcompany.reload()assertc.owner==company.ownerprint(f"Updated owner of company to '{company.owner!r}'")# Let's explore the find functionality# Note: You likely want to add indexes to support the queriesprint("Finding companies owned by a person with last name 'Doe'")asyncwith(awaitCompany.find({"owner.last_name":"Doe"},count=True))ascursor:print(f"Found {len(cursor)} companies")asyncforfound_companyincursor:print(f"Company: {found_company.company_id}")# Supported operators include: "==", "!=", "<", "<=", ">", ">="found_company=awaitCompany.find_one({"owner.last_name":"Doe","_id":{"!=":company}})print(f"Found the company {found_company.key_}")if__name__=="__main__":# Starting from Python 3.7 -># asyncio.run(main())# Compatible with Python 3.6 ->loop=asyncio.get_event_loop()result=loop.run_until_complete(main())
您可能会发现migrate-anything对于创建和管理集合和索引很有用。在
更多示例
- 这个graph example显示了阿兰戈丹蒂克是如何的 用于图形。请注意,图形相关功能位于 这一时刻真的很有限,很可能会在以后延长,甚至可能是 已重组,请谨慎使用。在
许可证
此代码是根据BSD 3条款许可发布的。中的详细信息 LICENSE文件。在
- 项目
标签: